1.DDL語句
create database xxxx
創建名爲xxxx的數據庫
use xxx
選擇數據庫xxx
show tables
查看數據庫裏的所有數據表
drop database dbname
刪除數據庫
create table tablename(
name1 type1 constraints,
name2 type2 constraints,
name3 type3 constraints,
....
namex typex contraints
) 創建一張表, name表示列名, type表示數據類型, contraints表示約束條件
desc tablename
查看錶的定義
show create table xxx
全面查看xxx表的定義信息
drop table xxx
刪除表xxx
alter table xxx modify stu_name varchar(20)
修改xxx表的stu_name屬性爲varchar(20)
alter table xxx drop stu_name
刪除一個表的一個字段,刪除stu_name這一列
alter table xxx change age age1 int(10)
字段改名, 吧age字段改爲age1 類型爲int(10)
alter table xxx add stu_age int(10) after stu_name
在stu_name後插入一個字段(默認的插入都是從最後)
alter table xxx modify stu_age int(10) first
修改age字段位置放到最前邊
alter table xxx rename xxxx
把xxx表改名爲xxxx
2.DML語句
1.插入操作
insert into xxx (name, age) values ('zhangsan', 18)
插入語句
insert into xxx values (1, 'zhangsan', 18, ....)
若所有列都添加那麼就可以不寫
insert into xxx (name, age) values ('zhangsan', 18), ('lisi', 19), ('wangwu', 20)
可一次插入多條數據
2. 更新操作
update xxxx set name='liuliu', age=12 where id=1
更新語句
update xx1 a, xx2 b set a.name='aaa', b.name='bbb' where xxxxxxxxx
同時更新兩個表的name
3.刪除操作
dalete from xxx where name='zhangsan'
刪除xxx表中name是張三的記錄
delete a, b from xx1 a, xx2 b where a.name=b.name
同時刪除2個表中的數據
4. 查詢操作
select * from xxx
查詢表中所有數據
select * form xxx where id = 1
帶條件查詢
select * from xxx where id != 1
不等於查詢, 也可以有 <,>, <=, >=等符號
select * from xxx where name like '%nike%'
模糊查詢
select distinct age from xxx
假如表中很多人年齡相同,我們要查都有幾歲的人,就用distinct去重
排序查詢
select * from xxx order by id
按照id排序(默認從小到大)
select * from xxx order by id desc
反轉排序
select * from xxx order by id, score
如果id相同, 按照score排序
select * from xxx limit 3
查詢顯示三條數據
select * from xxx limit 1, 3
從第二條數據開始顯示三條數據
聚合查詢
select count(*) from xxx
查詢總數
select class, count(1) from xxx group by class
查詢每個班的數, (沒錯就是寫作count(1) )
select class, count(1) from xxx group by class with rollup
更細緻的查詢(包括null)
select class, count(1) from xxx group by class having count(1) > 1
查詢總數大於一的結果
select sum(score), max(score), min(score), avg(score) from xxx
查詢總分, 最大,最小和平均分
連接查詢
select ename, departname from emp, dept where emp.deptno = dept.deptno
查詢emp和dept兩個表中no相同的記錄
select a.ename, b.departname from emp a, dept b where a.deptno=b.deptno
就是起了別名, 用a, b 替代emp和dept
內連接
select * from xx1 inner join xx2 where xx1.id = xx2.id
依舊查詢兩個表, 其中inner可以省去可以只寫join
外連接(左連接)
select * from xx1 left join xx2 where xx1.id = xx2.id
查詢兩個表, 左鏈接以左邊的表爲主, 去關聯右邊
外連接(右連接)
select * from xx1 right join xx2 where xx1.id = xx2.id
這次是以右邊的表爲主
有關連接詳細解釋戳這裏mysql查詢之連接查詢
子查詢
select * from xxx where xxid in (select id in xxx)
查詢某個東西是否在一個集合裏邊
select * from xxx where xxid = (select id in xxx limit 1)
如果查詢結果唯一,可以用等於連接
聯合查詢
select name from xxx union select name from yyy
這個是去重後的姓名
select name from xxx union all select name from yyy
這個是不去重的, 有多少就是多少
3.DCL語句
grant select, insert on dbname.* to 'zhangsan'@'localhost' identified by '123456'
創建一個叫做zhangsan的用戶, 密碼是123456, 並賦予zhangsan查詢和插入權限
revoke insert on dbname.* from 'zhangsan'@'localhost'
剝奪zhangsan的插入權限