【MySQL】DML常见操作
发布日期:2021-05-07 02:51:45 浏览次数:25 分类:精选文章

本文共 6761 字,大约阅读时间需要 22 分钟。


文章目录


DML常见操作

DML(Data Manipulation Language)数据操作语⾔,以INSERT、UPDATE、DELETE三种指 令为核⼼,分别代表插⼊、更新与删除,是必须要掌握的指令,DML和SQL中的select熟 称CRUD(增删改查)。

一、插⼊操作

1.插⼊单⾏2种⽅式

⽅式1

insert into 表名[(字段,字段)] values (值,值);

说明:

  • 值和字段需要⼀⼀对应
  • 如果是字符型或⽇期类型,值需要⽤单引号引起来;如果是数值类型,不需要⽤单引号
  • 字段和值的个数必须⼀致,位置对应
  • 字段如果不能为空,则必须插⼊值
  • 可以为空的字段可以不⽤插⼊值,但需要注意:字段和值都不写;或字段写上,值⽤null代替
  • 表名后⾯的字段可以省略不写,此时表⽰所有字段,顺序和表中字段顺序⼀致。

⽅式2

insert into 表名 set 字段 = 值,字段 = 值;
  • ⽅式2不常见,建议使⽤⽅式1


2.批量插⼊2种⽅式

⽅式1

insert into 表名 [(字段,字段)] values (值,值),(值,值),(值,值);

⽅式2

insert into 表名 [(字段,字段)] 数据来源select语句;

说明:

  • 数据来源select语句可以有很多种写法,需要注意:select返回的结果和插⼊数据的字段数量、顺序、类型需要⼀致。


3.测试

-- 删除test1drop table if exists test1;-- 创建test1create table test1(a int,b int);-- 删除test2drop table if exists test2;-- 创建test2create 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)


二、数据更新

1.单表更新

语法:

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: 0mysql> select *from test1;+------+------+| a    | b    |+------+------+|    2 |    1 ||    2 |    2 ||    2 |    2 |+------+------+3 rows in set (0.00 sec)mysql> update test1 as t set t.a = 3;Query OK, 6 rows affected (0.00 sec)Rows matched: 6 Changed: 6 Warnings: 0mysql> select *from test1;+------+------+| a    | b    |+------+------+|    3 |    1 ||    3 |    2 ||    3 |    2 |+------+------+3 rows in set (0.00 sec)mysql> update test1 set a = 1,b=2;Query OK, 6 rows affected (0.00 sec)Rows matched: 6 Changed: 6 Warnings: 0mysql> select *from test1;+------+------+| a    | b    |+------+------+|    1 |    2 ||    1 |    2 ||    1 |    2 |+------+------+3 rows in set (0.00 sec)


2.多表更新

可以同时更新多个表中的数据语法:

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 as t2 set t1.a = 2 ,t1.b = 2, t2.c1 = 10 where t1.a = t2.c1;

使⽤建议:建议采⽤单表⽅式更新,⽅便维护。


三、删除数据操作

1.使⽤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中存在的记录看⼀下运⾏效果:-- 删除test1drop table if exists test1;-- 创建test1create table test1(a int,b int);-- 删除test2drop table if exists test2;-- 创建test2create 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 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的记录看⼀下运⾏效果:-- 删除test1drop table if exists test1;-- 创建test1create table test1(a int,b int);-- 删除test2drop table if exists test2;-- 创建test2create 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后⾯跟上表名的⽤法。


2.使⽤truncate删除

语法

truncate 表名;


3.drop、truncate、delete 的区别

  • ==drop(删除表):==删除内容和定义,释放空间,简单来说就是把整个表去掉,以后要新增数据是不可能的,除⾮新增⼀个表。
    • drop语句将删除表的结构被依赖的约束(constrain),触发器(trigger),索引(index),依赖于该表的存储过程/函数将被保留,但其状态会变为:invalid。
      如果要删除表定义及其数据,请使⽤ drop table 语句。
  • ==truncate(清空表中的数据):==删除内容、释放空间但不删除定义(保留表的数据结构),与drop不同的是,只是清空表数据⽽已。
    • 注意:truncate不能删除具体⾏数据,要删就要把整个表清空了。
  • ==delete(删除表中的数据):==delete 语句⽤于删除表中的⾏。delete语句执⾏删除的过程是每次从表中删除⼀⾏,并且同时将该⾏的删除操作作为事务记录在⽇志中保存,以便进⾏进⾏回滚操作。
  • truncate与不带where的delete:只删除数据,⽽不删除表的结构(定义)
  • truncate table删除表中的所有⾏,但表结构及其列、约束、索引等保持不变。
  • 对于由foreign key约束引⽤的表,不能使⽤truncate table,⽽应使⽤不带where⼦句的delete语句。由于truncate table记录在⽇志中,所以它不能激活触发器。
  • delete语句是数据库操作语⾔(dml),这个操作会放到rollback segement中,事务提交之后才⽣效;如果有相应的trigger,执⾏的时候将被触发。truncate、drop 是数据库定义语⾔(ddl),操作⽴即⽣效,原数据不放到rollback segment中,不能回滚,操作不触发trigger。
  • 如果有⾃增列,truncate⽅式删除之后,⾃增列的值会被初始化,delete⽅式要分情况(如果数据库被重启了,⾃增列值也会被初始化,数据库未被重启,则不变)
    • 如果要删除表定义及其数据,请使⽤drop table语句
    • 安全性:⼩⼼使⽤drop和truncate,尤其没有备份的时候,否则哭都来不及
    • 删除速度,⼀般来说: drop>truncate>delete


上一篇:【HTML5 CSS】display和visibility的区别
下一篇:【Spring Boot】入门

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2025年04月13日 22时47分24秒