文章目录
1. 数据库约束
1.1 约束类型
- NOT NULL - 指示某列不能存储 NULL 值。
- UNIQUE - 保证某列的每行必须有唯一的值。
- DEFAULT - 规定没有给列赋值时的默认值。
- PRIMARY KEY - NOT NULL 和UNIQUE 的结合。确保某列(或两个列多个列的结合)有唯一标识,有助于更容易更快速地找到表中的一个特定的记录。
- FOREIGN KEY - 保证一个表中的数据匹配另一个表中的值的参照完整性。
- CHECK- 保证列中的值符合指定的条件。对于MySQL数据库,对CHECK子句进行分析,但是忽略CHECK子句。
1.2 null约束
--创建表时,可以指定某列不为空
create table student(
id int not null,
sn int,
name varchar(20),
qq_mail varchar(20));
1.3 unique: 唯一约束
--指定sn列为唯一的、不重复的
create table student(
id int not null,
sn int unique,
name varchar(20),
qq_mail varchar(20));
1.4 default: 默认值约束
--指定插入数据时,name列为空,默认值unkown
create table student(
id int not null,
sn int,
name varchar(20) default 'unknow',
qq_mail varchar(20));
1.5 primary key: 主键约束
--指定id列为主键
create table student(
id int primary key,
sn int,
name varchar(20) default 'unknow',
qq_mail varchar(20));
1.6 foreign key: 外键约束
外键用于关联其他表的主键或唯一键, 语法如下
foreign key (字段名) references 主表(列)
- 示例
-- 创建班级表classes, id为主键:
create table classes(
id int primary key auto_increment,
name varchar(20),
`desc` varchar(100));-- 创建班级表,有使用MySQL关键字作为字段时,需要使用``来标识
-- 创建学生表student,一个学生对应一个班级,一个班级对应多个学生。
-- 使用id为主键,classes_id为外键,关联班级表id
create table student(
id int primary key auto_increment,
sn int unique,
name varchar(20) default 'unknow',
qq_mail varchar(20),
class_id int,
foreign key (class_id) references classes(id));
1.7 check约束 (了解即可)
-- MySQL使用时不报错, 但忽略该约束
create table test(
id int,
name varchar(20),
sex varchar(1),
check(sex = '男' or sex '女'));
2. 表的设计
三大范式:
2.1 一对一
解释: 一个学生可以拥有一个账号, 一个账号只能属于一个学生
可以使用一张表表示, 也可以使用两张表(通过一个id链接)
2.2 6一对多
一个学生只能属于一个班级, 一个班级可以包含多个学生
2.3 多对多
一个学生可以选择多门课程学习, 一门课程也可以被多个学生选择
对对多举例
-- 创建课程表
create table course(
id int primary key auto_increment,
name varchar(20));
-- 创建学生课程中间表, 考试成绩表
create table score(
id int primary key auto_increment,
score decimal(3, 1),
student_id int,
course_id int,
foreign key (student_id) references student(id),
foreign key (course_id) references course(id));
-- 创建学生表
create table student(
id int primary key auto_increment,
name varchar(20));
3. 新增
可以将另一张表查到的信息插入到新的表里面
语法
INSERT INTO table_name [(column [, column ...])] SELECT ...
案例:创建一张用户表,设计有name姓名、email邮箱、sex性别、mobile手机号字段。需要把已有的学生数据复制进来,可以复制的字段为name、qq_mail
-- 创建一张用户表
create table user(
name varchar(20),
email varchar(10),
sex varchar(1),
mobile varchar(15));
-- 创建一张学生表
create table student(
id int primary key auto_increment,
name varchar(20),
qq_mail varchar(10));
-- 向用户表中插入一些数据
insert into user values
('张三', '123@qq.com', '男', '123456'),
('李四', '111@qq.com','男','111111'),
('王五','112@qq.com','女','112112');
select * from user;
+--------+------------+------+--------+
| name | email | sex | mobile |
+--------+------------+------+--------+
| 张三 | 123@qq.com | 男 | 123456 |
| 李四 | 111@qq.com | 男 | 111111 |
| 王五 | 112@qq.com | 女 | 112112 |
+--------+------------+------+--------+
-- 将user表里的name和mail导入到student表里的name和qq_mail
insert into
student
(name, qq_mail)
select name, email from user;
select * from student;
+----+--------+------------+
| id | name | qq_mail |
+----+--------+------------+
| 1 | 张三 | 123@qq.com |
| 2 | 李四 | 111@qq.com |
| 3 | 王五 | 112@qq.com |
+----+--------+------------+
-- 注意, 插入数据时列名可以不同, 但是数据类型必须相同
4. 查询
4.1 聚合查询
4.1.1 聚合函数
常见的统计总数、计算平局值等操作,可以使用聚合函数来实现,常见的聚合函数有:
案例
- count
-- 统计student表中共有多少位同学
select count(*) from student;
+----------+
| count(*) |
+----------+
| 7 |
+----------+
select count(0) from student;
+----------+
| count(0) |
+----------+
| 7 |
+----------+
-- 统计合法语文成绩人数, chinese为null不计入数量
select * from student;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 1 | 唐三藏 | 67.0 | 98.0 | 56.0 |
| 2 | 孙悟空 | 87.5 | 78.0 | 77.0 |
| 3 | 猪悟能 | 88.0 | 98.5 | 90.0 |
| 4 | 曹孟德 | 82.0 | 84.0 | 67.0 |
| 5 | 刘玄德 | 55.5 | 85.0 | 45.0 |
| 6 | 孙权 | 70.0 | 73.0 | 78.5 |
| 7 | 宋公明 | 75.0 | 65.0 | 30.0 |
| 8 | 张三 | NULL | 54.0 | 78.0 |
+------+-----------+---------+------+---------+
select count(chinese) from student;
+----------------+
| count(chinese) |
+----------------+
| 7 |
+----------------+
- sum
-- 统计数学成绩总分
select sum(math) from student;
+-----------+
| sum(math) |
+-----------+
| 635.5 |
+-----------+
-- 不及格<60的总分, 没有结果返回null
select sum(math) from student where math < 60;
+-----------+
| sum(math) |
+-----------+
| 54.0 |
+-----------+
- avg
-- 统计平均总分
select avg(chinese + math + english) as 平均总分 from student;
+--------------+
| 平均总分 |
+--------------+
| 221.42857 |
+--------------+
- max
-- 返回英语最高分
select max(english) from student;
+--------------+
| max(english) |
+--------------+
| 90.0 |
+--------------+
- min
-- 返回 > 70 分以上的数学最低分
select min(math) from student where math > 70;
+-----------+
| min(math) |
+-----------+
| 73.0 |
+-----------+
4.1.2 group by子句
SELECT 中使用 GROUP BY 子句可以对指定列进行分组查询。需要满足:使用 GROUP BY 进行分组查询时,SELECT 指定的字段必须是“分组依据字段”,其他字段若想出现在SELECT 中则必须包含在聚合函数中.
select column1, sum(column2), .. from table group by column1,column3;
案例
-- 数据准备, 职员表,有id(主键)、name(姓名)、role(角色)、salary(薪水)
create table emp(
id int primary key auto_increment,
name varchar(20) not null,
role varchar(20) not null,
salary numeric(11,2)
);
insert into emp(name, role, salary) values
('马云','服务员', 1000.20),
('马化腾','游戏陪玩', 2000.99),
('孙悟空','游戏角色', 999.11),
('猪无能','游戏角色', 333.5),
('沙和尚','游戏角色', 700.33),
('隔壁老王','董事长', 12000.66);
-- 查询每个角色的最高工资、最低工资和平均工资
select role, max(salary), min(salary), avg(salary) from emp group by role;
+--------------+-------------+-------------+--------------+
| role | max(salary) | min(salary) | avg(salary) |
+--------------+-------------+-------------+--------------+
| 服务员 | 1000.20 | 1000.20 | 1000.200000 |
| 游戏陪玩 | 2000.99 | 2000.99 | 2000.990000 |
| 游戏角色 | 999.11 | 333.50 | 677.646667 |
| 董事长 | 12000.66 | 12000.66 | 12000.660000 |
+--------------+-------------+-------------+--------------+
4.1.3 having
GROUP BY 子句进行分组以后,需要对分组结果再进行条件过滤时,不能使用 WHERE 语句,而需要用HAVING
-- 显示平均工资低于1500的角色和它的平均工资
select role, max(salary), min(salary), avg(salary) from emp group by role having avg(salary) < 1500;
+--------------+-------------+-------------+-------------+
| role | max(salary) | min(salary) | avg(salary) |
+--------------+-------------+-------------+-------------+
| 服务员 | 1000.20 | 1000.20 | 1000.200000 |
| 游戏角色 | 999.11 | 333.50 | 677.646667 |
+--------------+-------------+-------------+-------------+
4.2 联合查询
实际开发中往往数据来自不同的表,所以需要多表联合查询。多表查询是对多张表的数据取笛卡尔积:
注意:关联查询可以对关联表使用别名。
初始化案例数据
-- 创建班级表
create table classes(
id int primary key auto_increment,
name varchar(20),
`desc` varchar(100));
-- 创建学生表
create table student(
id int primary key auto_increment,
sn varchar(10), name varchar(20),
qq_mail varchar(20),
classes_id int,
foreign key(classes_id) references classes(id));
-- 创建课程表
create table course(
id int primary key auto_increment,
name varchar(20));
-- 创建分数表
create table score(
id int primary key auto_increment,
score decimal(3, 1),
student_id int,
course_id int,
foreign key(student_id) references student(id),
foreign key(course_id) references course(id));
-- 插入一些数据
insert into classes(name, `desc`) values
('计算机系2019级1班', '学习了计算机原理、C和Java语言、数据结构和算法'),
('中文系2019级3班','学习了中国传统文学'),
('自动化2019级5班','学习了机械自动化');
insert into student(sn, name, qq_mail, classes_id) values
('09982','黑旋风李逵','xuanfeng@qq.com',1),
('00835','菩提老祖',null,1),
('00391','白素贞',null,1),
('00031','许仙','xuxian@qq.com',1),
('00054','不想毕业',null,1),
('51234','好好说话','say@qq.com',2),
('83223','tellme',null,2),
('09527','老外学中文','foreigner@qq.com',2);
insert into course(name) values
('Java'),('中国传统文化'),('计算机原理'),('语文'),('高阶数学'),('英文');
insert into score(score, student_id, course_id) values
-- 黑旋风李逵
(70.5, 1, 1),(98.5, 1, 3),(33, 1, 5),(98, 1, 6),
-- 菩提老祖
(60, 2, 1),(59.5, 2, 5),
-- 白素贞
(33, 3, 1),(68, 3, 3),(99, 3, 5),
-- 许仙
(67, 4, 1),(23, 4, 3),(56, 4, 5),(72, 4, 6),
-- 不想毕业
(81, 5, 1),(37, 5, 5),
-- 好好说话
(56, 6, 2),(43, 6, 4),(79, 6, 6),
-- tellme
(80, 7, 2),(92, 7, 6);
4.2.1 内连接
语法
select 字段 from 表1 别名1 [inner] join 表2 别名2 on 连接条件 and 其他条件;
select 字段 from 表1 别名1,表2 别名2 where 连接条件 and 其他条件;
案例:
(1) 查询"许仙"同学的成绩
select
stu.name,
sco.score
from
student stu
inner join score
on stu.id=sco.student_id and stu.name='许仙';
-- 或者
select
stu.name
sco.score
from
student stu, score sco
where stu.id=sco.student_id and stu.name='许仙';
+--------+-------+
| name | score |
+--------+-------+
| 许仙 | 67.0 |
| 许仙 | 23.0 |
| 许仙 | 56.0 |
| 许仙 | 72.0 |
+--------+-------+
(2) 查询所有学生的总成绩, 及同学的个人信息:
-- 成绩表对学生表是多对1关系,查询总成绩是根据成绩表的同学id来进行分组的
select
stu.sn,
stu.name,
stu.qq_mail,
sum(sco.score)
from
student stu
join score sco on stu.id = sco.student_id
group by sco.student_id;
+-------+-----------------+-----------------+----------------+
| sn | name | qq_mail | sum(sco.score) |
+-------+-----------------+-----------------+----------------+
| 09982 | 黑旋风李逵 | xuanfeng@qq.com | 300.0 |
| 00835 | 菩提老祖 | NULL | 119.5 |
| 00391 | 白素贞 | NULL | 200.0 |
| 00031 | 许仙 | xuxian@qq.com | 218.0 |
| 00054 | 不想毕业 | NULL | 118.0 |
| 51234 | 好好说话 | say@qq.com | 178.0 |
| 83223 | tellme | NULL | 172.0 |
+-------+-----------------+-----------------+----------------+
(3) 查询所有同学的成绩, 级同学的个人信息
-- 查询出来的都是有成绩的同学,“老外学中文”同学 没有显示
select * from student stu join score sco on stu.id = sco.student_id;
+----+-------+-----------------+-----------------+------------+-------+------------+-----------+
| id | sn | name | qq_mail | classes_id | score | student_id | course_id |
+----+-------+-----------------+-----------------+------------+-------+------------+-----------+
| 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 1 | 70.5 | 1 | 1 |
| 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 1 | 98.5 | 1 | 3 |
| 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 1 | 33.0 | 1 | 5 |
| 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 1 | 98.0 | 1 | 6 |
| 2 | 00835 | 菩提老祖 | NULL | 1 | 60.0 | 2 | 1 |
| 2 | 00835 | 菩提老祖 | NULL | 1 | 59.5 | 2 | 5 |
| 3 | 00391 | 白素贞 | NULL | 1 | 33.0 | 3 | 1 |
| 3 | 00391 | 白素贞 | NULL | 1 | 68.0 | 3 | 3 |
| 3 | 00391 | 白素贞 | NULL | 1 | 99.0 | 3 | 5 |
| 4 | 00031 | 许仙 | xuxian@qq.com | 1 | 67.0 | 4 | 1 |
| 4 | 00031 | 许仙 | xuxian@qq.com | 1 | 23.0 | 4 | 3 |
| 4 | 00031 | 许仙 | xuxian@qq.com | 1 | 56.0 | 4 | 5 |
| 4 | 00031 | 许仙 | xuxian@qq.com | 1 | 72.0 | 4 | 6 |
| 5 | 00054 | 不想毕业 | NULL | 1 | 81.0 | 5 | 1 |
| 5 | 00054 | 不想毕业 | NULL | 1 | 37.0 | 5 | 5 |
| 6 | 51234 | 好好说话 | say@qq.com | 2 | 56.0 | 6 | 2 |
| 6 | 51234 | 好好说话 | say@qq.com | 2 | 43.0 | 6 | 4 |
| 6 | 51234 | 好好说话 | say@qq.com | 2 | 79.0 | 6 | 6 |
| 7 | 83223 | tellme | NULL | 2 | 80.0 | 7 | 2 |
| 7 | 83223 | tellme | NULL | 2 | 92.0 | 7 | 6 |
+----+-------+-----------------+-----------------+------------+-------+------------+-----------+
-- 学生表、成绩表、课程表3张表关联查询
select
stu.id,
stu.sn,
stu.name,
stu.qq_mail,
sco.score,
sco.course_id,
cou.name
from
student stu
join score sco on stu.id = sco.student_id
join course cou on sco.course_id = cou.id
order by stu.id;
+----+-------+-----------------+-----------------+-------+-----------+--------------------+
| id | sn | name | qq_mail | score | course_id | name |
+----+-------+-----------------+-----------------+-------+-----------+--------------------+
| 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 70.5 | 1 | Java |
| 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 98.5 | 3 | 计算机原理 |
| 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 33.0 | 5 | 高阶数学 |
| 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 98.0 | 6 | 英文 |
| 2 | 00835 | 菩提老祖 | NULL | 60.0 | 1 | Java |
| 2 | 00835 | 菩提老祖 | NULL | 59.5 | 5 | 高阶数学 |
| 3 | 00391 | 白素贞 | NULL | 33.0 | 1 | Java |
| 3 | 00391 | 白素贞 | NULL | 68.0 | 3 | 计算机原理 |
| 3 | 00391 | 白素贞 | NULL | 99.0 | 5 | 高阶数学 |
| 4 | 00031 | 许仙 | xuxian@qq.com | 67.0 | 1 | Java |
| 4 | 00031 | 许仙 | xuxian@qq.com | 23.0 | 3 | 计算机原理 |
| 4 | 00031 | 许仙 | xuxian@qq.com | 56.0 | 5 | 高阶数学 |
| 4 | 00031 | 许仙 | xuxian@qq.com | 72.0 | 6 | 英文 |
| 5 | 00054 | 不想毕业 | NULL | 81.0 | 1 | Java |
| 5 | 00054 | 不想毕业 | NULL | 37.0 | 5 | 高阶数学 |
| 6 | 51234 | 好好说话 | say@qq.com | 56.0 | 2 | 中国传统文化 |
| 6 | 51234 | 好好说话 | say@qq.com | 43.0 | 4 | 语文 |
| 6 | 51234 | 好好说话 | say@qq.com | 79.0 | 6 | 英文 |
| 7 | 83223 | tellme | NULL | 80.0 | 2 | 中国传统文化 |
| 7 | 83223 | tellme | NULL | 92.0 | 6 | 英文 |
+----+-------+-----------------+-----------------+-------+-----------+--------------------+
4.2.2 外连接
外连接分为左外连接和右外连接。如果联合查询,左侧的表完全显示我们就说是左外连接;右侧的表完全显示我们就说是右外连接。
语法
-- 左外连接,表1完全显示
select 字段名 from 表名1 left join 表名2 on 连接条件;
-- 右外连接,表2完全显示
select 字段 from 表名1 right join 表名2 on 连接条件;
案例:查询所有同学的成绩,及同学的个人信息,如果该同学没有成绩,也需要显示
-- “老外学中文”同学 没有考试成绩,也显示出来了
select * from student stu left join score sco on stu.id = sco.student_id;
+----+-------+-----------------+------------------+------------+-------+------------+-----------+
| id | sn | name | qq_mail | classes_id | score | student_id | course_id |
+----+-------+-----------------+------------------+------------+-------+------------+-----------+
| 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 1 | 70.5 | 1 | 1 |
| 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 1 | 98.5 | 1 | 3 |
| 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 1 | 33.0 | 1 | 5 |
| 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 1 | 98.0 | 1 | 6 |
| 2 | 00835 | 菩提老祖 | NULL | 1 | 60.0 | 2 | 1 |
| 2 | 00835 | 菩提老祖 | NULL | 1 | 59.5 | 2 | 5 |
| 3 | 00391 | 白素贞 | NULL | 1 | 33.0 | 3 | 1 |
| 3 | 00391 | 白素贞 | NULL | 1 | 68.0 | 3 | 3 |
| 3 | 00391 | 白素贞 | NULL | 1 | 99.0 | 3 | 5 |
| 4 | 00031 | 许仙 | xuxian@qq.com | 1 | 67.0 | 4 | 1 |
| 4 | 00031 | 许仙 | xuxian@qq.com | 1 | 23.0 | 4 | 3 |
| 4 | 00031 | 许仙 | xuxian@qq.com | 1 | 56.0 | 4 | 5 |
| 4 | 00031 | 许仙 | xuxian@qq.com | 1 | 72.0 | 4 | 6 |
| 5 | 00054 | 不想毕业 | NULL | 1 | 81.0 | 5 | 1 |
| 5 | 00054 | 不想毕业 | NULL | 1 | 37.0 | 5 | 5 |
| 6 | 51234 | 好好说话 | say@qq.com | 2 | 56.0 | 6 | 2 |
| 6 | 51234 | 好好说话 | say@qq.com | 2 | 43.0 | 6 | 4 |
| 6 | 51234 | 好好说话 | say@qq.com | 2 | 79.0 | 6 | 6 |
| 7 | 83223 | tellme | NULL | 2 | 80.0 | 7 | 2 |
| 7 | 83223 | tellme | NULL | 2 | 92.0 | 7 | 6 |
| 8 | 09527 | 老外学中文 | foreigner@qq.com | 2 | NULL | NULL | NULL |
+----+-------+-----------------+------------------+------------+-------+------------+-----------+
-- 对应的右外连接为:
select * from score sco right join student stu on stu.id = sco.student_id;
+-------+------------+-----------+----+-------+-----------------+------------------+------------+
| score | student_id | course_id | id | sn | name | qq_mail | classes_id |
+-------+------------+-----------+----+-------+-----------------+------------------+------------+
| 70.5 | 1 | 1 | 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 1 |
| 98.5 | 1 | 3 | 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 1 |
| 33.0 | 1 | 5 | 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 1 |
| 98.0 | 1 | 6 | 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 1 |
| 60.0 | 2 | 1 | 2 | 00835 | 菩提老祖 | NULL | 1 |
| 59.5 | 2 | 5 | 2 | 00835 | 菩提老祖 | NULL | 1 |
| 33.0 | 3 | 1 | 3 | 00391 | 白素贞 | NULL | 1 |
| 68.0 | 3 | 3 | 3 | 00391 | 白素贞 | NULL | 1 |
| 99.0 | 3 | 5 | 3 | 00391 | 白素贞 | NULL | 1 |
| 67.0 | 4 | 1 | 4 | 00031 | 许仙 | xuxian@qq.com | 1 |
| 23.0 | 4 | 3 | 4 | 00031 | 许仙 | xuxian@qq.com | 1 |
| 56.0 | 4 | 5 | 4 | 00031 | 许仙 | xuxian@qq.com | 1 |
| 72.0 | 4 | 6 | 4 | 00031 | 许仙 | xuxian@qq.com | 1 |
| 81.0 | 5 | 1 | 5 | 00054 | 不想毕业 | NULL | 1 |
| 37.0 | 5 | 5 | 5 | 00054 | 不想毕业 | NULL | 1 |
| 56.0 | 6 | 2 | 6 | 51234 | 好好说话 | say@qq.com | 2 |
| 43.0 | 6 | 4 | 6 | 51234 | 好好说话 | say@qq.com | 2 |
| 79.0 | 6 | 6 | 6 | 51234 | 好好说话 | say@qq.com | 2 |
| 80.0 | 7 | 2 | 7 | 83223 | tellme | NULL | 2 |
| 92.0 | 7 | 6 | 7 | 83223 | tellme | NULL | 2 |
| NULL | NULL | NULL | 8 | 09527 | 老外学中文 | foreigner@qq.com | 2 |
+-------+------------+-----------+----+-------+-----------------+------------------+------------+
-- 学生表、成绩表、课程表3张表关联查询
SELECT
stu.id,
stu.sn,
stu.NAME,
stu.qq_mail,
sco.score,
sco.course_id,
cou.NAME
FROM
student stu
LEFT JOIN score sco ON stu.id = sco.student_id
LEFT JOIN course cou ON sco.course_id = cou.id
ORDER BY
stu.id;
+----+-------+-----------------+------------------+-------+-----------+--------------------+
| id | sn | name | qq_mail | score | course_id | name |
+----+-------+-----------------+------------------+-------+-----------+--------------------+
| 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 70.5 | 1 | Java |
| 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 98.5 | 3 | 计算机原理 |
| 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 33.0 | 5 | 高阶数学 |
| 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 98.0 | 6 | 英文 |
| 2 | 00835 | 菩提老祖 | NULL | 60.0 | 1 | Java |
| 2 | 00835 | 菩提老祖 | NULL | 59.5 | 5 | 高阶数学 |
| 3 | 00391 | 白素贞 | NULL | 33.0 | 1 | Java |
| 3 | 00391 | 白素贞 | NULL | 68.0 | 3 | 计算机原理 |
| 3 | 00391 | 白素贞 | NULL | 99.0 | 5 | 高阶数学 |
| 4 | 00031 | 许仙 | xuxian@qq.com | 67.0 | 1 | Java |
| 4 | 00031 | 许仙 | xuxian@qq.com | 23.0 | 3 | 计算机原理 |
| 4 | 00031 | 许仙 | xuxian@qq.com | 56.0 | 5 | 高阶数学 |
| 4 | 00031 | 许仙 | xuxian@qq.com | 72.0 | 6 | 英文 |
| 5 | 00054 | 不想毕业 | NULL | 81.0 | 1 | Java |
| 5 | 00054 | 不想毕业 | NULL | 37.0 | 5 | 高阶数学 |
| 6 | 51234 | 好好说话 | say@qq.com | 56.0 | 2 | 中国传统文化 |
| 6 | 51234 | 好好说话 | say@qq.com | 43.0 | 4 | 语文 |
| 6 | 51234 | 好好说话 | say@qq.com | 79.0 | 6 | 英文 |
| 7 | 83223 | tellme | NULL | 80.0 | 2 | 中国传统文化 |
| 7 | 83223 | tellme | NULL | 92.0 | 6 | 英文 |
| 8 | 09527 | 老外学中文 | foreigner@qq.com | NULL | NULL | NULL |
+----+-------+-----------------+------------------+-------+-----------+--------------------+
4.2.3 自连接
自连接是指在同一张表连接自身进行查询。
案例:
显示所有“计算机原理”成绩比“Java”成绩高的成绩信息
-- 先查询“计算机原理”和“Java”课程的id
select id,name from course where name='Java' or name='计算机原理';
+----+-----------------+
| id | name |
+----+-----------------+
| 1 | Java |
| 3 | 计算机原理 |
+----+-----------------+
-- 再查询成绩表中,“计算机原理”成绩比“Java”成绩 好的信息
select
s1.*
from
score s1, score s2
where
s1.student_id = s2.student_id
and s1.score < s2.score
and s1.course_id = 1
and s2.course_id = 3;
+-------+------------+-----------+
| score | student_id | course_id |
+-------+------------+-----------+
| 70.5 | 1 | 1 |
| 33.0 | 3 | 1 |
+-------+------------+-----------+
-- 也可以使用join on 语句来进行自连接查询
select
s1.*
from
score s1
join score s2 on s1.student_id = s2.student_id
and s1.score < s2.score
and s1.course_id = 1
and s2.course_id = 3;
+-------+------------+-----------+
| score | student_id | course_id |
+-------+------------+-----------+
| 70.5 | 1 | 1 |
| 33.0 | 3 | 1 |
+-------+------------+-----------+
以上查询只显示了成绩信息,并且是分布执行的。要显示学生及成绩信息,并在一条语句显示:
select
stu.*,
s1.score Java,
s2.score 计算机原理
from
score s1
join score s2 on s1.student_id = s2.student_id
join student stu on s1.student_id = stu.id
join course c1 on s1.course_id = c1.id
join course c2 on s2.course_id = c2.id
and s1.score < s2.score
and c1.name = 'Java'
and c2.name = '计算机原理';
+----+-------+-----------------+-----------------+------------+------+-----------------+
| id | sn | name | qq_mail | classes_id | Java | 计算机原理 |
+----+-------+-----------------+-----------------+------------+------+-----------------+
| 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 1 | 70.5 | 98.5 |
| 3 | 00391 | 白素贞 | NULL | 1 | 33.0 | 68.0 |
+----+-------+-----------------+-----------------+------------+------+-----------------+
4.2.4 子查询
子查询是指嵌入在其他sql语句中的select语句,也叫嵌套查询
- 单行子查询:返回一行记录的子查询
-- 查询与“不想毕业” 同学的同班同学:
select * from
student
where
classes_id=(select classes_id from student where name='不想毕业');
+----+-------+-----------------+-----------------+------------+
| id | sn | name | qq_mail | classes_id |
+----+-------+-----------------+-----------------+------------+
| 1 | 09982 | 黑旋风李逵 | xuanfeng@qq.com | 1 |
| 2 | 00835 | 菩提老祖 | NULL | 1 |
| 3 | 00391 | 白素贞 | NULL | 1 |
| 4 | 00031 | 许仙 | xuxian@qq.com | 1 |
| 5 | 00054 | 不想毕业 | NULL | 1 |
+----+-------+-----------------+-----------------+------------+
- 多行子查询:返回多行记录的子查
-- 查询“语文”或“英文”课程的成绩信息
-- 1. [NOT] IN关键字:
-- 使用IN
select * from score where course_id in (select id from course where
name='语文' or name='英文');
-- 使用 NOT IN
select * from score where course_id not in (select id from course where
name!='语文' and name!='英文');
+-------+------------+-----------+
| score | student_id | course_id |
+-------+------------+-----------+
| 43.0 | 6 | 4 |
| 98.0 | 1 | 6 |
| 72.0 | 4 | 6 |
| 79.0 | 6 | 6 |
| 92.0 | 7 | 6 |
+-------+------------+-----------+
-- 可以使用多列包含
-- 插入重复的分数:score, student_id, course_id列重复
insert into score(score, student_id, course_id) values
-- 黑旋风李逵
(70.5, 1, 1),(98.5, 1, 3),
-- 菩提老祖
(60, 2, 1);
-- 查询重复的分数
select * from score
where
(score, student_id, course_id)
in
(select
score, student_id, course_id
from score
group by
score, student_id, course_id
having count(0) > 1);
+-------+------------+-----------+
| score | student_id | course_id |
+-------+------------+-----------+
| 70.5 | 1 | 1 |
| 98.5 | 1 | 3 |
| 60.0 | 2 | 1 |
| 70.5 | 1 | 1 |
| 98.5 | 1 | 3 |
| 60.0 | 2 | 1 |
+-------+------------+-----------+
-- 2. [NOT] EXISTS关键字:
-- 使用 EXISTS
select * from score sco
where
exists
(select sco.id from
course cou
where
(name='语文' or name='英文')
and cou.id = sco.course_id);
-- 使用 NOT EXISTS
select * from score sco
where
not exists
(select sco.id from
course cou
where
(name!='语文' and name!='英文')
and cou.id = sco.course_id);
+----+-------+------------+-----------+
| id | score | student_id | course_id |
+----+-------+------------+-----------+
| 17 | 43.0 | 6 | 4 |
| 4 | 98.0 | 1 | 6 |
| 13 | 72.0 | 4 | 6 |
| 18 | 79.0 | 6 | 6 |
| 20 | 92.0 | 7 | 6 |
+----+-------+------------+-----------+
- 在from子句中使用子查询:子查询语句出现在from子句中。这里要用到数据查询的技巧,把一个子查询当做一个临时表使用。
-- 查询所有比“中文系2019级3班”平均-- 获取“中文系2019级3班”的平均分,将其看作临时表
select
avg(sco.score) as score
from
score sco
join student stu on sco.student_id = stu.id
join classes cls on stu.classes_id = cls.id
where cls.name = '中文系2019级3班';
+----------+
| score |
+----------+
| 70.00000 |
+----------+
-- 查询成绩表中,比以上临时表平均分高的成绩:
select * from
score sco,
(select
avg(sco.score) as score
from
score sco
join student stu on sco.student_id = stu.id
join classes cls on stu.classes_id = cls.id
where cls.name = '中文系2019级3班') tmp
where sco.score > tmp.score;
+----+-------+------------+-----------+----------+
| id | score | student_id | course_id | score |
+----+-------+------------+-----------+----------+
| 1 | 70.5 | 1 | 1 | 70.00000 |
| 2 | 98.5 | 1 | 3 | 70.00000 |
| 4 | 98.0 | 1 | 6 | 70.00000 |
| 9 | 99.0 | 3 | 5 | 70.00000 |
| 13 | 72.0 | 4 | 6 | 70.00000 |
| 14 | 81.0 | 5 | 1 | 70.00000 |
| 18 | 79.0 | 6 | 6 | 70.00000 |
| 19 | 80.0 | 7 | 2 | 70.00000 |
| 20 | 92.0 | 7 | 6 | 70.00000 |
+----+-------+------------+-----------+----------+
4.2.5 合并查询
在实际应用中,为了合并多个select的执行结果,可以使用集合操作符 union,union all。使用UNION和UNION ALL时,前后查询的结果集中,字段需要一致。
- union
该操作符用于取得两个结果集的并集。当使用该操作符时,会自动去掉结果集中的重复行。
案例:查询id小于3,或者名字为“英文”的课程:
select * from course where id < 3
union
select * from course where name = '英文';
+----+--------------------+
| id | name |
+----+--------------------+
| 1 | Java |
| 2 | 中国传统文化 |
| 6 | 英文 |
+----+--------------------+
- union all
该操作符用于取得两个结果集的并集。当使用该操作符时,不会去掉结果集中的重复行。
案例:查询id小于3,或者名字为“Java”的课程
select * from course where id < 3
union all
select * from course where name = 'Java';
+----+--------------------+
| id | name |
+----+--------------------+
| 1 | Java |
| 2 | 中国传统文化 |
| 1 | Java |
+----+--------------------+
5. 内容重点总结
- 数据库约束
- 表的关系
1.一对一:
2.一对多:
3.多对多:需要创建中间表来映射两张表的关系 - 新增
INSERT INTO table_name [(column [, column ...])] SELECT ...
- 查询
1.聚合函数:MAX、MIN、AVG、COUNT、SUM
2.分组查询:GROUP BY… HAVING …
3.内连接
select ... from 表1,表2 where 条件
-- inner可以缺省
select ... from 表1 join 表2 on 条件 where 其他条件
4.外连接
select ... from 表1 left/right join 表2 on 条件 where 其他条件
5.自连接
select ... from 表1,表1 where 条件
select ... from 表1 join 表1 on 条件
6.子查询
-- 单行子查询
select ... from 表1 where 字段1 = (select ... from ...);
-- [NOT] IN
select ... from 表1 where 字段1 in (select ... from ...);
-- [NOT] EXISTS
select ... from 表1 where exists (select ... from ... where 条件);
-- 临时表:form子句中的子查询
select ... from 表1, (select ... from ...) as tmp where 条件
7.合并查询
-- UNION:去除重复数据
select ... from ... where 条件
union
select ... from ... where 条件
-- UNION ALL:不去重
select ... from ... where 条件
union all
select ... from ... where 条件
-- 使用UNION和UNION ALL时,前后查询的结果集中,字段需要一致
SQL查询中各个关键字的执行先后顺序: from > on> join > where > group by > with > having >
select > distinct > order by > limit