Developer necessary Mysql common commands, covering the data definition statements, data manipulation statements and control statements data, based Mysql5.7.
Data Definition Language (DDL)
Database operations
- Log database:
mysql -uroot -proot
- Create a database:
create database test
- View all databases:
show databases
- Select the database and use:
use test
- See all the data table:
show tables
- Delete the database:
drop database test
Operating Table
- Create a table:
create table emp(ename varchar(10),hiredate date,sal decimal(10,2),deptno int(2))
create table dept(deptno int(2),deptname varchar(10))
- View the table definition:
desc emp
- View the table definition (detail):
show create table emp \G
- Delete the table:
drop table emp
- Modify table field:
alter table emp modify ename varchar(20)
- Adding table fields:
alter table emp add column age int(3)
- Delete the table fields:
alter table emp drop column age
- Fields renamed;
alter table emp change age age1 int(4)
- Modify the table name:
alter table emp rename emp1
Data manipulation statements (DML)
Insert Record
- Specify the name inserted:
insert into emp (ename,hiredate,sal,deptno) values ('zhangsan','2018-01-01','2000',1)
- Do not specify a name inserted:
insert into emp values ('lisi','2018-01-01','2000',1)
- Bulk insert data:
insert into dept values(1,'dept1'),(2,'dept2')
Modify records
update emp set sal='4000',deptno=2 where ename='zhangsan'
Delete Record
delete from emp where ename='zhangsan'
inquiry record
- Query all records:
select * from emp
- The query does not duplicate records:
select distinct deptno from emp
- Conditions inquiry:
select * from emp where deptno=1 and sal<3000
- Sort and restrictions:
select * from emp order by deptno desc limit 2
- Paging query (query from 0 to start recording 10):
select * from emp order by deptno desc limit 0,10
- Polymerization (the number of sectors is greater than the sector inquiry number 1):
select deptno,count(1) from emp group by deptno having count(1) > 1
- Join query:
select * from emp e left join dept d on e.deptno=d.deptno
- Subquery:
select * from emp where deptno in (select deptno from dept)
- Joint record:
select deptno from emp union select deptno from dept
Data control statements (DCL)
The relevant authority
- Granted operating authority (to grant test user test database select and insert permissions to all tables):
grant select,insert on test.* to 'test'@'localhost' identified by '123'
- View account permissions:
show grants for 'test'@'localhost'
- Recovery of operating authority:
revoke insert on test.* from 'test'@'localhost'
- All permissions granted to all databases:
grant all privileges on *.* to 'test'@'localhost'
- All permissions granted to all databases (including the grant):
grant all privileges on *.* to 'test'@'localhost' with grant option
- SUPER PROCESS FILE grant permission (the database system privileges can not be specified):
grant super,process,file on *.* to 'test'@'localhost'
- Only granted permission to log on:
grant usage on *.* to 'test'@'localhost'
Account Related
- Delete Account:
drop user 'test'@'localhost'
- Change the password:
set password = password('123')
- Administrators modified by someone else Password:
set password for 'test'@'localhost' = password('123')
other
Character Sets Related
- View character sets:
show variables like 'character%'
- Specify the character set when creating a database:
create database test2 character set utf8
The time zone
- View the current time zone (UTC unified time for the world, China is UTC + 8):
show variables like "%time_zone%"
- When you modify the global zone is GMT mysql, that we live East Area 8:
set global time_zone = '+8:00';
- Modify the current conversation area:
set time_zone = '+8:00'http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/blog/refer_screen_50.png
- Effective immediately:
flush privileges