浓缩就是精华
1.创建表
create table 库名.表名(字段名1 字段类型1(字符长度 1),字段名2 字段类型2(字符长度 2));

2.为一个表增加字段
alter table 库名.表名 add [column] 字段名 数据类型(数据长度);

3.删除表
drop table 库名.表名;

4.修改表名
alter table 库名.旧表名 rename [to] 新表名;
等效于:rename table 库名.旧表名 to 新表名;

5.为表添加主键约束
alter table 库名.表名 add primary key(字段名);

6.删除主键
alter table 库名.表名 drop primary key;

7.为表添加非空约束
alter table 库名.表名 modify 字段名 数据类型(数据长度) not null;

8.修改表中某个字段的名称
alter table 库名.表名 change 原字段名 新字段名 数据类型(数据长度);

9.为表添加一个字段
alter table 库名.表名 add [column] 字段名 字段类型(字段长度);

10.修改表中指定字段的数据类型和数据长度
alter table 库名.表名 modify [column] 字段名 新数据类型(新数据长度)

11.删除表中指定的字段
alter table 库名.表名 drop [column] 字段名;

12.修改表将表中指定字段设置为外键
alter table 库名.表名 add constraint 外键约束名称 foreign key (本表设置为外键字段的名称) references 库名.被引用字段的表名称(被引用字段的名称);

13.删除表中指定字段的外键约束
步骤一:删除外键约束
alter table 库名.表名 drop foreign 外键约束名称;
步骤二:删除外键索引
drop index 表名_字段名_约束类型 on 库名.表名;

14.创建表设置复合主键
create table 库名.表名 (字段1 数据类型1(数据长度1),字段2 数据类型2(数据长度2),字段2 数据类型2(数据长度2) primary key(字段1,字段2));

15.只复制表结构,不复制数据
create table 库名.表名1 like 库名.表名2;

16.同时复制表结构和数据
create table 库名.表名1 [as] select * from 库名.表名2;

17.select语句
(1)select 字段列表
(2)from 数据表
(3)[where 条件]
(4)[group by 分组字段]
(5)[having 条件]
(6)[order by 排序字段 [asc | desc]]
(7)[limit [start],length]

18.多表查询
select 别名1.字段名1,别名2.字段名2
from 库名.表名1 [as] 别名1,库名.表名2 [as] 别名2,
where 别名1.字段名3=别名2.字段名3 and 其他条件;

19.子查询出现在from子句中
select 查询列表1
from (select 查询列表2 from 库名.表名 where 条件表达式);

20.子查询出现在where子句中
select 字段1
from 库名.表名
where 字段1 关系运算符( select 字段1 from 库名.表名 where 字段2 关系运算符 字段值);

21.子查询出现在having子句中
select 查询列表1
from 库名.表名
having 字段名 比较运算符(select 字段名 from 库名.表名 where 条件);

22.子查询操作符all
select 字段1
from 库名.表名
where 字段1 关系运算符 all(select 字段1 from 库名.表名 where 字段2 关系运算符 字段值);

22.子查询操作符any
select 字段1
from 库名.表名
where 字段1 关系运算符 any(select 字段1 from 库名.表名 where 字段2 关系运算符 字段值);

23.内连接
select 别名1.字段名1,别名2.字段名2
from 库名.表名1 [as] 别名1
inner join 库名.表名 [as] 别名2
on 别名1.字段名3=别名2.字段名3;

24.左外连接
select 别名1.字段名1,别名2.字段名2
from 库名.表名1 [as] 别名1
left join 库名.表名2 [as] 别名2
on 别名1.字段名3=别名2.字段名3;

25.右外连接
select 别名1.字段名1,别名2.字段名2
from 库名.表名1 [as] 别名1
right join 库名.表名2 [as] 别名2
on 别名1.字段名3=别名2.字段名3;

26.联合查询
select 别名1.字段名1,别名2.字段名2
from 库名.表名1 [as] 别名1
right join 库名.表名2 [as] 别名2
on 别名1.字段名3=别名2.字段名3;
where 字段名4=字段值4;
union
select 别名1.字段名1,别名2.字段名2
from 库名.表名1 [as] 别名1
left join 库名.表名2 [as] 别名2
on 别名1.字段名3=别名2.字段名3;
where 字段名4=字段值4;

27.往表中插入数据
insert into 库名.表名(字段名1,字段名2) values(字段值1,字段值2)

28.修改表中数据
update 库名.表名
set 字段名1=新字段值1
where 字段名2=字段值2;

29.删除表中的数据
delete from 库名.表名 where 字段名1=字段值1;
truncate 库名.表名;

30.用户创建
步骤一:创建用户
insert into mysql.user(host,user,password) values("IP地址","用户名",password(密码));
步骤二:为新用户授权
grant all privileges on . to 用户名@IP地址 identified by "密码";
步骤三:使授权生效
flush privileges;

31.授权
grant 权限列表 on 库名.表名 to 用户名@主机IP identified by "密码";

32.查看用户权限
show grants for 用户名@主机IP;
(1)查看当前登录用户权限
show grants;
(2)查看本地用户
show grants for 用户名@localhost;
(3)查看远程可以在任意一台主机上访问数据库的用户的权限
show grants for 用户名@"%";
(4)查看在远程指定 IP 的主机上访问数据的用户的权限
show grants for 用户名@"指定IP地址";

33.用户权限回收
revoke 权限 on 数据库.数据库对象 from 用户名@"数据库所在主机 IP";
步骤一:查看将要被回收权限的用户的权限
show grants for 用户名@数据库所在主机 IP;
步骤二:回收权限
revoke all privileges on . from 用户名@数据库所在主机 IP;
步骤三、刷新授权表,使回收权限生效
flush privileges;

34.用户删除
drop user 用户名@主机IP地址;
(1)删除主机 IP 为百分号%的远程用户
drop user 用户名;
(2)删除指定 IP 地址的远程用户
drop user 用户名@"具体指定IP地址";
(3))删除本地用户
drop user 用户名@localhost;

35.修改用户密码
步骤一:修改密码
update mysql.user
步骤二:使新密码生效
set password=password("新密码")
where user="用户名" and host="IP地址";