Hello大家好,本文要为大家带来的是有关SQL的高级查询,在上一文学习了一些 基础语法 后,我们应该更进一步地去学习一些复杂性的SQL查询语句,提升自己写SQL的能力💪
在做高级查询之前,我们先来做一些准备工作
create database jxgl;
-- Table: Course CREATE TABLE Course ( cno char(7) NOT NULL, cname char(40) NULL, cpno char(7) NULL, ccredit smallint NULL, PRIMARY KEY ( cno ) ); -- Table: Student CREATE TABLE Student ( sno char(7) NOT NULL, sname char(8) NOT NULL, class char(20) NULL, ssex char(2) NULL, bday datetime NULL, bplace char(10) NULL, mgrade smallint NULL, photo char(50) NULL, sfzh char(18) NULL, zxf smallint NULL, PRIMARY KEY ( sno ) ) ; -- Table: SC CREATE TABLE SC ( term smallint NOT NULL, sno char(7) NOT NULL , cno char(7) NOT NULL , grade smallint NULL, point decimal(2, 1) NULL, PRIMARY KEY (term,sno,cno), FOREIGN KEY (sno) REFERENCES Student (sno), FOREIGN KEY (cno) REFERENCES Course (cno) ON UPDATE CASCADE );
⇒ 然后我们还需要再去插入一些数据(具体见文末的整体SQL代码)
查询每一门课程及其先修课程,查询结果中显示课程号、课程名和先修课程名
我们可以从下面这张图来进行查看,那当我们有两张临时表时,查询的工作就很好做了,使用A表的cpno去B表中进行定位,便可以找到那个课程号
select A.cno, A.cname, B.cno from course A, course B where A.cpno = B.cno;
查询和“刘涛”在一个班级的学生的信息
面对这么一个查询,你首先会想到什么呢?
⇒ 如果是我的话,我会首先想到:既然要查询和“刘涛”在一个班级的学生的信息,那总得知道刘涛是哪个班级的吧😎
select class from student where sname = '刘涛'
select * from student where class in ( select class from students where sname = '刘涛' );
或者我们可以换个思路,使用自连接的形式来做
select s1.* from student s1,student s2 where s1.class = s2.class and s2.sname = '刘涛';
最后我们来看一下执行结果:
查询选修了‘计算机基础’课的学生的学号、姓名
我们可以根据三个表的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 = '计算机基础';
select sno from sc where cno in( select cno from course where cname = '计算机基础' );
select sno,sname from student where sno in( select sno from sc where cno in( select cno from course where cname = '计算机基础' ) );
通过执行结果我们可以看出最后都是一样的
首先一样先分析一下当前查询需要涉及到的表,很明显查询的语句中写选修课程、学生信息这两块,那我们就需要涉及【student】、【sc】表
select distinct sno from sc
select * from student where sno not in( select distinct sno from sc );
可以发现我们查询到了很多的记录
select * from student where not exists( select * from sc where sno = student.sno );
来看下运行结果就可以看出它们的效果是等同的
查询每个学生超过该门课程平均成绩的学号,课号
那在考虑本查询之前呢,我们先来考虑一个简单的问题
根据cno分组,求每门课的平均成绩
select cno, avg(grade) from sc group by cno
select sno, cno from sc as sc1 where grade > ( select avg(grade) as avg_grade from sc group by cno having sc1.cno = cno );
来看看执行结果可以看出,确实按照每个学生的学号显示出了它们超过平均成绩的课程号
查询既选修了“计算机基础”又选修了“C语言基础”的学生的学号
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语言基础”
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来实现~
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语言基础' );
看完了简易的高级查询后,我们再来看看一些复杂的高级查询🎈
create database ad_select;
-- 学生信息表 create table student( sno int primary key, sname varchar(15) ); -- 课程信息表 create table course( cno int primary key, cname varchar(15) ); -- 学生选课信息表 create table sc( sno int, cno int, grade int, foreign key (sno) references student (sno), foreign key (cno) references course (cno) );
-- 插入数据 insert into student values (1, '张三'); insert into student values (2, '李四'); insert into student values (3, '王五'); insert into student values (4, '马六'); insert into course values (1, '数据库'); insert into course values (2, '离散数学'); insert into course values (3, '操作系统'); insert into sc values (1, 1, 78); insert into sc values (1, 2, 87); insert into sc values (2, 1, 66); insert into sc values (3, 2, 80); insert into sc values (3, 3, 56); insert into sc values (4, 3, 86); insert into sc values (4, 1, 90); insert into sc values (1, 3, 77); insert into sc values (4, 2, 81); insert into sc values (2, 2, 83);
首先我们可以写出它的 关系代数表达式:πSno, Cno(SC)÷ πCno(C)
那它的SQL语句应该怎么实现呢?
select sno from sc where cno in ( select cno from course );
通过执行结果去查看就可以发现每当条件成立的时候,就会对应地将这个课程号是哪个学生所选的学生号给打印出来,那这个逻辑其实就已经错误了,我们要找的是 选修了全部课程的学生学号,而不是只选修了一门或者多门课程的学生学号
💬 那有同学问:这该怎么办呢?
select count(*) from c -- 得到全部课程数3
select sno, count(*) from sc group by sno
select sno, count(*) from sc group by sno having count(*) = (select count(*) from c);
首先我们可以写出它的 关系代数表达式:πCno, Sno(SC)÷ πsno(S)
select count(*) from student;
select cno,count(*) from sc group by cno
select cno, count(*) from sc group by cno having count(*) = (select count(*) from student);
执行结果,就可以看到课程号为2号的选课情况
如果你觉得上面两个只是小case的话,那就再来看看这题吧
同样,我们先写出它的 关系代数表达式:πCno, Sno(SC)÷ π(σsno='2' (SC))
select sno from sc where cno in ( select cno from sc where sno = '2' );
💡 此时的我灵机一动,想到了找exists这个谓词来帮忙,请读者先观看下面的这个SQL语句
not exists ( select * from sc as y where sno = '2' and not exists ( select * from sc where sno = h and cno = y.cno ) )
可能有的读者一时半会接收不了,没事,下面我会一一地进行分层解说
select distinct sno from sc as x where not exists( select * from sc as y where sno = '2' and not exists ( select * from sc where sno = x.sno and cno = y.cno ) );
马上我们就来一一解析一下🔍
select distinct sno from sc as x
select * from sc as y where sno = '2'
select * from sc where sno = x.sno and cno = y.cno
执行一下SQL语句我们可以看到找出了那几个至少学过了学号为2号同学所学课程的学生号
看完了上面这种解法后,不知读者理解了多少,我们再来看一种解法
那么最后查询出来的结果就如下所示,筛选掉了那些2号同学没学习过的课程:
具体的SQL语句如下
select * from sc as x ,(select * from sc where sno = '2') as y where x.cno = y.cno
select x.sno from sc as x ,(select * from sc where sno = '2') as y where x.cno = y.cno group by x.sno having count(*) = (select count(*) from sc where sno = '2');
💬 我们来分析一下吧:
group by x.sno
having count(*) = (select count(*) from sc where sno = '2');
最后来看一下执行结果:
不过上面的这一些算不得什么,下面我来介绍一个更加奇妙的写法,是从一位【高级工程师】那里学来的
读者可以先看看下面这句SQL(利用exists和except相结合)
select * from s where not exists ( select cno from sc where sno = '2' except select cno from sc where sno = s.sno );
💬 好,我们马上来分析一下:
where sno = s.sno
select cno from sc where sno = '2' except select cno from sc where sno = s.sno
select * from s where not exists ( select cno from sc where sno = '2' except select cno from sc where sno = s.sno );
以上就是本文所要涉及的全部高级查询,你学废了吗︿( ̄︶ ̄)︿
-- 注意:需要先建立一个空的数据库,然后再执行本脚本 !!! -- 本来可以加这句 create database jxgl ,但创建库后需要刷新,否则执行下面的语句失败 use jxgl; -- Table: Course CREATE TABLE Course ( cno char(7) NOT NULL, cname char(40) NULL, cpno char(7) NULL, ccredit smallint NULL, PRIMARY KEY ( cno ) ); INSERT INTO Course (cno, cname, cpno, ccredit) VALUES ('0000001', 'DB_Design ', '0000006', 4); INSERT INTO Course (cno, cname, cpno, ccredit) VALUES ('0000002', 'C语言基础 ', '0000027', 8); INSERT INTO Course (cno, cname, cpno, ccredit) VALUES ('0000003', 'UNIX ', '0000013', 5); INSERT INTO Course (cno, cname, cpno, ccredit) VALUES ('0000004', 'C#程序设计 ', '0000002', 6); INSERT INTO Course (cno, cname, cpno, ccredit) VALUES ('0000005', '现代物流概论 ', null, 4); INSERT INTO Course (cno, cname, cpno, ccredit) VALUES ('0000006', '数据库原理 ', '0000010', 6); INSERT INTO Course (cno, cname, cpno, ccredit) VALUES ('0000007', 'JAVA程序设计 ', '0000002', 8); INSERT INTO Course (cno, cname, cpno, ccredit) VALUES ('0000008', '电子商务 ', '0000027', 4); INSERT INTO Course (cno, cname, cpno, ccredit) VALUES ('0000009', '实用英语 ', null, 6); INSERT INTO Course (cno, cname, cpno, ccredit) VALUES ('0000010', '数据结构 ', '0000002', 4); INSERT INTO Course (cno, cname, cpno, ccredit) VALUES ('0000011', '邓小平理论 ', null, 2); INSERT INTO Course (cno, cname, cpno, ccredit) VALUES ('0000012', '体育 ', null, 4); INSERT INTO Course (cno, cname, cpno, ccredit) VALUES ('0000013', '操作系统 ', '0000002', 4); INSERT INTO Course (cno, cname, cpno, ccredit) VALUES ('0000014', '经济基础知识 ', null, 4); INSERT INTO Course (cno, cname, cpno, ccredit) VALUES ('0000027', '计算机基础 ', null, 4); INSERT INTO Course (cno, cname, cpno, ccredit) VALUES ('0000032', '多媒体技术 ', '0000027', 5); INSERT INTO Course (cno, cname, cpno, ccredit) VALUES ('0000034', '高等数学 ', null, 6); INSERT INTO Course (cno, cname, cpno, ccredit) VALUES ('0000039', '基础会计 ', null, 2); INSERT INTO Course (cno, cname, cpno, ccredit) VALUES ('0000045', '软件工程 ', '0000010', 4); INSERT INTO Course (cno, cname, cpno, ccredit) VALUES ('0000052', '财务会计 ', '0000039', 4); -- Table: Student CREATE TABLE Student ( sno char(7) NOT NULL, sname char(8) NOT NULL, class char(20) NULL, ssex char(2) NULL, bday datetime NULL, bplace char(10) NULL, mgrade smallint NULL, photo char(50) NULL, sfzh char(18) NULL, zxf smallint NULL, PRIMARY KEY ( sno ) ) ; INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0301101', '陈红 ', '03计算应用1 ', '女', '1982-12-2', '宁波 ', 400, null, '330102198212020021', 17); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0301102', '黄圣依 ', '03计算应用1 ', '女', '1983-6-9', '杭州 ', 325, null, '330102198306090020', 16); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0301103', '刘涛 ', '03计算应用1 ', '女', '1982-9-18', '绍兴 ', 311, null, '330102820918182 ', 15); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0301104', '宁静 ', '03计算应用1 ', '女', '1983-3-10', '温州 ', 298, null, '330104830310163 ', 20); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0301105', '许晴 ', '03计算应用1 ', '女', '1983-6-24', '温州 ', 367, null, '330105830624004 ', 20); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0301106', '黎明 ', '03计算应用1 ', '男', '1983-3-15', '台州 ', 412, null, '330122198303152826', 20); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0301107', '古天乐 ', '03计算应用1 ', '男', '1982-1-19', '宁波 ', 351, null, '330124198201191421', 25); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0301108', '林志颖 ', '03计算应用1 ', '男', '1981-9-23', '宁波 ', 326, null, '330203198109230655', 28); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0301109', '陈奕迅 ', '03计算应用1 ', '男', '1982-6-25', '杭州 ', 361, null, '330203198206252418', 22); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0301110', '徐若萱 ', '03计算应用1 ', '女', '1982-7-9', '宁波 ', 376, null, '330203198207090617', 22); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0301111', '陈冠希 ', '03计算应用1 ', '男', '1981-3-21', '杭州 ', 401, null, '330203810321003 ', 22); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311101', '赵薇 ', '03物流1 ', '女', '1982-2-11', '台州 ', 289, null, '330203820211092 ', 16); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311102', '董洁 ', '03物流1 ', '女', '1982-2-17', '金华 ', 378, null, '330203820217001 ', 24); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311103', '王力宏 ', '03物流1 ', '男', '1982-5-31', '温州 ', 361, null, '330203820531002 ', 24); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311104', '李嘉欣 ', '03物流1 ', '女', '1981-5-28', '宁波 ', 287, null, '330204198105281056', 24); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311105', '苏有朋 ', '03物流1 ', '男', '1982-4-16', '宁波 ', 372, null, '330204198204162036', 24); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311106', '夏雨 ', '03物流1 ', '男', '1982-10-12', '绍兴 ', 384, null, '330204198210121046', 16); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311107', '郭富城 ', '03物流1 ', '男', '1982-10-17', '台州 ', 343, null, '330204198210173022', 16); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311108', '袁咏仪 ', '03物流1 ', '女', '1981-11-16', '杭州 ', 376, null, '330204811116101 ', 18); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311109', '张柏芝 ', '03物流1 ', '女', '1982-3-29', '温州 ', 421, null, '330204820329201 ', 18); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311110', '张信哲 ', '03物流1 ', '男', '1982-7-14', '宁波 ', 408, null, '330204820714502 ', 24); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311111', '陈坤 ', '03物流1 ', '男', '1982-7-19', '宁波 ', 326, null, '330204820719604 ', 24); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311201', '王祖贤 ', '03物流2 ', '女', '1982-7-25', '绍兴 ', 337, null, '330204820725301 ', 20); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311202', '佟大为 ', '03物流2 ', '男', '1982-6-7', '金华 ', 322, null, '330205198206070617', 22); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311203', '谢霆锋 ', '03物流2 ', '男', '1982-11-2', '杭州 ', 364, null, '330205198211020964', 22); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311205', '胡军 ', '03物流2 ', '男', '1981-7-16', '宁波 ', 316, null, '330206198107163128', 22); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311206', '陈红 ', '03物流2 ', '女', '1981-11-9', '金华 ', 327, null, '330206198111095710', 20); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311207', '蒋勤勤 ', '03物流2 ', '女', '1982-1-9', '杭州 ', 424, null, '330206198201094616', 24); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311208', '吴彦祖 ', '03物流2 ', '男', '1982-6-1', '金华 ', 297, null, '330206198206013416', 28); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311209', '刘德华 ', '03物流2 ', '男', '1982-10-31', '宁波 ', 281, null, '330206198210313462', 28); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311210', '伊能静 ', '03物流2 ', '女', '1983-1-16', '金华 ', 356, null, '330206198301161425', 28); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311211', '李亚鹏 ', '03物流2 ', '男', '1981-11-10', '湖州 ', 288, null, '330206811110312 ', 21); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311301', '刘烨 ', '03物流3 ', '男', '1981-12-23', '宁波 ', 293, null, '330206811223462 ', 18); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311302', '周迅 ', '03物流3 ', '女', '1976-8-7', '台州 ', 357, null, '330206820201141 ', 18); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311303', '朱茵 ', '03物流3 ', '女', '1982-2-14', '湖州 ', 381, null, '330206820214091 ', 19); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311304', '刘嘉玲 ', '03物流3 ', '女', '1982-7-18', '绍兴 ', 405, null, '330206820718142 ', 25); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311305', '周杰 ', '03物流3 ', '男', '1982-8-5', '温州 ', 332, null, '330206820805031 ', 28); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311306', '李若彤 ', '03物流3 ', '女', '1982-8-9', '宁波 ', 311, null, '330206820809031 ', 25); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311307', '陆毅 ', '03物流3 ', '男', '1983-1-20', '绍兴 ', 367, null, '330203830120184 ', 25); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311308', '袁莉 ', '03物流3 ', '女', '1982-10-1', '宁波 ', 325, null, '330203821001094 ', 30); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311309', '刘亦菲 ', '03物流3 ', '女', '1982-5-26', '湖州 ', 354, null, '330203820526062 ', 24); INSERT INTO Student (sno, sname, class, ssex, bday, bplace, mgrade, photo, sfzh, zxf) VALUES ('0311310', '萧亚轩 ', '03物流3 ', '女', '1982-3-31', '温州 ', 405, null, '330203820331002 ', 20); -- Table: SC CREATE TABLE SC ( term smallint NOT NULL, sno char(7) NOT NULL , cno char(7) NOT NULL , grade smallint NULL, point decimal(2, 1) NULL, PRIMARY KEY (term,sno,cno), FOREIGN KEY (sno) REFERENCES Student (sno), FOREIGN KEY (cno) REFERENCES Course (cno) ON UPDATE CASCADE ); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0301101', '0000011', 88, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0301102', '0000011', 75, 1.5); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0301102', '0000027', 79, 1.5); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311101', '0000008', 86, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311101', '0000009', 58, 0); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311101', '0000011', 85, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311101', '0000027', 87, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311101', '0000034', 88, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311101', '0000039', 85, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311101', '0000052', 63, 0); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311102', '0000006', 61, 0); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311102', '0000008', 78, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311102', '0000009', 76, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311102', '0000011', 75, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311102', '0000027', 84, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311102', '0000034', 78, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311102', '0000052', 72, 1.5); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311111', '0000008', 75, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311111', '0000009', 74, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311111', '0000011', 95, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311111', '0000027', 82, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311111', '0000034', 72, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311111', '0000039', 66, 0); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311111', '0000052', 78, 1.5); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311201', '0000008', 68, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311201', '0000009', 81, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311201', '0000011', 78, 1.5); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311201', '0000027', 84, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311201', '0000034', 64, 0); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311201', '0000039', 77, 1.5); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311201', '0000052', 71, 2); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311202', '0000008', 78, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311202', '0000009', 70, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311202', '0000011', 65, 0); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311202', '0000027', 80, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311202', '0000034', 79, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311202', '0000039', 59, 0); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311202', '0000052', 87, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311211', '0000006', 70, 1.5); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311211', '0000008', 74, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311211', '0000009', 52, 0); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311211', '0000011', 79, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311211', '0000027', 75, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311211', '0000034', 79, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (1, '0311211', '0000052', 82, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (2, '0301101', '0000002', 80, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (2, '0301101', '0000006', 91, 1.5); INSERT INTO SC (term, sno, cno, grade, point) VALUES (2, '0301101', '0000007', 87, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (2, '0301101', '0000008', 55, 0); INSERT INTO SC (term, sno, cno, grade, point) VALUES (2, '0301101', '0000010', 66, 0); INSERT INTO SC (term, sno, cno, grade, point) VALUES (2, '0301101', '0000013', 90, 1.5); INSERT INTO SC (term, sno, cno, grade, point) VALUES (2, '0301101', '0000034', 81, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (2, '0301102', '0000002', 110, 1.5); INSERT INTO SC (term, sno, cno, grade, point) VALUES (2, '0301102', '0000008', 83, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (2, '0301102', '0000009', 76, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (2, '0301102', '0000010', 78, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (2, '0301102', '0000013', 82, 1); INSERT INTO SC (term, sno, cno, grade, point) VALUES (2, '0301102', '0000034', 64, 0); INSERT INTO SC (term, sno, cno, grade, point) VALUES (3, '0301101', '0000001', 85, null); INSERT INTO SC (term, sno, cno, grade, point) VALUES (3, '0311211', '0000001', 80, null); -- Table: Teacher CREATE TABLE Teacher ( tno char(3) NOT NULL, tname char(8) NULL, ps char(10) NULL, wday datetime NULL, dept char(16) NULL, pay smallint NULL, marry smallint NULL, resume varchar(200) NULL, PRIMARY KEY ( tno ) ) ; INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('001', '王涛 ', '讲师 ', '2001-9-1', '基础部 ', 2100, 0, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('002', '姚明 ', '助教 ', '2002-2-3', '基础部 ', 2100, 0, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('003', '蔡振华 ', '副教授 ', '2002-4-10', '经管系 ', 4800, 1, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('004', '田亮 ', '助教 ', '2003-7-10', '计算机系 ', 2100, 0, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('005', '李小双 ', '讲师 ', '2001-9-10', '应用技术系 ', 2600, 1, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('006', '孙继海 ', '讲师 ', '2001-1-20', '国际交流系 ', 2600, 0, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('007', '卡佩罗 ', '教授 ', '2001-5-16', '基础部 ', 5500, 0, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('008', '孔令辉 ', '助教 ', '2003-7-10', '基础部 ', 2100, 0, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('009', '刘玉栋 ', '助教 ', '2001-10-12', '基础部 ', 2100, 0, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('010', '隋菲菲 ', '助教 ', '2002-7-10', '国际交流系 ', 2100, 0, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('011', '张蓉芳 ', '副教授 ', '2001-12-13', '基础部 ', 4800, 1, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('012', '李宁 ', '讲师 ', '2003-10-19', '基础部 ', 2600, 1, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('013', '赵蕊蕊 ', '助教 ', '2003-7-1', '计算机系 ', 2100, 0, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('014', '谢军 ', '讲师 ', '2003-8-18', '计算机系 ', 2600, 1, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('015', '刘国梁 ', '讲师 ', '2003-9-10', '基础部 ', 2600, 0, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('016', '李永波 ', '副教授 ', '2002-8-3', '国际交流系 ', 2600, 1, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('017', '郎平 ', '副教授 ', '2003-3-30', '经管系 ', 4800, 1, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('018', '王军霞 ', '助教 ', '2001-5-19', '经管系 ', 2100, 0, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('019', '马晓春 ', '讲师 ', '2003-10-20', '国际交流系 ', 2600, 1, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('020', '章秋红 ', '讲师 ', '2003-9-1', '计算机系 ', 2600, 1, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('021', '罗雪娟 ', '助教 ', '2000-12-21', '经管系 ', 2100, 0, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('022', '聂卫平 ', '副教授 ', '2001-3-8', '应用技术系 ', 2600, 0, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('023', '陈忠和 ', '副教授 ', '2003-7-1', '计算机系 ', 2100, 1, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('024', '葛菲 ', '助教 ', '2003-7-1', '计算机系 ', 2100, 1, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('025', '孙俊 ', '助教 ', '2002-1-21', '基础部 ', 2100, 1, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('026', '邹振先 ', '副教授 ', '2003-8-20', '经管系 ', 4800, 1, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('027', '袁伟民 ', '教授 ', '2000-8-25', '校办 ', 5500, 1, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('028', '乔丹 ', '教授 ', '2000-10-1', '经管系 ', 5500, 1, null); INSERT INTO Teacher (tno, tname, ps, wday, dept, pay, marry, resume) VALUES ('029', '许海峰 ', '教授 ', '2003-2-20', '计算机系 ', 5500, 1, null); -- Table: TC CREATE TABLE TC ( list smallint, term smallint, class varchar(40) NULL, cno char(7) NULL, tno char(3) NULL, period smallint NULL, PRIMARY KEY (list), FOREIGN KEY (cno) REFERENCES Course (cno) ON UPDATE CASCADE, FOREIGN KEY (tno) REFERENCES Teacher (tno) ) ; INSERT INTO TC (list, term, class, cno, tno, period) VALUES (1, 1, '03物流1 ', '0000011', '001', 36); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (2, 1, '03物流1 ', '0000034', '002', 72); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (3, 1, '03物流1 ', '0000052', '003', 60); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (4, 1, '03物流1 ', '0000027', '004', 108); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (5, 1, '03物流1 ', '0000039', '005', 36); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (6, 1, '03物流1 ', '0000005', '006', 72); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (7, 1, '03物流1 ', '0000001', '007', 36); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (8, 1, '03物流2 ', '0000011', null, 36); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (9, 1, '03物流2 ', '0000034', '002', 72); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (10, 1, '03物流2 ', '0000052', '003', 60); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (11, 1, '03物流2 ', '0000027', '004', 108); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (12, 1, '03物流2 ', '0000039', '009', 36); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (13, 1, '03物流2 ', '0000005', '010', 72); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (14, 1, '03物流2 ', '0000001', '007', 36); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (15, 1, '03计算应用1 ', '0000011', '022', 36); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (16, 1, '03计算应用1 ', '0000034', null, 54); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (17, 1, '03计算应用1 ', '0000045', '013', 108); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (18, 1, '03计算应用1 ', '0000027', '014', 144); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (19, 1, '03计算应用1 ', '0000039', '015', 36); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (20, 1, '03计算应用1 ', '0000005', '016', 72); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (21, 1, '03计算应用1 ', '0000001', '007', 36); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (22, 2, '03物流1 ', '0000007', '017', 108); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (23, 2, '03物流1 ', '0000012', null, 54); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (24, 2, '03物流1 ', '0000005', '019', 72); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (25, 2, '03物流1 ', '0000008', '020', 108); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (26, 2, '03物流1 ', '0000032', '007', 36); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (27, 2, '03物流1 ', '0000004', '021', 72); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (28, 2, '03物流1 ', '0000003', '022', 36); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (29, 2, '03物流2 ', '0000007', '017', 108); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (30, 2, '03物流2 ', '0000012', '018', 54); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (31, 2, '03物流2 ', '0000005', '010', 72); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (32, 2, '03物流2 ', '0000008', '020', 108); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (33, 2, '03物流2 ', '0000032', '007', 36); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (34, 2, '03物流2 ', '0000004', '021', 72); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (35, 2, '03物流2 ', '0000003', '022', 36); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (36, 2, '03计算应用1 ', '0000002', '023', 144); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (37, 2, '03计算应用1 ', '0000010', '014', 72); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (38, 2, '03计算应用1 ', '0000013', null, 72); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (39, 2, '03计算应用1 ', '0000034', '025', 54); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (40, 2, '03计算应用1 ', '0000005', '016', 72); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (41, 2, '03计算应用1 ', '0000032', '007', 36); INSERT INTO TC (list, term, class, cno, tno, period) VALUES (42, 2, '03计算应用1 ', '0000003', '001', 36);
最后来总结一下本文所学习的内容📖