一、简介
数据操纵语言DML主要有三种形式:
1) 插入:INSERT INTO 表名 (列名)VALUES (值)
2) 更新:UPDATE 表名 SET 列 = 值 [WHERE 条件]
3) 删除:DELETE FROM 表名 [WHERE 条件]
二、INSRET操作
根据指定列写入值
INSERT INTO 表名 (列名) VALUES (值)
INSERT INTO 表名 (字段名 1, 字段名 2, 字段名 3…) VALUES (值 1, 值 2, 值 3);
-- 指定列新增数据
-- 如果主键是自动增长列可以不用写入
insert into users(userName,birthday,detail) values('tom',now(),'tom的个人信息');
insert into users(userName,birthday,detail) values('jack','1992-4-5','jack的个人信息');
insert into users(userName) values('white');
-- 写入的数据必须要满足约束条件
insert into users(birthday) values('1993-10-10');
-- 为表中每列都写入值
INSERT INTO 表名 VALUES (值)
INSERT INTO 表名 VALUES (值 1, 值 2, 值 3…);
-- 新增数据对整行所有列的数据都进行写入
insert into users values(100,'jerry','1994-1-2','jerry@sina.com','jerry的信息');
insert into users values(null,'zack','1993-6-10','zack@qq.com','zack的信息');
insert into users values(null,'tony',null,null,'tony的信息');
insert into users values(null,'marray','1992-12-10',default,null);
-- 创建users备份表,并将user表的数据都写入备份表
create table backup_user as select * from users;
三、UPDATE语句
更新操作,用来修改基本表中的数据。
--不带条件修改数据
UPDATE 表名 SET 字段名=值;
--带条件修改数据
UPDATE 表名 SET 字段名=值 WHERE 字段名=值;
其中,WHERE条件的使用
WHERE语句后应为一个表达式,且表达式返回是或否的结果
WHERE后的表达式可以为一个或多个使用逻辑运算符AND和OR关联
关系运算符 | 逻辑运算符 |
---|---|
> | AND |
< | OR |
>= | NOT |
<= | |
= | |
!= | |
BETWEEN AND | |
IN | |
LIKE | |
IS |
-- 修改表的数据
-- 对整表数据进行更新
update users set age=20;
-- 可以同时对多列的数据进行修改
update users set age=22,sexupdate users set age=22,sex='男';
-- 将编号为103的用户性别更改为女
update users set sex='女' where userid=103;
-- 将编号为1至3的用户的年龄更改为25
update users set age=25 where userid>=1 and userid<=3;
update users set age=26 where userid between 1 and 3;
update users set age=27 where userid in(1,2,3);
-- 将用户姓名是tom、white、zack的年龄更改为23
update users set age=23 where username in('tom','white','zack');
-- 将姓名以t开头的用户,将他的邮箱清空
-- %通配符,表示匹配任意长度的任意字符
update users set email=null where username like 't%' or username like 'T%';
-- 将姓名中包含ac的用户,将他们的生日设为1995-10-1
update users set birthday='1995-10-1' where username like '%ac%';
-- 将所有QQ邮箱的用户将他的年龄增加5岁
update users set age=age+5 where email like '%qq.com%'
-- 将所有没有填写过资料的用户将其用户信息变更为'待注销用户'
-- 数据为null的判断需要使用is
update users set detail='待注销用户' where detail is null;
-- 将所有填写过邮箱并且性别为男的用户,变更其年龄为20
update users set age=20 where email is not null and sex='男';
-- 将部门、员工、地址表进行导入,并对导入后的表进行复制,复制后的新表名称为location,dep,emp
create table dep as select * from departments;
create table emp as select * from employees;
create table location as select * from locations;
-- 修改编号为100的员工的薪资,增加500块
update emp set salary=salary+500 where employee_id=100;
-- 修改姓名中包含s(不区分大小写)的员工,将其部门调整到编号为50的部门中
-- concat函数用于拼接字段的值
update emp set department_id=50 where concat(first_name,last_name) like '%s%' or concat(first_name,last_name) like '%S%';
-- 调整老板的薪资增加5000
update emp set salary=salary+5000 where manager_id is null;
-- 将所有薪资低于6000的员工薪资上浮500
update emp set salary=salary+500 where salary<6000;
--关键字说明
--UPDATE: 修改数据
--SET: 修改哪些字段
--WHERE: 指定条件
where条件语句是用来限定修改哪一行记录的数据的,如果不加where语句,那么会将目标列所有的值都修改为同一个值。
四、DELETE操作
DELETE操作用于删除基本表中的数据;
TRUNCAT TABLE与DELETE的区别在于DELETE是逐行执行,每次执行将执行事务,并将数据操作记录日志。
TRUNCAT TABLE是整表数据删除为写入日志无法恢复数据,且自动增长列重
--不带条件删除数据
DELETE FROM 表名;
--带条件删除数据
DELETE FROM 表名 WHERE 字段名=值;
--删除表中所有记录
TRUNCATE TABLE 表名;
-- 删除back_user表中的所有数据
delete from backup_user;
truncate table backup_user;
-- 删除users表中编号为3的用户
delete from users where userid=3;