DML(Data Manipulation Language)数据操作语⾔,以INSERT、UPDATE、DELETE三种指
令为核⼼,分别代表插⼊、更新与删除,是必须要掌握的指令,DML和SQL中的select熟
称CRUD(增删改查)。
⽂中涉及到的语法⽤[]包含的内容属于可选项,下⾯做详细说明。插⼊操作
插⼊单⾏2种⽅式
⽅式1
insert into 表名[(字段,字段)] values (值,值);
说明:
值和字段需要⼀⼀对应
如果是字符型或⽇期类型,值需要⽤单引号引起来;如果是数值类型,不需要⽤
单引号
字段和值的个数必须⼀致,位置对应
字段如果不能为空,则必须插⼊值
可以为空的字段可以不⽤插⼊值,但需要注意:字段和值都不写;或字段写上,
值⽤null代替
表名后⾯的字段可以省略不写,此时表⽰所有字段,顺序和表中字段顺序⼀致。
⽅式2
insert into 表名 set 字段 = 值,字段 = 值;
⽅式2不常见,建议使⽤⽅式1
批量插⼊2种⽅式
⽅式1
insert into 表名 [(字段,字段)] values (值,值),(值,值),(值,值);
⽅式2
insert into 表 [(字段,字段)]
数据来源select语句;
说明:数据来源select语句可以有很多种写法,需要注意:select返回的结果和插⼊数据
的字段数量、顺序、类型需要⼀致。
关于select的写法后⾯⽂章会详细介绍。
如:
-- 删除test1
drop table if exists test1;
-- 创建test1
create table test1(a int,b int);
-- 删除test2
drop table if exists test2;
-- 创建test2
create table test2(c1 int,c2 int,c3 int);
-- 向test2中插⼊数据
insert into test2 values (100,101,102),(200,201,202),(300,301,302),
(400,401,402);
-- 向test1中插⼊数据
insert into test1 (a,b) select 1,1 union all select 2,2 union all
select 2,2;
-- 向test1插⼊数据,数据来源于test2表
insert into test1 (a,b) select c2,c3 from test2 where c1>=200;
select * from test1;
mysql> select * from test1;
+------+------+
| a | b |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 2 | 2 |
| 201 | 202 |
| 301 | 302 |
| 401 | 402 |
mysql> select * from test2;
+------+------+------+
| c1 | c2 | c3 |+------+------+------+
| 100 | 101 | 102 |
| 200 | 201 | 202 |
| 300 | 301 | 302 |
| 400 | 401 | 402 |
+------+------+------+
4 rows in set (0.00 sec)
数据更新
单表更新
语法:
update 表名 [[as] 别名] set [别名.]字段 = 值,[别名.]字段 = 值 [where条件];
有些表名可能名称⽐较长,为了⽅便操作,可以给这个表名起个简单的别名,更
⽅便操作⼀些。
如果⽆别名的时候,表名就是别名。
示例:
mysql> update test1 t set t.a = 2;
Query OK, 4 rows affected (0.00 sec)
Rows matched: 6 Changed: 4 Warnings: 0
mysql> update test1 as t set t.a = 3;
Query OK, 6 rows affected (0.00 sec)
Rows matched: 6 Changed: 6 Warnings: 0
mysql> update test1 set a = 1,b=2;
Query OK, 6 rows affected (0.00 sec)
Rows matched: 6 Changed: 6 Warnings: 0
多表更新
可以同时更新多个表中的数据语法:
update 表1 [[as] 别名1],表名2 [[as] 别名2]
set [别名.]字段 = 值,[别名.]字段 = 值
[where条件]
示例:
-- ⽆别名⽅式
update test1,test2 set test1.a = 2 ,test1.b = 2, test2.c1 = 10;
-- ⽆别名⽅式
update test1,test2 set test1.a = 2 ,test1.b = 2, test2.c1 = 10 where
test1.a = test2.c1;
-- 别名⽅式更新
update test1 t1,test2 t2 set t1.a = 2 ,t1.b = 2, t2.c1 = 10 where t1.a
= t2.c1;
-- 别名的⽅式更新多个表的多个字段
update test1 as t1,test2 t2 set t1.a = 2 ,t1.b = 2, t2.c1 = 10 where
t1.a = t2.c1;
使⽤建议
建议采⽤单表⽅式更新,⽅便维护。
删除数据操作
使⽤delete删除
delete单表删除
delete [别名] from 表名 [[as] 别名] [where条件];
注意:
如果⽆别名的时候,表名就是别名
如果有别名,delete后⾯必须写别名
如果没有别名,delete后⾯的别名可以省略不写。示例
-- 删除test1表所有记录
delete from test1;
-- 删除test1表所有记录
delete test1 from test1;
-- 有别名的⽅式,删除test1表所有记录
delete t1 from test1 t1;
-- 有别名的⽅式删除满⾜条件的记录
delete t1 from test1 t1 where t1.a>100;
上⾯的4种写法,⼤家可以认真看⼀下。
多表删除
可以同时删除多个表中的记录,语法如下:
delete [别名1,别名2] from 表1 [[as] 别名1],表2 [[as] 别名2] [where条件];
说明:
别名可以省略不写,但是需要在delete后⾯跟上表名,多个表名之间⽤逗号隔
开。
示例1
delete t1 from test1 t1,test2 t2 where t1.a=t2.c2;
删除test1表中的记录,条件是这些记录的字段a在test.c2中存在的记录
看⼀下运⾏效果:
-- 删除test1
drop table if exists test1;
-- 创建test1
create table test1(a int,b int);
-- 删除test2
drop table if exists test2;
-- 创建test2
create table test2(c1 int,c2 int,c3 int);
-- 向test2中插⼊数据insert into test2 values (100,101,102),(200,201,202),(300,301,302),
(400,401,402);
-- 向test1中插⼊数据
insert into test1 (a,b) select 1,1 union all select 2,2 union all
select 2,2;
-- 向test1插⼊数据,数据来源于test2表
insert into test1 (a,b) select c2,c3 from test2 where c1>=200;
mysql> select * from test1;
+------+------+
| a | b |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 2 | 2 |
| 201 | 202 |
| 301 | 302 |
| 401 | 402 |
mysql> select * from test2;
+------+------+------+
| c1 | c2 | c3 |
+------+------+------+
| 100 | 101 | 102 |
| 200 | 201 | 202 |
| 300 | 301 | 302 |
| 400 | 401 | 402 |
+------+------+------+
4 rows in set (0.00 sec)
mysql> delete t1 from test1 t1,test2 t2 where t1.a=t2.c2;
Query OK, 3 rows affected (0.00 sec)
mysql> select * from test1;
+------+------+
| a | b |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 2 | 2 |+------+------+
3 rows in set (0.00 sec)
从上⾯的输出中可以看到test1表中3条记录被删除了。
示例2
delete t2,t1 from test1 t1,test2 t2 where t1.a=t2.c2;
同时对2个表进⾏删除,条件是test.a=test.c2的记录
看⼀下运⾏效果:
-- 删除test1
drop table if exists test1;
-- 创建test1
create table test1(a int,b int);
-- 删除test2
drop table if exists test2;
-- 创建test2
create table test2(c1 int,c2 int,c3 int);
-- 向test2中插⼊数据
insert into test2 values (100,101,102),(200,201,202),(300,301,302),
(400,401,402);
-- 向test1中插⼊数据
insert into test1 (a,b) select 1,1 union all select 2,2 union all
select 2,2;
-- 向test1插⼊数据,数据来源于test2表
insert into test1 (a,b) select c2,c3 from test2 where c1>=200;
mysql> select * from test1;
+------+------+
| a | b |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 2 | 2 |
| 201 | 202 |
| 301 | 302 || 401 | 402 |
+------+------+
6 rows in set (0.00 sec)
mysql> select * from test2;
+------+------+------+
| c1 | c2 | c3 |
+------+------+------+
| 100 | 101 | 102 |
| 200 | 201 | 202 |
| 300 | 301 | 302 |
| 400 | 401 | 402 |
+------+------+------+
4 rows in set (0.00 sec)
mysql> delete t2,t1 from test1 t1,test2 t2 where t1.a=t2.c2;
Query OK, 6 rows affected (0.00 sec)
mysql> select * from test1;
+------+------+
| a | b |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 2 | 2 |
+------+------+
3 rows in set (0.00 sec)
mysql> select * from test2;
+------+------+------+
| c1 | c2 | c3 |
+------+------+------+
| 100 | 101 | 102 |
+------+------+------+
1 row in set (0.00 sec)
从输出中可以看出test1和test2总计6条记录被删除了。平时我们⽤的⽐较多的⽅式是delete from 表名这种语法,上⾯我们介绍了再delete后
⾯跟上表名的⽤法,⼤家可以在回顾⼀下,加深记忆。