DML语言
数据操作语言
插入:insert
修改:update
删除:delete
一、插入语句
方式一:经典的插入
语法:
insert into 表名(列名,...)values(值1,...)
案例:
1.插入的值的类型要与列的类型一致或者兼容
insert into beauty(id,NAME,sex,borndate,phone,photo,boyfriend_id)
values(13,'贾玲','1982-5-8','18896325874',NULL,2);
2.不可以为null的列必须插入值,可以为null的列如何插入值的?
方式一:
insert into beauty(id,NAME,sex,borndate,phone,photo,boyfriend_id)
values(13,'贾玲','1982-5-8','18896325874',NULL,2);
方式二:
insert into beauty(id,NAME,sex,borndate,phone,boyfriend_id)
values(13,'贾玲','1982-5-8','18896325874',2);
3.列的顺序可以调换
insert into beauty(NAME,sex,id,phone)
values('杨幂','女',14,'10086');
4.列数和值的个数必须一致
insert into beauty(NAME,sex,id,phone,boyfriend_id)
values('关晓彤','女',15,'10086');×
5.可以省略列名,默认是所有列,而且列的顺序和表中列的顺序是一致的
insert into beauty values (16,'张飞','男',NULL,'110',NULL,NULL);
方式二:
语法:
insert into 表名
set 列名=值,列名=值......
案例:
insert into beauty
set id= 17,NAME='刘涛',phone='15236987';
方式一PK方式二:
1.方式一支持插入多行,但方式二不支持
insert into beauty(id,NAME,sex,borndate,phone,photo,boyfriend_id)
values(18,'贾玲1','1982-5-8','18896325874',NULL,2),
values(19,'贾玲2','1982-5-8','18896325874',NULL,2),
values(20,'贾玲3','1982-5-8','18896325874',NULL,2);
2.方式一支持子查询,方式二不支持
insert into beauty(id,NAME,phone)
select 21,'宋茜','15879455879';
二、修改语句
1.修改单表的记录
语法:
update 表名
set 列=新值,列=新值,...
where 筛选条件;
案例.修改beauty姓唐的女神的电话为10086
update beauty set phone = '10086'
where name like '唐%';
2.修改多表的记录
语法:
sql92语法:
update 表1 别名,表2 别名
set 列=值,...
where 连接条件
and 筛选条件;
sql99语法:
update 表1 别名
inner|left|right join 表2 别名
on 连接条件
set 列=值,...
where 筛选条件;
案例1.修改张无忌女朋友的手机号为114
update boys b
inner join beauty b on bo.'id'=b.'boyfriend_id'
set b.phone='114'
where bo.'boyName'='张无忌';
三、删除语句
方式一:delete
语法:
1.单表的删除
delete from 表名 where 筛选条件
案例:删除手机号以9结尾的女神信息
delete from beauty where phone like '%9';
2.多表的删除
sql92语法:
delete 别名
from 表1 别名,表2 别名
where 连接条件
and 筛选条件
sql99语法:
delete 别名
from 表1 别名
inner|left|right join 表2 别名
on 连接条件
where 筛选条件;
案例:删除张无忌女朋友的信息
delete b
from beauty b
inner join boys bo on b.'boyfriend_id' = bo.'id'
where bo.'boyName'='张无忌';
方式二:truncate
语法:truncate table 表名;
一删全删
truncate table boys;
delete PK truncate:【重点面试题】
1.delete 可以加where条件,truncate不可以
2.truncate效率高一些
3.假如说要删除的字段有自增长列,用delete删除后,自增长列从断点处开始,而truncate删除后,再删除数据自增长列的值从1开始。
4.truncate删除后没有返回值,delete删除后有返回值
5.truncate删除不能回滚,而delete删除后可以回滚。
测试
1.运行一下脚本创建表my_employees
create table my_employees (
Id int (10),
First_name varchar (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表中插入下列数据
INSERT INTO my_employees
VALUES(1,'patel','Ralph','Rpatel',895),
(2,'Dancs','Betty','Bdancs',860),
(3,'Biri','Ben','Bbiri',1100),
(4,'Newman','Chad','Cnewman',750),
(5,'Ropeburn','Audrey','Aropebur',1550);
4.向uesrs表中插入数据
INSERT INTO users
VALUES(1,'Rpatel',10),
(2,'Bdancs',10),
(3,'Bbiri',20),
(4,'Cnewman',30),
(5,'Aropebur',40);
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_employees表的记录全部删除
delete u,m
from users u
inner join my_employees m
on m.`Userid`=u.`userid`
where m.`Userid`='Bbiri';
8.删除所有数据
delete from my_employees;
delete from users;
9.检查所有修正
SELECT * FROM my_employees;
SELECT * FROM users;
10.清空表my_employees
TRUNCATE TABLE my_employees;