DML语言:数据库操作语言
插入: insert
修改: update
删除: delete
语法:
insert into 表名(列名,...) values(值1,...);
insert into beauty(id,name,sex,borndate,phone,photo,boyfriend_id) values(13,"唐艺昕","女","1990-4-23","189888888",null,2);
方式一:
insert into beauty(id,name,sex,borndate,phone,photo,boyfriend_id) values(13,"唐艺昕","女","1990-4-23","189888888",null,2);
方式二:
insert into beauty(id,name,sex,borndate,phone,boyfriend_id) values(14,"董璇","女","1980-4-40","1382888888",3);
insert into beauty(name,sex,id,phone) values("蒋欣","女",16,"110");
insert into beauty(name,sex,id,phone) values("关晓彤","女") //会报错,列数和值的个数不一致
insert into beauty values(18,"张飞","男",null,"119",null,null);
语法:
insert into 表名
set 列名=值,列名=值,...
insert into beauty set id=19,name="刘涛",phone="9999";
两种插入方式比较:
insert into beauty(id,name,sex,borndate,phone,photo,boyfriend_id)
values(24,"唐艺昕1","女","1990-4-23","189888888",null,2),
(25,"唐艺昕1","女","1990-4-23","189888888",null,2),
(26,"唐艺昕2","女","1990-4-23","189888888",null,2);
insert into beauty(id,name,phone) select 26,"宋茜","118098641";
insert into beauty(id,name,phone) select id,boyname,"123456" from boys where id < 3;
语法:
update 表名
set 列=新值,列=新值...
where 筛选条件;
update beauty set phone = "1388998887" where name like "唐%";
update boys set boyname = "张飞",usercp = 10 where id = 2;
//sql92语法:
update 表1 别名,表2 别名
set 列 = 值,....
where 连接条件
and 筛选条件;
//sql99语法:
update 表1 别名
inner | left | right join 表2 别名
on 连接条件
set 列 = 值,...
where 筛选条件
update boys bo inner join beauty b on bo."id" = b."boyfriend_id"
set b."phone" = "114" where bo."boyName" = "张无忌";
update boys bo right join b on bo."id" = b."boyfriend_id"
set b."boyfriend_id" = 2
where b."id" is null;
//单表的删除
delete from 表名 where 筛选条件;
//多表的删除
sql92语法:
delete 表1的别名,表2的别名
from 表1 别名,表2 别名
where 筛选条件
and 筛选条件
sql99语法:
delete 表1的别名,表2的别名
from 表1 别名
inner | left | right join 表2 别名 on 连接条件
where 筛选条件
delete from beauty where phone like "%9";
delete b from beauty b
inner join boys bo on b."boyfriend_id" = bo."id"
where bo."boyName" = "张无忌";
delete b,bo from beauty b
inner join boys bo on b."boyfriend_id" = bo."id"
where bo."boyName" = "黄晓明";
truncate table 表名; //全部删除
truncate table boys; //将整个表的数据清空
两种方式的比较:
① delete 可以加 where 筛选条件 ,truncate 不能加
② truncate 删除效率更高(没有筛选条件)
③ 如果要删除的表中有自增长列 : 用delete 删除后,再插入数据,自增长列的值从断点开始;而 truncate删除后,再插入数据,自增长列的值从1 开始
④ truncate 删除后没有返回值,delete删除有返回值
⑤ truncate 删除不能回滚,delete 删除可以回滚