文章目录
- 一.插入语言
- 二.修改语句
- 三.删除语句
-
- 方式一:delete
- 方式二:truncate
- [delete pk truncate面试题]
数据操作语言:
插入:insert
修改:update
删除:delete
123
一.插入语言
方式一:经典的插入
语法:
insert into 表名(列名,...) value(值1,...);
1.插入的值的类型要与列的类型一致或兼容
use girls;
insert into beauty(id,name,sex,borndate,phone,photo,boyfriend_id)
values(13,'唐艺昕','女','1990-4-23','18988888888',null,2); # 类型一致或兼容
select * from beauty;
12345
2.不可以为null的列必须插入值,可以为null的列是如何插入值的?
方式①:
insert into beauty(id,name,sex,borndate,phone,photo,boyfriend_id)
values(13,'唐艺昕','女','1990-4-23','18988888888',null,2); # 直接输入null
12
方式②:
insert into beauty(id,name,sex,borndate,phone,boyfriend_id)
values(14,'金星','女','1990-4-23','13888888888',9);
insert into beauty(id,name,sex,phone)
values(15,'娜扎','女','13888888888'); # 加默认值
12345
3.列的顺序可否调换?可以
insert into beauty(name,sex,id,phone) # 顺序可调换
values('蒋欣','女',16,'110');
12
4.列数和值的个数必须一致
insert into beauty(name,sex,id,phone,boyfriend_id)
values('蒋欣','女',17,'110'); # 个数不一致,报错!
12
5.可以省略列名,默认是所有列,而且列的顺序和表的顺序是一致的
insert into beauty # 省略列名,但顺序默认一致
values(18,'张飞','男',null,'119',null,null);
12
方式二:
语法:
insert into 表名 set 列名=值,列名=值,...
insert into beauty
set id=19,name='刘涛',phone='999';
12
两种方式大PK
1.方式一支持插入多行,方式二不支持
insert into beauty # 方式一支持插入多行,方式二不支持
values(23,'唐艺昕1','女','1990-4-23','18988888888',null,2),
(24,'唐艺昕2','女','1990-4-23','18988888888',null,2),
(25,'唐艺昕3','女','1990-4-23','18988888888',null,2);
1234
2.方式一支持子查询,方式二不支持
insert into beauty (id,name,phone)
select 26,'宋茜','11809866'; # 方式一支持select,也相当于插入
12
二.修改语句
1.修改单表的记录★★★
update 表名 set 列=新值,列=新值,... where 筛选条件;
2.修改多表的记录[补充]
语法:
sql92语法:
update 表1 别名,表2 别名
set 列=值,...
where 连接条件
and 筛选条件;
sql99语法:
update 表1 别名
inner/left/right join 表2 别名
on 连接条件
set 列=值,...
where 筛选条件
123456789101112
1.修改单表的记录
案例1: 修改beauty表中姓唐的女神电话更改为13899888899
set sql_safe_updates=0;
update beauty set phone='13899888899' # 修改单表update ... set
where name like '唐%';
123
** 案例2:** 修改boys表中id为2的名称为张飞,魅力值为10
update boys set boyname='张飞',usercp=10
where id =2;
12
2.修改多表记录
案例1: 修改张无忌女朋友的手机号为114
update boys bo
inner join beauty b on bo.id=b.boyfriend_id
set b.phone='114'
where bo.boyName='张无忌'; # 内连接修改多表记录
1234
案例2: 修改没有男朋友的女神的男朋友都为2号
update boys bo
right join beauty b on bo.id=b.boyfriend_id
set b.boyfriend_id=2
where b.id is null; # 右外连接修改多表记录
1234
三.删除语句
方式一:delete
语法:
1.单表的删除★★★
delete from 表名 where 筛选条件
2.多表的删除[补充]
sql92语法:
delete 表1/表2的别名 from 表1 别名,表2 别名 where 连接条件 and 筛选条件;
sql99语法:
delete 表1/表2的别名 from 表1 别名
inner/left/right join 表2 别名
on 连接条件
where 筛选条件;
方式二:truncate
语法:truncate table 表名;
方式一:delete
1.单表的删除
案例: 删除手机号以9结尾的女神信息
delete from beauty
where phone like '%9';
select * from beauty;
123
2.多表的删除
案例: 删除张无忌女朋友的信息
delete b
from beauty b
inner join boys bo on b.boyfriend_id=bo.id
where bo.boyName='张无忌'; # 内连接多表删除
1234
案例: 删除黄晓明的信息以及他女朋友的信息
delete b,bo
from beauty b
inner join boys bo on b.boyfriend_id=bo.id
where bo.boyName='黄晓明';
1234
方式二:truncate
案例: 将魅力值大于100的男神删除
truncate table boys where usercp>100; #不允许加where,相当于清空
1
delete pk truncate[面试题]
1.delete
可以加where
条件,truncate
不能加
2.truncate
删除,效率高一丢丢
3.假如要删除的表中有自增长列,如果用delete
删除后,再插入数据,自增长的值从断点开始,
而truncate
删除后,再插入数据,自增长列的值从1开始.
4.truncate
删除没有返回值,delete
删除有返回值
5.truncate
删除不能回滚,delete
删除可以回滚
delete from boys;
truncate table boys;
insert into boys (boyname,usercp)
values('张飞',100),('刘备',100),('关云长',100);