DML数据操纵语言
DML数据操纵语言
目录
- 概述
- 一、插入语句
- (一)方式一
- (二)方式二:
- (三)两种方式的比较
- 二、修改语句
- 三、删除语句
- 概述
- 方式一:delete
- 方式二:truncate语句 【清空语句】
- delete VS truncate 【面试题!!!】
概述
数据操作语言:
插入:insert
修改:update
删除:delete
一、插入语句
(一)方式一
- 语法:
insert into 表名(列名,……) value(值1,……) - 特点:
- 要求值的类型和字段的类型必须一致或兼容
- 字段的个数和顺序不一定与原始表中的字段个数和顺序一致,但必须保证值和字段一一对应
- 假如表中有可以为null的字段,注意可以通过以下两种方式插入null值
①字段和值都省略
②字段不省略,值对应写null - 字段和值的个数必须一致
- 字段名可以省略,此时默认所有列
use girls;
- 插入的值得类型要与列的类型一致或者兼容
insert into beauty(id,name,sex,borndate,phone,photo,boyfriend_id) value(13,'唐艺昕','女','1990-4-23','13853884820',null,2); select * from beauty;
- 可以为null的列应该如何插入值(注:不能为null的值必须插入值)
- 方式一:对应列名写null
insert into beauty(id,name,sex,borndate,phone,photo,boyfriend_id) value(14,'金星','女','1980-7-23','13853584826',null,9);
- 方式二:列名和null均省略
insert into beauty(id,name,sex,phone) value(15,'娜扎','女','13356789987');
- 方式一:对应列名写null
- 列的顺序可以调换 【一一对应即可】
insert into beauty(name,sex,id,phone) value('蒋欣','女','16','13978894567');select *from beauty;
- 列数和值的个数必须一致
insert into beauty(id,name,sex,phone) value(17,'关晓彤','女','13853586857');
- 可以省略列名,默认所有列,而且列的顺序和表中列的顺序是一致的
insert into beauty values(18,'LYH','男',null,'13009991313',null,null);
(二)方式二:
语法:
insert into 表名
set 列名=值,列名=值,……;
insert into beauty set id=19,name='刘涛',phone='18830475643';
(三)两种方式的比较
-
方式一支持插入多行,方式二不支持
insert into beauty value(23,'唐艺昕1','女','1990-4-23','13853884820',null,2),(24,'唐艺昕2','女','1990-4-23','13853884820',null,2),(25,'唐艺昕3','女','1990-4-23','13853884820',null,2);select *from beauty;
-
方式一支持子查询,方式二不支持
insert into beauty(id,name,phone) select '20','李一桐','13456243455';
二、修改语句
- 修改单表的记录
语法:
update 表名
set 列=新值,列=新值,……
where 筛选条件; - 修改多表的记录 【补充】
语法:- sql92语法:
update 表1 别名1,表2,别名2
set 列=值,……
where 连接条件 and 筛选条件; - sql99语法:
update 表1 别名1
inner|left|right| join 表2 别名2
on 连接条件
set 列=值,……
where 筛选条件;
- sql92语法:
- 修改单表的记录
#案例1:修改beauty表中姓唐的女神的电话为13899888899 set sql_safe_updates=0; update beauty set phone='13899888899' where name like '唐%'; select * from beauty; #案例2:修改boys表中的id=2的名称为张飞,userCP=10 select * from boys; update boys set boyName='张飞',userCP=10 where id=2;
- 修改多表的记录
#案例1:修改张无忌的女朋友的手机号为114 update boys bo inner join beauty b on b.boyfriend_id = bo.idset b.phone='114' where bo.boyName='张无忌'; update boys bo,beauty b set b.phone='114' where b.boyfriend_id=bo.id and bo.boyName='张无忌'; select * from beauty; #案例2:修改没有男朋友的女神的的男朋友编号都改为2 update beauty b left join boys bo on b.boyfriend_id=bo.id set boyfriend_id=2 where bo.id is null;
三、删除语句
以行为单位删除
概述
方式一:delete
语法:
1、单表的删除
delete from 表名 where 筛选条件
2、多表的删除(级联删除)
sql92语法
delete 别名1 【,别名2 】from 表1 别名1,表2 别名2 where 连接条件 and 筛选条件
sql99语法
delete 别名1 【,别名2 】from 表1 别名1 inner|left|right join 表2 别名2 on 连接条件 where 筛选条件
方式二:truncate
语法:
truncate table 表名;
方式一:delete
1、单表的删除
#案例1:删除手机号以9结尾的女神的信息
use girls;
delete from beauty where phone like '%9';
select * from beauty;
2、多表的删除
#案例2:删除张无忌的女朋友的信息
delete b from beauty b inner join boys bo on b.boyfriend_id=bo.id where bo.boyname='张无忌';
#案例3:删除黄晓明的信息以及他女朋友的信息
delete b,bo from beauty b inner join boys bo on b.boyfriend_id = bo.id where bo.boyname='黄晓明';
select * from beauty;
select * from boys;
方式二:truncate语句 【清空语句】
truncate不能和where搭配,当truncate用于删除整张表时,他的效率比delete要高
delete VS truncate 【面试题!!!】
- delete可以加where条件,但truncate不能
- truncate删除效率更高一些
- 假如要删除的表中自增长列,
如果用delete删除后,再插入数据,自增长列的值从断点开始,
而truncate删除后,再插入数据,自增长列的值从1开始 - truncate删除没有返回值,delete删除有返回值(能返回受影响的行数)
- truncate删除不能回滚,delete删除可以回滚
- 使用delete进行删除
select * from boys; delete from boys; insert into boys(boyname,userCP) values('张飞',100),('刘备',140),('关云长',120);
- 【补充】delete可以搭配limit使用
语法:delete from 表名 【where 筛选条件】 【limit 条目数】
案例use girls; select * from beauty; delete from beauty limit 1;#beauty表中第一条数据被删除 delete from beauty where boyfriend_id = 9 limit 1;#虽然符合筛选条件的记录有多个但是只删除了第一条。