插入:insert
修改:modify
删除:delete
insert into 表名(列名,...) values(值1,...);
特点:
1 插入字符、字符串或日期要加单引号,插入数值不需要加引号
2 插入的值的类型要与列类型一致或兼容
3 列的顺序可以调换
4 可以省略列名,此时默认所有列,而且列的书序和表中列的顺序一致
INSERT INTO beauty (id,NAME,sex,borndate,phone,photo,boyfriend_id)
VALUES(13,'唐艺昕','女','1990-04-06','18801253240',NULL,2);
INSERT INTO beauty (id,NAME,sex,borndate,phone,boyfriend_id)
VALUES(15,'金星','女','1990-04-06','18801253240',2);
insert into 表名
set 列名=值,列名=值...
INSERT INTO beauty
SET id=20,NAME='高圆圆',phone='15501255234';
两种方式比较:
1 方式一支持插入多行
INSERT INTO beauty (id,NAME,sex,borndate,phone,photo,boyfriend_id)
VALUES(21,'唐艺昕','女','1990-04-06','18801253240',NULL,2),
(22,'唐艺昕','女','1990-04-06','18801253240',NULL,2),
(23,'唐艺昕','女','1990-04-06','18801253240',NULL,2);
2 方式一支持子查询,方式二不支持
SELECT INTO beauty (id,NAME,phone)
SELECT 26,'项羽','15536201420';
插入多行数据的两种方式
INSERT INTO employees VALUES
(1,'hu','henry','[email protected]','15501253210','AC_ACCOUNT',2000,0.2,2,10,NULL),
(2,'hu','henry','[email protected]','15501253210','AC_ACCOUNT',2000,0.2,2,10,NULL),
(3,'hu','henry','[email protected]','15501253210','AC_ACCOUNT',2000,0.2,2,10,NULL),
(4,'hu','henry','[email protected]','15501253210','AC_ACCOUNT',2000,0.2,2,10,NULL);
INSERT INTO employees
SELECT 5,'hu','henry','[email protected]','1524236554241','AC_ACCOUNT',3000,0.3,2,30,NULL UNION
SELECT 6,'hu','henry','[email protected]','1524236554241','AC_ACCOUNT',3000,0.3,2,30,NULL UNION
SELECT 7,'hu','henry','[email protected]','1524236554241','AC_ACCOUNT',3000,0.3,2,30,NULL;
语法:
update 表名
set 列=新值,列=新值...
where 筛选条件
#修改女神表中姓唐的女生的电话为18888888888
UPDATE beauty SET phone='18888888888' WHERE NAME LIKE '唐%';
#修改boys表中id 为2的名字为张飞,,魅力值为 10
UPDATE boys SET boyName='张飞',userCp=10 WHERE id=2;
语法:
sql92语法
update 表1 别名,表2 别名
set 列=值,...
where 连接条件 and 筛选条件
sql99语法
update 表名 别名
inner|left|right join 表2 别名
on 连接条件
set 列=值
where 筛选条件;
#修改张无忌的女朋友的手机号为114
UPDATE beauty b SET phone='18888888888'
INNER JOIN boys bo
ON b.`name`=bo.boyName
WHERE b.`name`='张无忌';
1 单表的删除
delete from 表名 where 筛选条件
DELETE FROM beauty WHERE phone LIKE ‘%9’;
2 多表删除
sql 92语法:
delete 表1的别名,表2的别名
from 表1 别名,表2 别名
where 连接条件
and 筛选条件
sql99语法:
delete 表1的别名,表2的别名
from 表1 别名
inner|left|right join 表2 别名 on连接条件
where 筛选条件
案例:
#删除张无忌的女朋友的信息
DELETE be
FROM beauty be
INNER JOIN boys b
ON be.`boyfriend_id`=b.`id`
WHERE b.`boyName`='张无忌';
#删除黄晓明的信息以及他女朋友的信息
DELETE be,b
FROM beauty be
INNER JOIN boys b
ON be.`boyfriend_id`=b.`id`
WHERE b.`boyName`='黄晓明';
语法: Truncate table 表名;
Truncate table boys;
1 delete 可以加 where 条件,truncate 不能加
2 truncate删除,效率高一丢丢
3 加入要删除表中的自增长列,
delete删除后,再插入数据,自增长列的值从断点开始;
truncate删除后,再插入数据,自增长列的值从1开始
4 truncate删除没有返回值,delete删除有返回值
5 truncate删除不能回滚,delete删除可以回滚