DML(Data Manipulation Language)数据操作语言
一、插入语句
方式一:
语法:insert into 表名(列名,…) values(值1,…)
方式二:
语法:insert into 表名 set 列名=值,列名=值,…
-
方式一:
语法:insert into 表名(列名,…) values(值1,…)insert into student(mobile,address,id,name) values ('18338945560','安徽六安','eb0a220a-60ae-47b6-9e6d-a901da9fe355','LiLy'); insert into student values ('a273ea66-0a42-48d2-a17b-388a2feea244','李%四','98765432130','北京东城区'); insert into student values ('6ab71673-9502-44ba-8db0-7f625f17a67d','王_五','98765432130','北京朝阳区');
注意:
- 字段列表顺序可以不按表中字段顺序列出来,但对应值列表必须和字段名列表对应;
- 如果没有字段列表,则值列表中值的顺序必须按实际表中字段的默认顺序进行排列;
- 如果字段值是字符类型或日期类型,则需使用单引号引起来;
使用子查询进行插入:
create table new_student( id char(36) primary key, name varchar(8) not null, mobile char(11), address varchar(150) ) insert into new_student select id,name,mobile,address from student
注意:
- insert子句中字段名列表的数目和数据类型必须和select子句中选择的字段列表的数目和数据类型相匹配。数据类型不考虑长度,但是须比查出来的对应列的长度长或相等。
- select查询字段名可以和insert插入表对应字段名不一致。
-
方式二:
语法:insert into 表名 set 列名=值,列名=值,…insert into beauty set id = 19,name = '刘涛',phone='999';
- . 两种方式的对比:
- 方式1支持插入多行,但是方式2不支持
INSERT INTO beauty VALUES(23,'唐艺昕1','女','1990-4-23','1898888888',NULL,2) ,(24,'唐艺昕2','女','1990-4-23','1898888888',NULL,2) ,(25,'唐艺昕3','女','1990-4-23','1898888888',NULL,2);
- 方式1支持子查询,方式2不支持
INSERT INTO beauty(id,NAME,phone) SELECT 26,'宋茜','11809866'; INSERT INTO beauty(id,NAME,phone) SELECT id,boyname,'1234567' FROM boys WHERE id<3;
- 方式1支持插入多行,但是方式2不支持
二、修改语句
1.修改单表的记录★
语法:
update 表名
set 列=新值,列=新值,...
where 筛选条件;
2.修改多表的记录【补充】
sql92语法:
update 表1 别名,表2 别名
set 列=值,...
where 连接条件
and 筛选条件;
sql99语法:
update 表1 别名
inner|left|right join 表2 别名
on 连接条件
set 列=值,...
where 筛选条件;
-
修改单表的记录
#案例1:修改beauty表中姓唐的女神的电话为13899888899 update beauty set phone='13899888899' where name like '唐%'; #案例2:修改boys表中id好为2的名称为张飞,魅力值 10 update boys set boyname='张飞', usercp = 10 where id = 2;
-
修改多表的记录
#案例 1:修改张无忌的女朋友的手机号为114 #92语法: update beauty,boys set phone='114' where beauty.boyfriend_id = boys.id and boyName = '张无忌'; #99语法: update beauty join boys on beauty.boyfriend_id = boys.id set phone='115' where boyName='张无忌' #案例2:修改没有男朋友的女神的男朋友编号都为2号 update beauty left join boys on beauty.boyfriend_id = boys.id set boyfriend_id = 2 where boys.id is null;
三、删除语句
方式一: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
-
单表的删除:
#案例:删除手机号以9结尾的女神信息 delete from beauty where phone like '%9';
-
多表的删除:
#案例:删除张无忌的女朋友的信息 delete beauty from beauty join boys on beauty.boyfriend_id = boys.id where boyName = '张无忌'; #案例:删除黄晓明的信息以及他女朋友的信息 delete beauty,boys from beauty join boys on beauty.boyfriend_id = boys.id where boyName = '黄晓明';
-
-
方式二:truncate
truncate table boys; #删除表中所有数据
-
delete和truncate的对比:
1.delete 可以加where 条件,truncate不能加 2.truncate删除,效率高一丢丢 3.假如要删除的表中有自增长列: 如果用delete删除后,再插入数据,自增长列的值从断点开始, 而truncate删除后,再插入数据,自增长列的值从1开始。 4.truncate删除没有返回值,delete删除有返回值 5.truncate删除不能回滚,delete删除可以回滚.