前言
开发者必备Mysql常用命令,涵盖了数据定义语句、数据操纵语句及数据控制语句,基于Mysql5.7。
数据定义语句(DDL)
数据库操作

表操作
1
| create table emp(ename varchar(10),hiredate date,sal decimal(10,2),deptno int(2))
|
1
| create table dept(deptno int(2),deptname varchar(10))
|


1
| show create table emp \G
|

1
| alter table emp modify ename varchar(20)
|
1
| alter table emp add column age int(3)
|
1
| alter table emp drop column age
|
1
| alter table emp change age age1 int(4)
|
1
| alter table emp rename emp1
|
数据操纵语句(DML)
插入记录
1
| insert into emp (ename,hiredate,sal,deptno) values ('zhangsan','2018-01-01','2000',1)
|
1
| insert into emp values ('lisi','2018-01-01','2000',1)
|
1
| insert into dept values(1,'dept1'),(2,'dept2')
|
修改记录
1
| update emp set sal='4000',deptno=2 where ename='zhangsan'
|
删除记录
1
| delete from emp where ename='zhangsan'
|
查询记录
1
| select distinct deptno from emp
|
1
| select * from emp where deptno=1 and sal<3000
|
1
| select * from emp order by deptno desc limit 2
|
1
| select * from emp order by deptno desc limit 0,10
|
1
| select deptno,count(1) from emp group by deptno having count(1) > 1
|
1
| select * from emp e left join dept d on e.deptno=d.deptno
|
1
| select * from emp where deptno in (select deptno from dept)
|
1
| select deptno from emp union select deptno from dept
|
数据控制语句(DCL)
权限相关
- 授予操作权限(将test数据库中所有表的select和insert权限授予test用户):
1
| grant select,insert on test.* to 'test'@'localhost' identified by '123'
|
1
| show grants for 'test'@'localhost'
|

1
| revoke insert on test.* from 'test'@'localhost'
|

1
| grant all privileges on *.* to 'test'@'localhost'
|
1
| grant all privileges on *.* to 'test'@'localhost' with grant option
|
- 授予SUPER PROCESS FILE权限(系统权限不能指定数据库):
1
| grant super,process,file on *.* to 'test'@'localhost'
|
1
| grant usage on *.* to 'test'@'localhost'
|
账号相关
1
| drop user 'test'@'localhost'
|
1
| set password = password('123')
|
1
| set password for 'test'@'localhost' = password('123')
|
其他
字符集相关
1
| show variables like 'character%'
|

1
| create database test2 character set utf8
|

时区相关
- 查看当前时区(UTC为世界统一时间,中国为UTC+8):
1
| show variables like "%time_zone%"
|

- 修改mysql全局时区为北京时间,即我们所在的东8区:
1
| set global time_zone = '+8:00';
|

最后