DML
DML英文全称是Data Manipulation Language(数据操作语言),用来对数据库中表的数据记录进行增、删、改操作。
Ø添加数据(INSERT)
insert into tb_emp(username, name, create_time) values ('lingting', '聆听', now());insert into tb_emp values(null, 'lingting1', '123', '2024-07-02');insert into tb_emp(username, name, create_time)values ('lingting', '聆听', now()),('lingting02', '聆听02', now());
注意事项:
Ø修改数据(UPDATE)
修改数据:update 表名 set 字段名1 = 值1 , 字段名2 = 值2 , .... [ where 条件 ] ;
update tb_emp set name = '聆听' , update_time = now() where id = 1;update tb_emp set entrydate = '2024-01-01' , update_time = now();
注意事项:
修改语句的条件可以有,也可以没有,如果没有条件,则会修改整张表的所有数据。
Ø删除数据(DELETE)
删除数据:delete from 表名 [ where 条件 ];
delete from tb_emp where id = 1;delete from tb_emp;
注意事项:
DQL
DQL-基本查询
select name, entrydate from tb_emp;-- 不推荐(性能低)
select * from tb_emp;-- as可以省略
select name as 姓名,entrydate as 入职日期 from tb_emp;select distinct job from tb_emp;
DQL-条件查询
比较运算符 | 功能 |
> | 大于 |
>= | 大于等于 |
< | 小于 |
<= | 小于等于 |
= | 等于 |
<> 或 != | 不等于 |
between ... and ... | 在某个范围之内(含最小、最大值) |
in(...) | 在in之后的列表中的值,多选一 |
like 占位符 | 模糊匹配(_匹配单个字符, %匹配任意个字符) |
is null | 是null |
逻辑运算符 | 功能 |
and 或 && | 并且 (多个条件同时成立) |
or 或 || | 或者 (多个条件任意一个成立) |
not 或 ! | 非 , 不是 |
select * from tb_emp where name = '聆听';select * from tb_emp where id <= 10;select * from tb_emp where job is null;
select * from tb_emp where job is not null;select * from tb_emp where password != '123456';
select * from tb_emp where password <> '123456';select * from tb_emp where entrydate >= '2000-01-01' and entrydate <= '2010-01-01';
select * from tb_emp where entrydate between '2000-01-01' and '2010-01-01';select * from tb_emp where entrydate between '2000-01-01' and '2010-01-01' and gender = '2';
select * from tb_emp where job = 2 or job = 3 or job = 4;
select * from tb_emp where job in (2, 3, 4);select * from tb_emp where name like '__';select * from tb_emp where name like '张%';
ps:后面的内容下次更新;