mysql –u 用户名 -p密码
create database 数据库名;
show create databse 数据库名;
show databases;
drop database 数据库名;
use 数据库名;
select @@tx_isolation;
show warnings;
mysqldump –u 用户名 –p密码 数据名 > 存放路径 文件名.sql
1.创建一个数据库 mydb2 ,但是这个数据库目前是空.
2.use 数据名
3.在mysql 控制台下 使用 source 备份文件路径
source 文件路径.sql
create table if not exists users9(
id1 int primary key comment '学号',
id2 primary key auto_increment,
id3 int unique,
_float float(5,2) zerofill,
money decimal(10,2),
birthday date,
hiredate1 datetime,
hiredate2 timestamp,
year1 year,
year2 year(2),
int_1 tinyint unsigned,
int_2 smallint,
int_3 int,
int_4 bigint,
name varchar(64) default NULL,
gender enum('男','女','保密') not null,
_bit bit(64),
content text,
bool boolean
)character set utf8, collate utf8_general_ci, comment='表的注释';
create table 新表 like 原表;
create table 新表
as
select 源表字段1 新表字段1, 源表字段2 新表字段2, ...
from 源表;
insert into 新表 (新表字段) select 源表字段 from 原表;
show tables;
show tables like '%x';
desc(describe) 表名;
show columns from student;
show create table test2;
rename table 原表名 to 新表名;
alter table 表名 character set(=) 字符集名;
alter table test1 comment '修改后的表的注释';
alter table test1 modify column id int comment '修改后的字段注释';
show create table test1;
show full columns from test;
drop table 表名;
truncate table 表名;
start transaction;
rollback;
commit;
set autocommit=false;
savepoint 保存点名称;
rollback to 保存点名称;
commit;
alter table 表名 add 字段名 数据类型;
alter table 表名 add 字段名 tinyint(2) zerofill;
alter table 表名 add 字段名 数据类型 first;
alter table 表名 drop 字段名;
alter table 表名 modify 字段名 新的数据类型;
alter table 表名 modify 字段名 新的数据类型 (after id);
alter table 表名 change 原名 改后名 数据类型;
alter table 表名 add index 索引别名 (字段名 );
drop index 索引别名 on 表名;
insert into 表名(字段名) values(值);
insert into 表名 select * from 表名;
update 表名 set 字段名 = 值;
delete from 表名 where 条件;
create table 新表表名(字段1 字段1类型,字段2 字段2类型);
insert into 新表表名 (select 字段1, 字段2 from 原表表名);
insert into 表名 values('重复的主键值', '新数据') on duplicate key update 字段名 = '新数据';
select distinct * from 表名 where 条件;
select * from 表名 where 字段名 like 'a%';
select ifnull(字段名, 0) from 表名;
select english as '英语', math as 数学 from 表名;
select 别名.* from 表名 as 别名;
select * from 表名 as 别名 where 别名.字段名 = 数据;
select * from 表名1 as 别名1 inner join 表名2 as 别名2 on 别名1.字段名 = 别名2.字段名;
select * from 表名 where 字段名 in (值1, 值2, ...);
select count(*) from 表名;
select * from student limit m-1, n-1;
select * from 表名
union [all]
select * from 表名;
select 字段名1, 字段名2 from 表名1
union
select 字段名3, 字段名4 from 表名2;
(select 字段名1, 字段名2 from 表名1 order by 字段名 desc limit 999)
union
(select 字段名3, 字段名4 from 表名2 order by 字段名 limit 999);
select sno from 表1 , 表2 where 条件;
select 字段 from 表1
left join 表2
on 表1.字段名1 = 表2.字段名1;
select 字段 from 表1
right [outer] join 表2
on 表1.字段名1 = 表2.字段名1;
select 字段 from 表1
inner join 表2
on 表1.字段名1 = 表2.字段名1;
select 字段 from 表1 inner join 表2 using(sno);
select 字段 from 表1
natural left join 表2;
create view 视图名 as
select ...;
alter view 视图名
as
select ...;
drop view 视图名;
insert into 视图名 values(...);
delete from 视图名 where 条件;
update 视图名 set 条件;
create view 视图名
as
select ... where age > 30 with check option;
create algorithm = temptable view 视图名
as
select ...;
https:
create table test9(
id int primary key,
name char
);
create table test8(
id int,
name char,
primary key(id, name)
);
alter table 表名 add primary key(字段名,字段名);
alter table 表名 modify 字段名 列数据类型 primary key;
alter table 表名 drop primary key;
create table unique1(
id int unique key,
name char
);
create table unique2(
id int,
name char,
unique(id, name)
);
alter table 表名 add unique (字段名,字段名);
alter table unique1 drop index id;
create table my_auto(id int primary key auto_increment, num int);
alter table my_auto2 add primary key auto_increment(id);
insert into my_auto values(NULL,11);
insert into my_auto values(default,12);
insert into my_auto values(4,14);
insert into my_auto (num) values(15);
insert into my_auto values(3,13);
alter table my_auto auto_increment = 8;
alter table my_auto modify id int;
alter table my_auto drop primary key;
show variables like 'auto_increment%';
set auto_increment_increment = 5;
set auto_increment_increment = 100;
truncate 表名;
create table student(
id int primary key comment '学生id'
);
create table class1(
class_id int primary key comment '班级id',
class_name varchar(20) not null,
id int comment '学生id',
foreign key(id) references student(id)
constraint 别名 foreign key(id) references student(id)on delete set null on update cascade
);
alter table 子表名 add [constraint 别名] foreign key(子表字段名) references 父表名(父表字段名);
alter table 表名 drop foreign key 别名;
select day(now());
select current_date();
select time(now());
select current_time();
select now();
select current_timestamp();
select second(now());
select second(current_time());
select date_add(now(), interval + 365 day);
select date_sub(now(), interval - 365 day);
select * from 表名 where date_add(字段名(时间类型), interval 2 hour) >= now();
select * from 表名 where datediff(now(), 字段名) > 100;
select charset(字段名) from 表名;
select concat('ab', 'c', 'd'...);
select concat(字段名, 'abc') from 表名;
select instr('abcdef', 'a');
select ucase('abc');
select lcase('ABC');
select left('abcde', 3);
select left('abcde', 3);
select substring('abcd', 2, 2);
select length('abc');
select replace('abca', 'a', 'x');
select strcmp('b', 'a');
ltrim(string)
rtrim(string)
trim(string)
select bin(255);
select hex(255);
select ceiling(2.0000001);
select floor(2.999999);
select format(2.345, 2);
select mod(3,2);
select rand();
delimiter ||
delimiter ;
begin
end
grant select on sport.* to 'normal'@'localhost' identified by password '*F3F91B23FC1DB728B49B1F22DEE3D7A839E10F0E' with grant option; -- with grant option 允许授权给其他用户
-- 查询出你的密码对应的字符串,如:"normal" 对应上面的 'F3F91B23FC1DB728B49B1F22DEE3D7A839E10F0E'
select password('你想输入的密码');
-- 刷新权限
flush privileges;
-- 查看当前用户的权限
show grants;
-- 查看某个用户的权限
show grants for 'normal'@'localhost';
-- 授权,把更新权限给 normal'@'localhost 用户(需要在root账户下操作)
grant update on sport.* to 'normal'@'localhost' with grant option;
-- 回收权限
revoke update on sport.* from 'normal'@'localhost';
-- 被授权 用户拥有 授权的权限(在授权时候后面加上)
with grant option;
-- 创建用户
create user 用户名@ip地址 identified by '密码';
drop user 用户名@IP地址;
create trigger
trigger_name
trigger_time
trigger_event
ON tbl_name
for each row
trigger_stmt
create procedure 函数名(in 参数名 参数类型)
select * from 表名 where 字段名 = 参数名;
call 函数名(参数);
drop procedure 函数名;