文章目录
一、前言 & 准备工作
Hello大家好,本文要为大家带来的是有关SQL的高级查询,在上一文学习了一些 基础语法 后,我们应该更进一步地去学习一些复杂性的SQL查询语句,提升自己写SQL的能力💪
二、简易高级查询
⚙ 准备工作
在做高级查询之前,我们先来做一些准备工作
- 我们来新建一个教学管理
jxgl
数据库
createdatabase jxgl;
- 然后为了简易起见,就不创建教师表了,主要涉及以下的三个表,SQL语句供读者使用:
-- Table: CourseCREATETABLE Course
(
cno char(7)NOTNULL,
cname char(40)NULL,
cpno char(7)NULL,
ccredit smallintNULL,PRIMARYKEY( cno ));-- Table: StudentCREATETABLE Student
(
sno char(7)NOTNULL,
sname char(8)NOTNULL,
class char(20)NULL,
ssex char(2)NULL,
bday datetimeNULL,
bplace char(10)NULL,
mgrade smallintNULL,
photo char(50)NULL,
sfzh char(18)NULL,
zxf smallintNULL,PRIMARYKEY( sno ));-- Table: SCCREATETABLE SC
(
term smallintNOTNULL,
sno char(7)NOTNULL,
cno char(7)NOTNULL,
grade smallintNULL,pointdecimal(2,1)NULL,PRIMARYKEY(term,sno,cno),FOREIGNKEY(sno)REFERENCES Student (sno),FOREIGNKEY(cno)REFERENCES Course (cno)ONUPDATECASCADE);
⇒ 然后我们还需要再去插入一些数据(具体见文末的整体SQL代码)
1、查询每一门课程及其先修课程
查询每一门课程及其先修课程,查询结果中显示课程号、课程名和先修课程名
- 首先我们来看第一个简易的高级SQL查询,提到了需要我们去查询每一门课程及其先修课程,那查询每一门课很简单,但是要去查询这门课的先修课程的话就需要有些技巧了
- 因为从上面所定义的表结构来看,我们只能查询到先修课程的 课程号,但是 课程名 却无法直接知晓。所以我们要先去查询
先修课程号
,然后再对其进行转换 - 而先修课程号作为一门课程,它只不过是 course表中的另外一条记录,需要两个course表(别名分别取A、B)做一个等值连接
我们可以从下面这张图来进行查看,那当我们有两张临时表时,查询的工作就很好做了,使用A表的
cpno
去B表中进行定位,便可以找到那个课程号
select A.cno, A.cname, B.cno from course A, course B where A.cpno = B.cno;
- 我们来看看执行结果,发现成功了!
2、查询和“刘涛”在一个班级的学生的信息
查询和“刘涛”在一个班级的学生的信息
面对这么一个查询,你首先会想到什么呢?
⇒ 如果是我的话,我会首先想到:既然要查询和“刘涛”在一个班级的学生的信息,那总得知道刘涛是哪个班级的吧😎
- 这个SQL语句相信大家如果学习过基本的SQL查询一定都会使用
select class from student where sname ='刘涛'
- 那在知晓了班级后,就可以根据这个班级去找到其他学生的信息了,这里我们使用到的是
SQL中的嵌套查询
select*from student where class in(select class from students where sname ='刘涛');
或者我们可以换个思路,使用自连接的形式来做
- 那在这里我们就需要两张表了,就和第一题差不多。我们可以将条件都写在
where
子句中,使用【class】这个字段来连接两个临时表
select s1.*from student s1,student s2
where s1.class = s2.class and s2.sname ='刘涛';
最后我们来看一下执行结果:
3、查询选修了‘计算机基础’课的学生的学号、姓名
查询选修了‘计算机基础’课的学生的学号、姓名
- 还是老样子,我们得先来分析一下,题目要我们去查询 选修了‘计算机基础’课的学生的学号、姓名,首先我们要考虑的是:这个查询需要我们显示哪几个字段?涉及哪几张表?
③ 连接方式
- 很明显,因为
“计算机基础”
这门课程,所以我们需要【course表】,又因为要知道是哪个学生选修了,所以还需要涉及到【sc表】,最后,因为还需要显示选修了这门课的学生的学号、姓名,所以还需要涉及【student表】
我们可以根据三个表的
sno
、
cno
来进行相连
select student.sno, sname from student, sc, course
where student.sno = sc.sno and sc.cno = course.cno and cname ='计算机基础';
我们来看看执行结果:
④ 子查询嵌套方式
然后我们再来看一种:子查询嵌套方式。这种方法就需要考察大家的逻辑嵌套思维
- 首先最内层的查询应该是通过
“计算机基础”
去获取到这门课的课程号,那么
select cno from course where cname ='计算机基础';
- 但找到这个
cno
之后,我们就可以根据这个字段去【sc】表里找那些选修了这门课的学生,然后返回它们的学生学号
select sno from sc where cno in(select cno from course where cname ='计算机基础');
- 最后我们再通过这个学号去【student表】中找到对应的那个学生,然后输出其学号与姓名
select sno,sname from student where sno in(select sno from sc where cno in(select cno from course where cname ='计算机基础'));
通过执行结果我们可以看出最后都是一样的
4、查询没有选修课程的学生的信息
首先一样先分析一下当前查询需要涉及到的表,很明显查询的语句中写
选修课程
、
学生信息
这两块,那我们就需要涉及【student】、【sc】表
① 子查询in方式
- 那首先的话我们拿子查询in的方式来展开,先去这个【sc表】里以去重的形式寻找选修了课程的学生学号
selectdistinct sno from sc
- 那题目要求我们查询的是 没有选修课程的学生的信息,所以此刻我们要使用
not in
比较合适一些
select*from student where sno notin(selectdistinct sno from sc
);
可以发现我们查询到了很多的记录
② 子查询exists方式
- 再介绍一种方式,也就是我们所学习过的
exists
这个谓词,每次取到外层循环中这个学生的学号sno
,判断其是否在【sc】表中出现过,使用not exists
就是去找没有出现过的那个学生学号
select*from student wherenotexists(select*from sc where sno = student.sno
);
来看下运行结果就可以看出它们的效果是等同的
5、查询每个学生超过该门课程平均成绩的学号, 课号
查询每个学生超过该门课程平均成绩的学号,课号
- 首先一样来分析一下,此查询涉及了哪张表?通过
成绩、学号、课号
可以看出只涉及到了【sc】表中的字段
那在考虑本查询之前呢,我们先来考虑一个简单的问题
根据cno分组,求每门课的平均成绩
- 对于这个查询只要有学习过基础的SQL语法,那相信都难不倒大家,那既然要求平均成绩,那这里就会用到一个聚合函数叫做
avg()
select cno,avg(grade)from sc groupby cno
- 好,接下去的话我们就按照题目的要求来,要我们求的是超过该门课的平均成绩,所以我们可以沿用上面的思想。在【sc表】中查询相关学生信息的时候
select sno, cno from sc as sc1 where grade >(selectavg(grade)as avg_grade from sc groupby cno having sc1.cno = cno
);
来看看执行结果可以看出,确实按照每个学生的学号显示出了它们超过平均成绩的课程号
6、查询既选修了“计算机基础”又选修了“C语言基础”的学生的学号
查询既选修了“计算机基础”又选修了“C语言基础”的学生的学号
① 自连接
- 从题目本身我们可以看出,这里有两个限制条件,一个是选修
“计算机基础”
、另一个则是“C语言基础”
,那在这里的话我们就可以去定义出两张有关【sc表】的临时表,然后通过自连接的方式将他们相连即可
select sc1.sno from sc sc1, sc sc2
where sc1.sno = sc2.sno
and sc1.cno =(select cno from course where cname ='计算机基础')and sc2.cno =(select cno from course where cname ='C语言基础');
通过运行来看,就是有那么一位天之骄子👼既选了
“计算机基础”
又选了
“C语言基础”
② 求交集
- 第二种方法的话则是通过求交集的方式来找到最后的结果。意思是说,我们可以用关系逻辑代数中的
intersect
来拼接下面两个SQL
select sno from sc,course where sc.cno = course.cno and cname ='计算机基础'
select sno from sc,course where sc.cno = course.cno and cname ='C语言基础'
不过在MySQL中可没有
intersect
这个关键字,读者如果想继续研究的话可以用
inner join
来实现~
③ 子查询
- 最后我们再来看一种查询方式 means 【子查询】,具体SQL如下
select sno from sc,course where
sc.cno = course.cno and cname ='计算机基础'and sno in(select sno from sc, course where sc.cno = course.cno and cname ='C语言基础');
三、复杂高级查询
看完了简易的高级查询后,我们再来看看一些复杂的高级查询🎈
⚙ 准备工作
- 同样,在做复杂高级查询之前我们也需要去做一些简易的查询工作,先创建一个专门存放我们进行高级查询的数据库
createdatabase ad_select;
- 接下去我们要创建几张数据表来承载相应的数据,分别是 学生信息表
student
、课程信息表course
、学生选课信息表sc
-- 学生信息表createtable student(
sno intprimarykey,
sname varchar(15));-- 课程信息表createtable course(
cno intprimarykey,
cname varchar(15));-- 学生选课信息表createtable sc(
sno int,
cno int,
grade int,foreignkey(sno)references student (sno),foreignkey(cno)references course (cno));
- 接下去呢我们再往数据表中插入一些数据来进行观察
-- 插入数据insertinto student values(1,'张三');insertinto student values(2,'李四');insertinto student values(3,'王五');insertinto student values(4,'马六');insertinto course values(1,'数据库');insertinto course values(2,'离散数学');insertinto course values(3,'操作系统');insertinto sc values(1,1,78);insertinto sc values(1,2,87);insertinto sc values(2,1,66);insertinto sc values(3,2,80);insertinto sc values(3,3,56);insertinto sc values(4,3,86);insertinto sc values(4,1,90);insertinto sc values(1,3,77);insertinto sc values(4,2,81);insertinto sc values(2,2,83);
- 然后来看看数据是否被插入进去了
1、查询选修了全部课程的学生号码
首先我们可以写出它的 关系代数表达式:
πSno, Cno(SC)÷ πCno(C)
那它的SQL语句应该怎么实现呢?
- 有些同学一开始可能想到了这样的写法,去【course表】中找到那些课程的课程号,若是【sc表】中有这些课程号的话就输出这个课程号所对应的学生号
select sno from sc where cno in(select cno from course
);
通过执行结果去查看就可以发现每当条件成立的时候,就会对应地将这个课程号是哪个学生所选的学生号给打印出来,那这个逻辑其实就已经错误了,我们要找的是 选修了全部课程的学生学号,而不是只选修了一门或者多门课程的学生学号
💬 那有同学问:这该怎么办呢?
- 其实这并不难,本题的着手点就在于这个【全部课程】,那我们就需要先获取到全部的课程有多少
selectcount(*)from c -- 得到全部课程数3
- 那当我们知晓了全部的课程数后,就可以根据每个学生的学号去做分组统计,看看每个学生到底选修了那些课程
select sno,count(*)from sc groupby sno
- 那很简单,将上面两个SQL做个合并即可,当我们根据学号统计完后,再使用
having子句
去做一个筛选即可
select sno,count(*)from sc groupby sno havingcount(*)=(selectcount(*)from c);
- 执行一下结果我们可以看到,有两个学生选修了全部课程,所以他们的课程代码就被打印出来了
2、查询被全部学生选修的课程号码
首先我们可以写出它的 关系代数表达式:
πCno, Sno(SC)÷ πsno(S)
- 本题还是一样我们要先去找到突破口,即这个【全部学生】,我们去S表中进行统计即可,然后可以得到总的学生人数为 4 人
selectcount(*)from student;
- 然后我们再按课号去进行统计,观察当前这课程有多少人进行了选修
select cno,count(*)from sc groupby cno
- 最后还是一样,将上面二者进行一个拼接
select cno,count(*)from sc groupby cno havingcount(*)=(selectcount(*)from student);
执行结果,就可以看到课程号为2号的选课情况
3、查询至少选修了学号是2号的学生所选修的全部课程的学生号码【⭐】
如果你觉得上面两个只是小case的话,那就再来看看这题吧
同样,我们先写出它的 关系代数表达式:
πCno, Sno(SC)÷ π(σsno='2' (SC))
- 可能很多同学一上来就会想到这样的写法:
select sno from sc where cno in(select cno from sc where sno ='2');
- 然后我们去看一下执行结果发现属实是有些混乱了,有些只找到一门2号同学所学过的课程就停下来了
💡 此时的我灵机一动,想到了找
exists
这个谓词来帮忙,请读者先观看下面的这个SQL语句
notexists(select*from sc as y where sno ='2'andnotexists(select*from sc where sno = h and cno = y.cno
))
- 仔细观察可以发现,我在这里使用到了两个
not exists
,这指的就是我在小标题中所讲到的双重否定,这句SQL的语义是:不存在这样的课程,学号为2号的学生选了,而学号为h的学生却没有选
可能有的读者一时半会接收不了,没事,下面我会一一地进行分层解说
Way1:exists表双重否定
- 以下就是我们本题的SQL正解
selectdistinct sno from sc as x wherenotexists(select*from sc as y where sno ='2'andnotexists(select*from sc where sno = x.sno and cno = y.cno
));
马上我们就来一一解析一下🔍
- 首先是最外层的这个临时表x的建立,代表的是 我们要去定位的那个同学的学号
selectdistinct sno from sc as x
- 接下去第二层这个临时表y的建立,代表的是 我们要去定位的那个2号同学
select*from sc as y where sno ='2'
- 相信阅读本文的读者多多少少有些编程基础,那我们就可以来做这样一个比喻:把外层的两个查询看做是 两个嵌套的for循环,而下面的这个查询则可以看作是 if条件判断- 对于
sno = x.sno
这个条件对应的是第一个查询,依次拿到当前学生的学号进行判断- 对于cno = y.cno
这个条件对应的是第二个查询,获取到学号为2号学生的课号进行判断
select*from sc where sno = x.sno and cno = y.cno
执行一下SQL语句我们可以看到找出了那几个至少学过了学号为2号同学所学课程的学生号
Way2:优先筛选group统计
看完了上面这种解法后,不知读者理解了多少,我们再来看一种解法
- 对于此种方法,我的思路是分别去【sc表】中做两次的查找,临时表x代表的是要搜寻的那些学生,而临时表y代表的则是学号为2号的学生,
where
子句后面跟的就是课程号相等的情况。
那么最后查询出来的结果就如下所示,筛选掉了那些2号同学没学习过的课程:
具体的SQL语句如下
select*from sc as x ,(select*from sc where sno ='2')as y where x.cno = y.cno
- 那有了上面的这个思想之后,我们再去想出当前这个问题的解决方案就简单很多了,以下是具体的SQL语句
select x.sno from sc as x ,(select*from sc where sno ='2')as y
where x.cno = y.cno groupby x.sno
havingcount(*)=(selectcount(*)from sc where sno ='2');
💬 我们来分析一下吧:
- 可以看到,我在上面上面这个语句的基础上加了一个
group by
子句,代表将查询出来的记录即哪个学生选了哪门课按照每一位学生的学号来进行统计
groupby x.sno
- 最后还有一个
having
子句代表的则是在统计完成之后再去做一个筛选的工作,筛选出所选课程的数量和2号同学所选课程数一致的学生,最后输出的便是这些学生的学号
havingcount(*)=(selectcount(*)from sc where sno ='2');
最后来看一下执行结果:
Way3:最牛的简易除法🐂
不过上面的这一些算不得什么,下面我来介绍一个更加奇妙的写法,是从一位【高级工程师】那里学来的
读者可以先看看下面这句SQL(利用
exists
和
except
相结合)
select*from s wherenotexists(select cno from sc where sno ='2'exceptselect cno from sc where sno = s.sno
);
💬 好,我们马上来分析一下:
- 内部的
where
子句相信读者在认真看下来之后应该很熟悉了,这里的s.sno
可以看作是我们写循环时的循环变量i
,它是每一轮循环都会发生变化的,即一直在做判断看哪个学号的学生是符合条件的
where sno = s.sno
- 这里的 except 意思是 第一个查询结果中排除第二个查询结果中存在的行。那么下面两句你就可以理解为 从学号为2号的学生所选的课程中除去当前这个学生所选的课程
select cno from sc where sno ='2'exceptselect cno from sc where sno = s.sno
- 那从整体来看,上面的这个查询是包在一个
not exists
的谓词中,还记得我们在第一个方法中所谈到过的【exists表双重否定】吗?这里其实也有同样的含义,既然我们要找到 至少选修了学号是2号的学生所选修的全部课程的学生,就需要让我们所查询出来的内容是一个空集才可以
select*from s wherenotexists(select cno from sc where sno ='2'exceptselect cno from sc where sno = s.sno
);
- 就上面这样说的话太抽象了,我们可以画个图来理解一下,上面的
except
子句指的就是右边 - 左边,如果我们得到的是空集的话,则代表学号为h的那位同学一定学习了学号为2号同学所学的全部课程。当返回结果后再根据not exists
双重否定表肯定,那么这个学生就是符合条件的那一个
以上就是本文所要涉及的全部高级查询,你学废了吗︿( ̄︶ ̄)︿
四、SQL数据展示(自取)
-- 注意:需要先建立一个空的数据库,然后再执行本脚本 !!!-- 本来可以加这句 create database jxgl ,但创建库后需要刷新,否则执行下面的语句失败use jxgl;-- Table: CourseCREATETABLE Course
(
cno char(7)NOTNULL,
cname char(40)NULL,
cpno char(7)NULL,
ccredit smallintNULL,PRIMARYKEY( cno ));INSERTINTO Course (cno, cname, cpno, ccredit)VALUES('0000001','DB_Design ','0000006',4);INSERTINTO Course (cno, cname, cpno, ccredit)VALUES('0000002','C语言基础 ','0000027',8);INSERTINTO Course (cno, cname, cpno, ccredit)VALUES('0000003','UNIX ','0000013',5);INSERTINTO Course (cno, cname, cpno, ccredit)VALUES('0000004','C#程序设计 ','0000002',6);INSERTINTO Course (cno, cname, cpno, ccredit)VALUES('0000005','现代物流概论 ',null,4);INSERTINTO Course (cno, cname, cpno, ccredit)VALUES('0000006','数据库原理 ','0000010',6);INSERTINTO Course (cno, cname, cpno, ccredit)VALUES('0000007','JAVA程序设计 ','0000002',8);INSERTINTO Course (cno, cname, cpno, ccredit)VALUES('0000008','电子商务 ','0000027',4);INSERTINTO Course (cno, cname, cpno, ccredit)VALUES('0000009','实用英语 ',null,6);INSERTINTO Course (cno, cname, cpno, ccredit)VALUES('0000010','数据结构 ','0000002',4);INSERTINTO Course (cno, cname, cpno, ccredit)VALUES('0000011','邓小平理论 ',null,2);INSERTINTO Course (cno, cname, cpno, ccredit)VALUES('0000012','体育 ',null,4);INSERTINTO Course (cno, cname, cpno, ccredit)VALUES('0000013','操作系统 ','0000002',4);INSERTINTO Course (cno, cname, cpno, ccredit)VALUES('0000014','经济基础知识 ',null,4);INSERTINTO Course (cno, cname, cpno, ccredit)VALUES('0000027','计算机基础 ',null,4);INSERTINTO Course (cno, cname, cpno, ccredit)VALUES('0000032','多媒体技术 ','0000027',5);INSERTINTO Course (cno, cname, cpno, ccredit)VALUES('0000034','高等数学 ',null,6);INSERTINTO Course (cno, cname, cpno, ccredit)VALUES('0000039','基础会计 ',null,2);INSERTINTO Course (cno, cname, cpno, ccredit)VALUES('0000045','软件工程 ','0000010',4);INSERTINTO Course (cno, cname, cpno, ccredit)VALUES('0000052','财务会计 ','0000039',4);-- Table: StudentCREATETABLE Student
(
sno char(7)NOTNULL,
sname char(8)NOTNULL,
class char(20)NULL,
ssex char(2)NULL,
bday datetimeNULL,
bplace char(10)NULL,
mgrade smallintNULL,
photo char(50)NULL,
sfzh char(18)NULL,
zxf smallintNULL,PRIMARYKEY( sno ));INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0301101','陈红 ','03计算应用1 ','女','1982-12-2','宁波 ',400,null,'330102198212020021',17);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0301102','黄圣依 ','03计算应用1 ','女','1983-6-9','杭州 ',325,null,'330102198306090020',16);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0301103','刘涛 ','03计算应用1 ','女','1982-9-18','绍兴 ',311,null,'330102820918182 ',15);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0301104','宁静 ','03计算应用1 ','女','1983-3-10','温州 ',298,null,'330104830310163 ',20);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0301105','许晴 ','03计算应用1 ','女','1983-6-24','温州 ',367,null,'330105830624004 ',20);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0301106','黎明 ','03计算应用1 ','男','1983-3-15','台州 ',412,null,'330122198303152826',20);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0301107','古天乐 ','03计算应用1 ','男','1982-1-19','宁波 ',351,null,'330124198201191421',25);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0301108','林志颖 ','03计算应用1 ','男','1981-9-23','宁波 ',326,null,'330203198109230655',28);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0301109','陈奕迅 ','03计算应用1 ','男','1982-6-25','杭州 ',361,null,'330203198206252418',22);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0301110','徐若萱 ','03计算应用1 ','女','1982-7-9','宁波 ',376,null,'330203198207090617',22);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0301111','陈冠希 ','03计算应用1 ','男','1981-3-21','杭州 ',401,null,'330203810321003 ',22);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311101','赵薇 ','03物流1 ','女','1982-2-11','台州 ',289,null,'330203820211092 ',16);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311102','董洁 ','03物流1 ','女','1982-2-17','金华 ',378,null,'330203820217001 ',24);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311103','王力宏 ','03物流1 ','男','1982-5-31','温州 ',361,null,'330203820531002 ',24);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311104','李嘉欣 ','03物流1 ','女','1981-5-28','宁波 ',287,null,'330204198105281056',24);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311105','苏有朋 ','03物流1 ','男','1982-4-16','宁波 ',372,null,'330204198204162036',24);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311106','夏雨 ','03物流1 ','男','1982-10-12','绍兴 ',384,null,'330204198210121046',16);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311107','郭富城 ','03物流1 ','男','1982-10-17','台州 ',343,null,'330204198210173022',16);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311108','袁咏仪 ','03物流1 ','女','1981-11-16','杭州 ',376,null,'330204811116101 ',18);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311109','张柏芝 ','03物流1 ','女','1982-3-29','温州 ',421,null,'330204820329201 ',18);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311110','张信哲 ','03物流1 ','男','1982-7-14','宁波 ',408,null,'330204820714502 ',24);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311111','陈坤 ','03物流1 ','男','1982-7-19','宁波 ',326,null,'330204820719604 ',24);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311201','王祖贤 ','03物流2 ','女','1982-7-25','绍兴 ',337,null,'330204820725301 ',20);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311202','佟大为 ','03物流2 ','男','1982-6-7','金华 ',322,null,'330205198206070617',22);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311203','谢霆锋 ','03物流2 ','男','1982-11-2','杭州 ',364,null,'330205198211020964',22);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311205','胡军 ','03物流2 ','男','1981-7-16','宁波 ',316,null,'330206198107163128',22);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311206','陈红 ','03物流2 ','女','1981-11-9','金华 ',327,null,'330206198111095710',20);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311207','蒋勤勤 ','03物流2 ','女','1982-1-9','杭州 ',424,null,'330206198201094616',24);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311208','吴彦祖 ','03物流2 ','男','1982-6-1','金华 ',297,null,'330206198206013416',28);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311209','刘德华 ','03物流2 ','男','1982-10-31','宁波 ',281,null,'330206198210313462',28);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311210','伊能静 ','03物流2 ','女','1983-1-16','金华 ',356,null,'330206198301161425',28);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311211','李亚鹏 ','03物流2 ','男','1981-11-10','湖州 ',288,null,'330206811110312 ',21);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311301','刘烨 ','03物流3 ','男','1981-12-23','宁波 ',293,null,'330206811223462 ',18);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311302','周迅 ','03物流3 ','女','1976-8-7','台州 ',357,null,'330206820201141 ',18);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311303','朱茵 ','03物流3 ','女','1982-2-14','湖州 ',381,null,'330206820214091 ',19);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311304','刘嘉玲 ','03物流3 ','女','1982-7-18','绍兴 ',405,null,'330206820718142 ',25);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311305','周杰 ','03物流3 ','男','1982-8-5','温州 ',332,null,'330206820805031 ',28);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311306','李若彤 ','03物流3 ','女','1982-8-9','宁波 ',311,null,'330206820809031 ',25);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311307','陆毅 ','03物流3 ','男','1983-1-20','绍兴 ',367,null,'330203830120184 ',25);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311308','袁莉 ','03物流3 ','女','1982-10-1','宁波 ',325,null,'330203821001094 ',30);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311309','刘亦菲 ','03物流3 ','女','1982-5-26','湖州 ',354,null,'330203820526062 ',24);INSERTINTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf)VALUES('0311310','萧亚轩 ','03物流3 ','女','1982-3-31','温州 ',405,null,'330203820331002 ',20);-- Table: SCCREATETABLE SC
(
term smallintNOTNULL,
sno char(7)NOTNULL,
cno char(7)NOTNULL,
grade smallintNULL,pointdecimal(2,1)NULL,PRIMARYKEY(term,sno,cno),FOREIGNKEY(sno)REFERENCES Student (sno),FOREIGNKEY(cno)REFERENCES Course (cno)ONUPDATECASCADE);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0301101','0000011',88,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0301102','0000011',75,1.5);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0301102','0000027',79,1.5);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311101','0000008',86,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311101','0000009',58,0);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311101','0000011',85,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311101','0000027',87,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311101','0000034',88,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311101','0000039',85,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311101','0000052',63,0);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311102','0000006',61,0);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311102','0000008',78,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311102','0000009',76,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311102','0000011',75,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311102','0000027',84,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311102','0000034',78,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311102','0000052',72,1.5);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311111','0000008',75,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311111','0000009',74,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311111','0000011',95,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311111','0000027',82,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311111','0000034',72,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311111','0000039',66,0);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311111','0000052',78,1.5);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311201','0000008',68,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311201','0000009',81,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311201','0000011',78,1.5);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311201','0000027',84,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311201','0000034',64,0);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311201','0000039',77,1.5);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311201','0000052',71,2);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311202','0000008',78,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311202','0000009',70,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311202','0000011',65,0);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311202','0000027',80,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311202','0000034',79,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311202','0000039',59,0);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311202','0000052',87,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311211','0000006',70,1.5);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311211','0000008',74,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311211','0000009',52,0);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311211','0000011',79,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311211','0000027',75,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311211','0000034',79,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(1,'0311211','0000052',82,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(2,'0301101','0000002',80,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(2,'0301101','0000006',91,1.5);INSERTINTO SC (term, sno, cno, grade,point)VALUES(2,'0301101','0000007',87,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(2,'0301101','0000008',55,0);INSERTINTO SC (term, sno, cno, grade,point)VALUES(2,'0301101','0000010',66,0);INSERTINTO SC (term, sno, cno, grade,point)VALUES(2,'0301101','0000013',90,1.5);INSERTINTO SC (term, sno, cno, grade,point)VALUES(2,'0301101','0000034',81,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(2,'0301102','0000002',110,1.5);INSERTINTO SC (term, sno, cno, grade,point)VALUES(2,'0301102','0000008',83,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(2,'0301102','0000009',76,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(2,'0301102','0000010',78,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(2,'0301102','0000013',82,1);INSERTINTO SC (term, sno, cno, grade,point)VALUES(2,'0301102','0000034',64,0);INSERTINTO SC (term, sno, cno, grade,point)VALUES(3,'0301101','0000001',85,null);INSERTINTO SC (term, sno, cno, grade,point)VALUES(3,'0311211','0000001',80,null);-- Table: TeacherCREATETABLE Teacher
(
tno char(3)NOTNULL,
tname char(8)NULL,
ps char(10)NULL,
wday datetimeNULL,
dept char(16)NULL,
pay smallintNULL,
marry smallintNULL,
resume varchar(200)NULL,PRIMARYKEY( tno ));INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('001','王涛 ','讲师 ','2001-9-1','基础部 ',2100,0,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('002','姚明 ','助教 ','2002-2-3','基础部 ',2100,0,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('003','蔡振华 ','副教授 ','2002-4-10','经管系 ',4800,1,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('004','田亮 ','助教 ','2003-7-10','计算机系 ',2100,0,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('005','李小双 ','讲师 ','2001-9-10','应用技术系 ',2600,1,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('006','孙继海 ','讲师 ','2001-1-20','国际交流系 ',2600,0,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('007','卡佩罗 ','教授 ','2001-5-16','基础部 ',5500,0,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('008','孔令辉 ','助教 ','2003-7-10','基础部 ',2100,0,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('009','刘玉栋 ','助教 ','2001-10-12','基础部 ',2100,0,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('010','隋菲菲 ','助教 ','2002-7-10','国际交流系 ',2100,0,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('011','张蓉芳 ','副教授 ','2001-12-13','基础部 ',4800,1,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('012','李宁 ','讲师 ','2003-10-19','基础部 ',2600,1,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('013','赵蕊蕊 ','助教 ','2003-7-1','计算机系 ',2100,0,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('014','谢军 ','讲师 ','2003-8-18','计算机系 ',2600,1,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('015','刘国梁 ','讲师 ','2003-9-10','基础部 ',2600,0,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('016','李永波 ','副教授 ','2002-8-3','国际交流系 ',2600,1,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('017','郎平 ','副教授 ','2003-3-30','经管系 ',4800,1,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('018','王军霞 ','助教 ','2001-5-19','经管系 ',2100,0,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('019','马晓春 ','讲师 ','2003-10-20','国际交流系 ',2600,1,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('020','章秋红 ','讲师 ','2003-9-1','计算机系 ',2600,1,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('021','罗雪娟 ','助教 ','2000-12-21','经管系 ',2100,0,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('022','聂卫平 ','副教授 ','2001-3-8','应用技术系 ',2600,0,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('023','陈忠和 ','副教授 ','2003-7-1','计算机系 ',2100,1,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('024','葛菲 ','助教 ','2003-7-1','计算机系 ',2100,1,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('025','孙俊 ','助教 ','2002-1-21','基础部 ',2100,1,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('026','邹振先 ','副教授 ','2003-8-20','经管系 ',4800,1,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('027','袁伟民 ','教授 ','2000-8-25','校办 ',5500,1,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('028','乔丹 ','教授 ','2000-10-1','经管系 ',5500,1,null);INSERTINTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume)VALUES('029','许海峰 ','教授 ','2003-2-20','计算机系 ',5500,1,null);-- Table: TCCREATETABLE TC
(
list smallint,
term smallint,
class varchar(40)NULL,
cno char(7)NULL,
tno char(3)NULL,
period smallintNULL,PRIMARYKEY(list),FOREIGNKEY(cno)REFERENCES Course (cno)ONUPDATECASCADE,FOREIGNKEY(tno)REFERENCES Teacher (tno));INSERTINTO TC (list, term, class, cno, tno, period)VALUES(1,1,'03物流1 ','0000011','001',36);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(2,1,'03物流1 ','0000034','002',72);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(3,1,'03物流1 ','0000052','003',60);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(4,1,'03物流1 ','0000027','004',108);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(5,1,'03物流1 ','0000039','005',36);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(6,1,'03物流1 ','0000005','006',72);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(7,1,'03物流1 ','0000001','007',36);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(8,1,'03物流2 ','0000011',null,36);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(9,1,'03物流2 ','0000034','002',72);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(10,1,'03物流2 ','0000052','003',60);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(11,1,'03物流2 ','0000027','004',108);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(12,1,'03物流2 ','0000039','009',36);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(13,1,'03物流2 ','0000005','010',72);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(14,1,'03物流2 ','0000001','007',36);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(15,1,'03计算应用1 ','0000011','022',36);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(16,1,'03计算应用1 ','0000034',null,54);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(17,1,'03计算应用1 ','0000045','013',108);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(18,1,'03计算应用1 ','0000027','014',144);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(19,1,'03计算应用1 ','0000039','015',36);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(20,1,'03计算应用1 ','0000005','016',72);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(21,1,'03计算应用1 ','0000001','007',36);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(22,2,'03物流1 ','0000007','017',108);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(23,2,'03物流1 ','0000012',null,54);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(24,2,'03物流1 ','0000005','019',72);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(25,2,'03物流1 ','0000008','020',108);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(26,2,'03物流1 ','0000032','007',36);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(27,2,'03物流1 ','0000004','021',72);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(28,2,'03物流1 ','0000003','022',36);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(29,2,'03物流2 ','0000007','017',108);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(30,2,'03物流2 ','0000012','018',54);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(31,2,'03物流2 ','0000005','010',72);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(32,2,'03物流2 ','0000008','020',108);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(33,2,'03物流2 ','0000032','007',36);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(34,2,'03物流2 ','0000004','021',72);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(35,2,'03物流2 ','0000003','022',36);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(36,2,'03计算应用1 ','0000002','023',144);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(37,2,'03计算应用1 ','0000010','014',72);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(38,2,'03计算应用1 ','0000013',null,72);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(39,2,'03计算应用1 ','0000034','025',54);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(40,2,'03计算应用1 ','0000005','016',72);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(41,2,'03计算应用1 ','0000032','007',36);INSERTINTO TC (list, term, class, cno, tno, period)VALUES(42,2,'03计算应用1 ','0000003','001',36);
五、总结与提炼
最后来总结一下本文所学习的内容📖
- 本文我们所涉及的是SQL中的高级查询,从简易的查询入手,在学习了基础的SQL语法后,我们再去看一些略带复杂性的查询就没那么难了,其中不仅涉及
group by
、having
等这些子句,而且涉及in
、exists
这些谓词,有了它们我们在作一些复杂查询的时候也显得没那么难哩! - 接下去又涉及了一些复杂的高级查询,通过逻辑的转换、逐步地分析,发现只要将复杂的查询切分成一个个的逻辑块,然后再将他们一一拼接起来,逻辑上就显得很清楚了
- 只有将所学习过的知识给运用起来,我们才可以对SQL语句做到融汇贯通
版权归原作者 烽起黎明 所有, 如有侵权,请联系我们删除。