目录
1. CRUD
2. 新增(Create)
2.1 单行数据 + 全列插入
2.2 多行数据 + 指定列插入
3. 查询(Retrieve)
3.1 全列查询
3.2 指定列查询
3.3 查询字段为表达式
3.4 别名
3.5 去重:DISTINCT
3.6 排序:ORDER BY
3.7 条件查询:WHERE
3.8 分页查询:LIMIT
4. 修改(Update)
5. 删除(Delete)
注释:在 SQL 中可以使用 “-- 空格 + 描述 ” 来表示注释说明
-- "-- "表示注释说明
CRUD 即增加(Create)、查询(Retrieve)、更新(Update)、删除(Delete)四个单词的首字母缩写。
案例:
-- 创建一张学生表
DROP TABLE IF EXISTS student;
CREATE TABLE student (
id INT,
sn INT comment '学号',
name VARCHAR(20) comment '姓名',
qq_mail VARCHAR(20) comment 'QQ邮箱'
);
-- values 中的值的顺序,必须严格按照建表时的顺序
INSERT INTO student VALUES (100, 10000, '张三', NULL);
INSERT INTO student VALUES (101, 10001, '李四', '11111');
insert into student values (102, 10002, '王五', '22222');
insert into student values (103, 10003, '宋江', null),
(104, 10004, '卢俊义', '44444');
-- 没有出现的字段,填充默认值
insert into student (id,sn,name) values (105, 10005, '公孙胜'),
(106, 10006,'吴用');
案例:
-- 创建考试成绩表
DROP TABLE IF EXISTS exam_result;
CREATE TABLE exam_result (
id INT,
name VARCHAR(20),
chinese DECIMAL(3,1),
math DECIMAL(3,1),
english DECIMAL(3,1)
);
-- 插入测试数据
insert into exam_result (id, name, chinese, math, english) values
(1,'武松', 80, 92, 67),
(2,'石秀', 86, 72, 97),
(3,'花容', 75, 98, 88),
(4,'柴进', 97, 85, 78),
(5,'李应', 87, 93, 87),
(6,'鲁智深', 89, 90, 96);
-- 慎用
SELECT * FROM exam_result;
select id, name from exam_result;
select id from exam_result;
select name, id from exam_result;
select 103 from exam_result;
select id, name, 103 from exam_result;
select 100 / 2 from exam_result;
select id * 10 from exam_result;
select id, name, english + math + chinese from exam_result;
-- 和null比较值都为null, 不是 0 or 1
-- 有 null 参与的运算结果还是 null
-- 即使是 null,做bool值的时候,也看作 false
select * from student;
select id = 102 from student;
select id,id = 102 from student;
select id,id != 102 from student;
select id , name, qq_mail != null from student;
select id , name, qq_mail != 120 from student;
select id , name, qq_mail != null from student;
select id , name, qq_mail + 10 from student;
select id = 102,id + 9901 from student;
select id = 102,id - 9901 from student;
select id , name, qq_mail + 10 from student;
as
select id, name, english + math + chinese as 总分 from exam_result;
select id, name, english + math + chinese as total from exam_result;
select id,name,chinese as 语文,math as 数学,english as 英语 from exam_result;
select id,name,chinese as 语文,math as 数学,english as 英语 ,chinese + math + english as 总分 from exam_result;
-- as 可以省略
select id, name, english + math + chinese 总分 from exam_result;
使用 DISTINCT 关键字对某列数据进行去重
select distinct math from exam_result;
语法:
-- ASC 为升序(从小到大)
-- DESC 为降序(从大到小)
-- 默认为 ASC
SELECT ... FROM table_name [WHERE ...]
ORDER BY column [ASC|DESC], [...];
1. 没有 ORDER BY 子句的查询,返回的顺序是未定义的,永远不要依赖这个顺序2. NULL 数据排序,视为比任何值都小,升序出现在最上面,降序出现在最下面
select * from student order by qq_mail;
select * from student order by qq_mail asc;
-- 结果相同,asc可以省略
select * from student order by qq_mail desc;
3. 使用表达式及别名排序
select id,name,chinese + math + english from exam_result order by chinese + math + english desc;
select id, name, chinese + math + english as 总分 from exam_result order by 总分 desc;
4. 可以对多个字段进行排序,排序优先级随书写顺序
-- 表示先以math 降序比较,若math相同则以chinese比较,若chinese也相同,则以english比较
select * from exam_result order by math desc ,chinese,english;
指摘出较为重要的一部分:
-- 1. >=
select id ,name ,math from exam_result where math >= 80;
-- 2. <=> 也等于,NULL 安全('=',NULL 不安全),例如 NULL <=> NULL 的结果是 TRUE(1)
select * from exam_result where null <=> null;
-- 因为全为真,所有都查询
-- 3. between .. and .. [ .. ]
select id,name ,math from exam_result where math between 80 and 95;
-- 4. in(option ..) 如果是option 里的就查询,指定查询
select * from exam_result where name in ('武松', '鲁智深');
-- 5. is null
select * from exam_result where null is null ;
-- 因为全为真,所有都查询
-- 6. like 模糊匹配。% 表示任意多个(包括 0 个)任意字符;
select * from exam_result where name like '鲁%';
select * from exam_result where name like '%智%';
select * from exam_result where name like '%深';
-- '_' 表示任意一个字符
select * from exam_result where name like '鲁__';
-- 结果相同
-- 7. and or not
select * from exam_result where chinese > 80 and english >80;
select * from exam_result where chinese > 80 or english >80;
select * from exam_result where not english >80;
注 :1. WHERE 条件可以使用表达式,但不能使用别名。2. AND 的优先级高于 OR ,在同时使用时,需要使用小括号 () 包裹优先执行的部分
-- 分页
-- limit : 限制 每次多少个结果
-- offset : 起始位置,从 0 开始计算
-- limit l offset o;
-- limit o, l;
-- limit l; <-> limit l offset 0;
-- 起始位置:0,限制:2
select * from exam_result order by math, id limit 2;
-- 起始位置:3,限制:2
select * from exam_result order by math, id limit 2 offset 3;
-- 起始位置:3,限制:2
select * from exam_result order by math, id limit 3, 2;
语法:
UPDATE table_name SET column = expr [, column = expr ...]
[WHERE ...] [ORDER BY ...] [LIMIT ...]
-- 将武松同学的数学成绩变更为 80 分
UPDATE exam_result SET math = 80 WHERE name = '武松';
-- 将鲁智深同学的数学成绩变更为 60 分,语文成绩变更为 70 分
UPDATE exam_result SET math = 60, chinese = 70 WHERE name = '鲁智深';
-- 将总成绩倒数前三的 3 位同学的数学成绩加上 30 分
UPDATE exam_result SET math = math + 30 ORDER BY chinese + math + english LIMIT
3;
语法:
DELETE FROM table_name [WHERE ...] [ORDER BY ...] [LIMIT ..
DELETE FROM exam_result WHERE name = '武松';
DROP TABLE IF EXISTS for_delete;
CREATE TABLE for_delete (
id INT,
name VARCHAR(20)
);
-- 插入测试数据
INSERT INTO for_delete (name) VALUES ('A'), ('B'), ('C');
-- 删除整表数据
DELETE FROM for_delete;