DML(Data Manipulation Language)語句:數據操縱語句,用於添加、刪除、更新和查詢數據庫記錄,並檢查數據完整性。經常使用的語句關鍵字主要包括 insert、delete、update 和 select 等。數據庫
經常使用約束類型函數
約束類型 | 解釋 |
---|---|
not null | 非空約束,設定的內容在添加數據的時候不能是 null 的 |
unique | 惟一約束,值是惟一的 |
primary key | 主鍵約束,非空惟一 |
auto_increment | 主鍵自增約束,主鍵爲數值型 |
foreign key | 外鍵約束,B 表使用 A 表的主鍵 |
經常使用 where 查詢條件code
where 條件 | 解釋 |
---|---|
> < <= >= <> | 大於、小於、大於(小於)等於、不等於 |
between...and... | 在某一區間,閉區間,效果同 and |
in(set) | 在 in 列表中的值,如 in(100, 200),效果同 or |
like 通配符 | 模糊查詢:% 匹配多個字符,如 first_name like "a%";_ 匹配一個字符,如 first_name like "a_"。 |
is null | 爲空 |
is not null | 不爲空 |
and | 同時成立 |
or | 任一成立 |
not | 不成立,如 where not(salary > 100); |
經常使用聚合查詢條件排序
統計函數 | 解釋 |
---|---|
count() | 求總數 |
avg() | 求平均數 |
sum() | 求和 |
max() | 求最大值 |
min() | 求最小值 |
含可空字段、非空可是含有默認值的字段、自增字段,能夠不用在 insert 後的字段列表裏面出現,values 後面只寫對應字段名稱的 value,這些沒有寫的字段能夠自動設置爲 NULL、默認值、自增的下一個數字。three
1)插入單條記錄ip
# 插入單條記錄,插入到指定列 # insert into 表名 (列名1, 列名2, ...) values (值1, 值2, ...); > insert into student (name, age, class) values ("xiao bai", 18, "one"); Query OK, 1 row affected (0.09 sec)
# 插入單條記錄,插入到全部列 # insert into 表名 values (值1, 值2, ...); > insert into student values (2, "li si", 20, 90, "two", "aaaaaaaa"); Query OK, 1 row affected (0.04 sec)
2)插入多條記錄rem
# 插入多條記錄,插入到指定列 # insert into 表名 (列名1, 列名2, ...) values (值1, 值2, ...), (值1, 值2, ...), ...; > insert into student (name, age, class) values ("zhang san", 18, "one"), ("li si", 19, "one"); Query OK, 2 rows affected (0.03 sec) Records: 2 Duplicates: 0 Warnings: 0
# 插入多條記錄,插入到全部列 # insert into 表名 values (值1, 值2, ...), (值1, 值2, ...), ...; > insert into student values (5, "zhang san", 20, 90, "two", "aaaaaaaa"), (6, "li si", 21, 92, "two", "bbbbbbbb"); Query OK, 2 rows affected (0.08 sec) Records: 2 Duplicates: 0 Warnings: 0
不論時單表仍是多表,不加 where 條件將會把表的全部記錄刪除。it
1)刪除單個表中的記錄io
# 刪除單個表中的記錄,刪除全部表記錄 # delete from 表名; > delete from student; Query OK, 5 rows affected (0.08 sec)
# 刪除單個表中的記錄,刪除知足條件的表記錄 # delete from 表名 where 條件; > delete from student where no = 5; Query OK, 1 row affected (0.11 sec)
# 刪除單個表中的記錄,刪除全部表記錄 # truncate table 表名; > truncate table student; Query OK, 0 rows affected (0.08 sec)
2)刪除多個表中的記錄table
# 刪除多個表中的記錄,若是 from 後面的表用別名,delete 後面也要用相應的別名 # delete 表名1, 表名2, ... from 表名1, 表名2, ... [where 條件]; > delete a, b from emp a, dept b where a.dept.no = b.deptno and a.deptno = 3; Query OK, 2 rows affected (0.04 sec)
1)更新單個表中的記錄
# 更新單個表中的記錄,全部指定列記錄 # update 表名 set 字段名 = 值, 字段名 = 值; > update student set age = 18; Query OK, 4 rows affected (0.02 sec) Rows matched: 6 Changed: 4 Warnings: 0
# 更新單個表中的記錄,知足條件的指定列記錄 # update 表名 set 字段名 = 值, 字段名 = 值 where 條件; > update student set age = 19 where class = "two"; Query OK, 3 rows affected (0.02 sec) Rows matched: 3 Changed: 3 Warnings: 0
2)更新多個表中的記錄
# 更新多個表中的記錄 # update 表名1, 表名2, ... set 表名1.字段名1 = 值, 表名1.字段名2 = 值, 表名2.字段名1 = 值, 表名2.字段名2 = 值, ... [where 條件]; > update emp a, dept b set a.sal = a.sal * b.deptno, b.deptname = a.ename where a.deptno = b.deptno; Query OK, 3 rows affected (0.04 sec) Rows matched: 5 Changed: 3 Warnings: 0
1)簡單查詢
# 查詢單表記錄,查詢全部數據 # select * from 表名; > select * from zhangwu; +----+-----------------+-------+ | id | name | money | +----+-----------------+-------+ | 1 | chifanzhichu | 247 | | 2 | gongzishouru | 12345 | | 3 | fuzhuangzhichu | 1000 | | 4 | chifanzhichu | 325 | | 5 | gupiaoshouru | 8000 | | 6 | damajiangzhichu | 8000 | | 7 | NULL | 5000 | +----+-----------------+-------+ 7 rows in set (0.00 sec)
# 查詢單表記錄,查詢指定字段記錄 # select 字段1, 字段2, ... from 表名; > select id, name from zhangwu; +----+-----------------+ | id | name | +----+-----------------+ | 1 | chifanzhichu | | 2 | gongzishouru | | 3 | fuzhuangzhichu | | 4 | chifanzhichu | | 5 | gupiaoshouru | | 6 | damajiangzhichu | | 7 | NULL | +----+-----------------+ 7 rows in set (0.00 sec)
2)去重查詢
# 查詢單表記錄,去除重複記錄 # select distinct 字段名 from 表名; > select distinct money from zhangwu; +-------+ | money | +-------+ | 247 | | 12345 | | 1000 | | 325 | | 8000 | | 5000 | +-------+ 6 rows in set (0.00 sec)
3)條件查詢
# 查詢單表記錄,查詢全部知足條件的記錄 # select * from 表名 where 條件; > select * from zhangwu where money >= 8000; +----+-----------------+-------+ | id | name | money | +----+-----------------+-------+ | 2 | gongzishouru | 12345 | | 5 | gupiaoshouru | 8000 | | 6 | damajiangzhichu | 8000 | +----+-----------------+-------+ 3 rows in set (0.00 sec)
4)別名查詢
# 查詢單表記錄,表別名查詢 # select 字段名 from 表名 [as] 別名; > select name from zhangwu as zw; +-----------------+ | name | +-----------------+ | chifanzhichu | | gongzishouru | | fuzhuangzhichu | | chifanzhichu | | gupiaoshouru | | damajiangzhichu | | NULL | +-----------------+ 7 rows in set (0.00 sec)
# 查詢單表記錄,列別名查詢 # select 字段名 [as] 別名 from 表名; > select name as na from zhangwu; +-----------------+ | na | +-----------------+ | chifanzhichu | | gongzishouru | | fuzhuangzhichu | | chifanzhichu | | gupiaoshouru | | damajiangzhichu | | NULL | +-----------------+ 7 rows in set (0.00 sec)
5)列運算查詢
# 查詢單表記錄,列運算查詢 # select 字段名(列運算) from 表名; > select money + 10000 from zhangwu; +---------------+ | money + 10000 | +---------------+ | 10247 | | 22345 | | 11000 | | 10325 | | 18000 | | 18000 | | 15000 | +---------------+ 7 rows in set (0.00 sec)
6)排序查詢
# 查詢單表記錄,單列排序查詢 # select 字段名 from 表名 [where 條件] order by 列名 排序方式; > select * from zhangwu where money > 1000 order by money asc; +----+-----------------+-------+ | id | name | money | +----+-----------------+-------+ | 7 | NULL | 5000 | | 5 | gupiaoshouru | 8000 | | 6 | damajiangzhichu | 8000 | | 2 | gongzishouru | 12345 | +----+-----------------+-------+ 4 rows in set (0.00 sec)
# 查詢單表記錄,多列排序查詢 # select 字段名 from 表名 [where 條件] order by 列名 排序方式, 列名 排序方式, ...; > select * from zhangwu where money > 1000 order by money asc, id desc; +----+-----------------+-------+ | id | name | money | +----+-----------------+-------+ | 7 | NULL | 5000 | | 6 | damajiangzhichu | 8000 | | 5 | gupiaoshouru | 8000 | | 2 | gongzishouru | 12345 | +----+-----------------+-------+ 4 rows in set (0.00 sec)
7)限制查詢
# 查詢單表記錄,限制查詢 # select 字段名 from 表名 [where 條件] [order by 列名 排序方式] limit [起始偏移量] 獲取的行數 > select * from zhangwu where money > 1000 order by money asc, id desc limit 3; +----+-----------------+-------+ | id | name | money | +----+-----------------+-------+ | 7 | NULL | 5000 | | 6 | damajiangzhichu | 8000 | | 5 | gupiaoshouru | 8000 | +----+-----------------+-------+ 3 rows in set (0.01 sec)
8)聚合查詢
# 查詢單表記錄,求總數、統計 # select count(字段名) from 表名; > select count(id) from zhangwu; +-----------+ | count(id) | +-----------+ | 7 | +-----------+ 1 row in set (0.00 sec)
# 查詢單表記錄,求和 # select sum(字段名) from 表名; > select sum(money) from zhangwu; +------------+ | sum(money) | +------------+ | 34917 | +------------+ 1 row in set (0.00 sec)
# 查詢單表記錄,求平均數 # select avg(字段名) from 表名; > select avg(money) from zhangwu; +-------------------+ | avg(money) | +-------------------+ | 4988.142857142857 | +-------------------+ 1 row in set (0.00 sec)
# 查詢單表記錄,求最大值 # select max(字段名) from 表名; > select max(money) from zhangwu; +------------+ | max(money) | +------------+ | 12345 | +------------+ 1 row in set (0.01 sec)
# 查詢單表記錄,求最小值 # select min(字段名) from 表名; > select min(money) from zhangwu; +------------+ | min(money) | +------------+ | 247 | +------------+ 1 row in set (0.00 sec)
9)分組查詢
# 查詢單表記錄,分組查詢 # select 字段名 from 表名 [where 條件] group by 分組字段名 [order by 排序字段名 排序方式]; > select class, count(no), avg(score) from student group by class; +-------+-----------+------------+ | class | count(no) | avg(score) | +-------+-----------+------------+ | one | 3 | 81.6667 | | two | 2 | 77.5000 | | three | 2 | 83.0000 | +-------+-----------+------------+ 3 rows in set (0.00 sec)
# 查詢單表記錄,分組後過濾查詢 # select 字段名 from 表名 [where 條件] group by 分組字段名 having 分組後的過濾條件 [order by 排序字段名 排序方式]; > select class, count(no), avg(score) from student group by class having avg(score) >= 80; +-------+-----------+------------+ | class | count(no) | avg(score) | +-------+-----------+------------+ | one | 3 | 81.6667 | | three | 2 | 83.0000 | +-------+-----------+------------+ 2 rows in set (0.00 sec)
# 查詢單表記錄,分組後彙總查詢,對分類後的結果進行再彙總 # select 字段名 from 表名 [where 條件] group by 分組字段名 with rollup [order by 排序字段名 排序方式]; > select class, count(no), avg(score) from student group by class with rollup; +-------+-----------+------------+ | class | count(no) | avg(score) | +-------+-----------+------------+ | one | 3 | 81.6667 | | three | 2 | 77.5000 | | two | 2 | 83.0000 | | NULL | 7 | 242.1667 | +-------+-----------+------------+ 4 rows in set (0.00 sec)
10)子查詢
where 子查詢通常返回單行單列、單行多列、多行單列(in、any、all)的數據,from 子查詢通常返回多行多列的數據。
where 子查詢
# 查詢單表記錄,where 單行單列子查詢 # select 字段名 from 表名 where 條件(子查詢語句); > select * from student where score > (select score from student where name = "xiao bai" ) order by score asc; +----+-----------+------+-------+-------+----------+ | no | name | age | score | class | address | +----+-----------+------+-------+-------+----------+ | 1 | xiao hei | 18 | 80 | one | NULL | | 6 | li si | 19 | 80 | two | bbbbbbbb | | 3 | zhang san | 18 | 95 | one | NULL | +----+-----------+------+-------+-------+----------+ 3 rows in set (0.00 sec)
# 查詢單表記錄,where 單行多列子查詢 # select 字段名 from 表名 where 條件(子查詢語句); > select * from student where (age, class) = (select age, class from student where name = "zhang san" ) order by score asc; +----+-----------+------+-------+-------+---------+ | no | name | age | score | class | address | +----+-----------+------+-------+-------+---------+ | 1 | xiao hei | 18 | 80 | one | NULL | | 3 | zhang san | 18 | 95 | one | NULL | | 4 | wang wu | 18 | 70 | one | NULL | +----+-----------+------+-------+-------+---------+ 3 rows in set (0.00 sec)
# 查詢單表記錄,where 多行單列 in 子查詢,與普通的 in 條件相似,若是子查詢記錄數惟一,還可使用 = 代替 in # select 字段名 from 表名 where 條件(in 子查詢語句); > select * from student where age in (select age from student where class = "one" ) order by age asc; +----+-----------+------+-------+-------+----------+ | no | name | age | score | class | address | +----+-----------+------+-------+-------+----------+ | 4 | wang wu | 18 | 70 | one | NULL | | 1 | xiao hei | 18 | 80 | one | NULL | | 3 | zhang san | 19 | 95 | one | NULL | | 6 | li si | 19 | 80 | two | bbbbbbbb | | 7 | xiao bai | 19 | 79 | three | NULL | | 2 | da bai | 19 | 75 | two | aaaaaaaa | +----+-----------+------+-------+-------+----------+ 6 rows in set (0.00 sec)
# 查詢單表記錄,where 多行單列 =any 子查詢,與 in 子查詢同樣 # select 字段名 from 表名 where 條件(=any 子查詢語句); > select * from student where age = any (select age from student where class = "one" ) order by age asc; +----+-----------+------+-------+-------+----------+ | no | name | age | score | class | address | +----+-----------+------+-------+-------+----------+ | 4 | wang wu | 18 | 70 | one | NULL | | 1 | xiao hei | 18 | 80 | one | NULL | | 3 | zhang san | 19 | 95 | one | NULL | | 6 | li si | 19 | 80 | two | bbbbbbbb | | 7 | xiao bai | 19 | 79 | three | NULL | | 2 | da bai | 19 | 75 | two | aaaaaaaa | +----+-----------+------+-------+-------+----------+ 6 rows in set (0.01 sec)
# 查詢單表記錄,where 多行單列 >any 子查詢,大於最小值 # select 字段名 from 表名 where 條件(>any 子查詢語句); > select * from student where age > any (select age from student where class = "one" ) order by age asc; +----+-----------+------+-------+-------+----------+ | no | name | age | score | class | address | +----+-----------+------+-------+-------+----------+ | 2 | da bai | 19 | 75 | two | aaaaaaaa | | 3 | zhang san | 19 | 95 | one | NULL | | 6 | li si | 19 | 80 | two | bbbbbbbb | | 7 | xiao bai | 19 | 79 | three | NULL | | 5 | ma liu | 20 | 78 | three | NULL | +----+-----------+------+-------+-------+----------+ 5 rows in set (0.00 sec)
# 查詢單表記錄,where 多行單列 <any 子查詢,小於最大值 # select 字段名 from 表名 where 條件(<any 子查詢語句); > select * from student where age < any (select age from student where class = "one" ) order by age asc; +----+----------+------+-------+-------+---------+ | no | name | age | score | class | address | +----+----------+------+-------+-------+---------+ | 1 | xiao hei | 18 | 80 | one | NULL | | 4 | wang wu | 18 | 70 | one | NULL | +----+----------+------+-------+-------+---------+ 2 rows in set (0.00 sec)
# 查詢單表記錄,where 多行單列 >all 子查詢,大於最大值 # select 字段名 from 表名 where 條件(>all 子查詢語句); > select * from student where age > all (select age from student where class = "one" ) order by age asc; +----+--------+------+-------+-------+---------+ | no | name | age | score | class | address | +----+--------+------+-------+-------+---------+ | 5 | ma liu | 20 | 78 | three | NULL | +----+--------+------+-------+-------+---------+ 1 row in set (0.00 sec)
# 查詢單表記錄,where 多行單列 <all 子查詢,小於最小值 # select 字段名 from 表名 where 條件(<all 子查詢語句); > select * from student where age < all (select age from student where class = "one" ) order by age asc; +----+----------+------+-------+-------+---------+ | no | name | age | score | class | address | +----+----------+------+-------+-------+---------+ | 7 | xiao bai | 17 | 79 | three | NULL | +----+----------+------+-------+-------+---------+ 1 row in set (0.00 sec)
# 查詢單表記錄,from 多行多列子查詢,通常看成一張臨時表的方式來處理 # select 字段名 from 表名1 內/外鏈接表子查詢語句; > select B.bno, B.bname, B.bano, C.aname from B, ( select ano, aname, loc from A where ano < 2 ) C where B.bano = C.ano; +-----+----------+------+-----------+ | bno | bname | bano | aname | +-----+----------+------+-----------+ | 0 | zhangsan | 0 | 研發部 | +-----+----------+------+-----------+ 1 row in set (0.00 sec)
從大致上分,錶鏈接分爲內鏈接和外鏈接,它們之間最主要的區別是,內鏈接僅選出兩張表中互相匹配的記錄,而外鏈接會選出其它不匹配的記錄。
1)多表聯合查詢
# 查詢多表記錄,多表聯合查詢,必定要有表於表之間的關聯字段 # select 字段名 from 表名1, 表名2 where 關聯條件; > select B.bno, B.bname, B.bano, A.aname from B, A where B.bano = A.ano; +-----+----------+------+-----------+ | bno | bname | bano | aname | +-----+----------+------+-----------+ | 0 | zhangsan | 0 | 研發部 | | 1 | lisi | 2 | 銷售部 | +-----+----------+------+-----------+ 2 rows in set (0.00 sec)
2)內聯查詢
# 查詢多表記錄,內鏈接查詢 # select 字段名 from 表名1 inner join 表名2 on 關聯條件; > select B.bno, B.bname, B.bano, A.aname from B inner join A on B.bano = A.ano; +-----+----------+------+-----------+ | bno | bname | bano | aname | +-----+----------+------+-----------+ | 0 | zhangsan | 0 | 研發部 | | 1 | lisi | 2 | 銷售部 | +-----+----------+------+-----------+ 2 rows in set (0.00 sec)
3)外聯查詢
# 查詢多表記錄,左外鏈接查詢,包含全部的左表中的記錄甚至是右表中沒有和它匹配的記錄 # select 字段名 from 表名1 left outer join 表名2 on 關聯條件; > select B.bno, B.bname, B.bano, A.aname from B left outer join A on B.bano = A.ano; +-----+----------+------+-----------+ | bno | bname | bano | aname | +-----+----------+------+-----------+ | 0 | zhangsan | 0 | 研發部 | | 1 | lisi | 2 | 銷售部 | | 2 | wangwu | NULL | NULL | +-----+----------+------+-----------+ 3 rows in set (0.00 sec)
# 查詢多表記錄,右外鏈接查詢,包含全部的右表中的記錄甚至是左表中沒有和它匹配的記錄 # select 字段名 from 表名1 left outer join 表名2 on 關聯條件; > select B.bno, B.bname, B.bano, A.aname from B right outer join A on B.bano = A.ano order by bno asc; +------+----------+------+-----------+ | bno | bname | bano | aname | +------+----------+------+-----------+ | NULL | NULL | NULL | 財務部 | | 0 | zhangsan | 0 | 研發部 | | 1 | lisi | 2 | 銷售部 | +------+----------+------+-----------+ 3 rows in set (0.00 sec)
# 查詢多表記錄,全外鏈接查詢,包含全部的左表和右表中的記錄甚至是沒有匹配的記錄 # MySQL 是不支持全外的鏈接的,這裏給出的寫法適合 Oracle 和 DB2 # select 字段名 from 表名1 left outer join 表名2 on 關聯條件; > select B.bno, B.bname, B.bano, A.aname from B full outer join A on B.bano = A.ano order by bno asc;
4)記錄聯合查詢
# 查詢多表記錄,記錄聯合查詢 # select 字段名 from 表名1 union | union all select 字段名 from 表名2; > select ano, aname, loc from A union all select bno, bname, bano from B; +-----+-----------+------+ | ano | aname | loc | +-----+-----------+------+ | 0 | 研發部 | aaaa | | 1 | 財務部 | bbbb | | 2 | 銷售部 | cccc | | 0 | zhangsan | 0 | | 1 | lisi | 2 | | 2 | wangwu | NULL | +-----+-----------+------+ 6 rows in set (0.00 sec)