文章目录
1.DML语言:数据操作语言
插入:insert
修改:update
删除:delete
1.1.插入语句
1.1.1方式一:
-
语法:
insert into 表名(列名,...) values(值1,...);
-
插入的值的类型要与列的类型一致或兼容
insert into beauty(id,NAME,sex,borndate,phone,photo,boyfriend_id)
VALUES(13,'唐艺昕','女','1990-4-23','198888888',NULL,2);
- .不可以为null的列必须插入值,可以为null的列如何插入值?
- 方式一:列名写着,值为null
insert into beauty(id,NAME,sex,borndate,phone,photo,boyfriend_id)
VALUES(13,'唐艺昕','女','1990-4-23','198888888',NULL,2);
- 方式二:列名和值都不写
insert into beauty(id,NAME,sex,borndate,phone,boyfriend_id)
VALUES(13,'唐艺昕','女','1990-4-23','198888888',2);
- 列的顺序可以调换
insert into beauty(NAME,sex,id,borndate,phone,boyfriend_id)
VALUES('唐艺昕','女',13,'1990-4-23','198888888',2);
- 列数和值的个数必须一致
insert into beauty(NAME,sex,id,borndate,phone,boyfriend_id)
VALUES('唐艺昕','女',13);
//错误:列数和值的个数不一致
- 可以省略列名,默认所有列,而且列的顺序和表中列的顺序一致
insert into beauty
VALUES(13,'唐艺昕','女','1990-4-23','198888888',NULL,2);
1.1.2方式二:
- 语法:
insert into 表名
set 列名=值,列名=值,....
insert into beauty
set id=19,NAME='刘涛',phone='999';
1.1.3两种方式大PK
- 方式一支持插入多行,方式二不支持
insert into beauty(id,NAME,sex,borndate,phone,photo,boyfriend_id)
VALUES(13,'唐艺昕','女','1990-4-23','198888888',NULL,2)
(14,'唐艺昕','女','1990-4-23','198888888',NULL,2)
(15,'唐艺昕','女','1990-4-23','198888888',NULL,2);
- 方式一支持子查询,方式二不支持
insert into beauty(id,NAME,photo)
select 16,'宋茜','198888888';
insert into beauty(id,NAME,photo)
select id,boyname,'198888888'
from boys where id<3;
1.2 修改语句
- 修改单表的记录【*】:
语法:
update 表名
set 列=新值,列=新值...
where 筛选条件
- 修改多表的记录【补充】
语法:
- 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='13888999988' where name like '唐%';
案例2:修改boys表中id号为2的名称为张飞,魅力值为10
update boys set name='张飞',usercp='10' where id=2;
- 修改多表的记录
案例1.修改张无忌的女朋友的手机号为114
update boys bo
inner join beauty b on bo.`id`=b.`boyfriend`
set b.`phone`='114'
where bo.`boyName`='张无忌';
案例2:修改没有男朋友的女神的男朋友编号都为2号
update boys bo
right inner join beauty b on bo.`id`=b.`boyfriend_id`
set b.`boyfriend`='2'
where b.`boyfriend` is null;
1.3 删除语句
1.3.1 方式一:delete
语法:
-
单表的删除【*】
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 筛选条件
1.3.2方式二:truncate
语法:
truncate table 表名;
1.3.3 案例
- 方式一:delete
- 单表的删除
案例1:删除手机号为9结尾的女神信息
delete from beauty where phone like '%9';
- 多表的删除
案例1:删除张无忌的女朋友的信息
delete b from beauty b left outer join boys bo on b.
boyfriend_id=bo.id
where bo.boyName
=‘张无忌’;`
案例:删除黄晓明的信息以及他女朋友的信息
delete b,bo from boys bo right outer join beauty b on b.
boyfriend_id=bo.
idwhere bo.
boyName='黄晓明';
- 方式二:truncate语句
案例:将魅力值>100的男神信息删除
truncate table boys where usercp>'100';
------------错误,truncate语句后不能接where语句,用于删除表中全部数据
`truncate table boys;’
1.3.4 truncate PK delete
-
delete 可以加where条件,truncate不能加
-
truncate删除,效率高一点点
-
假如要删除的数据表中有自增长列,
如果用delete删除后,再插入数据,自增长列的值从断点开始
而truncate删除后,再插入数据,自增长列的值从1开始 -
truncate删除没有返回值,delete删除有返回值
-
truncate删除不能回滚,delete删除可以回滚
2.练习
1.运行一下脚本创建表my employees
create table my employees(
id int(10)
first_name vanchar(10)
last_name varchar(10)
userid varchar(10)
salary double(10,2)
)
create table users(
id int,
userid varchar(10),
department_id int
)
2.显示表my_employees的结构
desc my_employees;
3.向my_employees表中插入下列数据
id first_name last_name userid salary
1 patel ralph rpatel 895
2 dancs betty bdancs 860
insert into my_employees(id,first_name,last_name,userid,salary) values(1,'patel','ralph','rpatel',895),(2,'dancs','betty','bdancs',860);
insert into my_employees set id=1,first_name=patel,last_name=ralph,userid=rpatel,salary=895;
insert into my_employees select(1,'patel','ralph','rpatel',895)union select (2,'dancs','betty','bdancs',860) union .......;
4.向userid表中插入数据
1 rpatel 10
2 bdancs 10
insert into userid(id,userid,department_id) values(1,'rpatel',10),(2,'bdancs',10);
insert into userid values(1,'rpatel',10),(2,'bdancs',10);
5.将3号员工的last_name修改为drelxer
update my_employees set last_name='drelxer' where id='3';
6.将所有工资少于900的员工的工资改为1000、
update my_employees set salary=1000 where salary<900;
7.将userid为bbiri的user表和my_employee表的记录全部删除
delete us,em from user us inner join my_employee em on us.
userid=em.
useridwhere em.
userid='bbiri';
8.删除所有数据
delete * from users;
delete * from my_employees;
9.检查所做的修正
select * from users;
select * from my_employees;
10.清空表my_employees
truncate from my_employees;