现在有一教学管理系统,具体的关系模式如下:
Student (no, name, sex, birthday, class)
Teacher (no, name, sex, birthday, prof, depart)
Course (cno, cname, tno)
Score (no, cno, degree)
其中表中包含如下数据:
Course表:
Score表:
Student表:
Teacher表:
根据上面描述完成下面问题:
(注意:注意保存脚本,尤其是DDL和DML,以便进行数据还原)
DDL
写出上述表的建表语句。
命令:create table Student(no int,name varchar(14),sex varchar(2),birthday date,class int);
create table Teacher(no int,name varchar(14),sex varchar(2),birthday date,prof varchar(10),depart varchar(10));
create table Course(cno varchar(8),cname varchar(14),tno int);
create table Score(no int,cno varchar(8),degree int);
DML
给出相应的INSERT语句来完成题中给出数据的插入。
命令:
insert into Student values(5001,'李勇','男','1987-7-22',95001);
insert into Student values(5002,'刘晨','女','1987-11-15',95002);
insert into Student values(5003,'王敏','女','1987-10-5',95001);
insert into Student values(5004,'李好尚','男','1987-9-25',95003);
insert into Student values(5005,'李军','男','1987-7-17',95004);
insert into Student values(5006,'范新位','女','1987-6-18',95005);
insert into Student values(5007,'张霞东','女','1987-8-29',95006);
insert into Student values(5008,'赵薇','男','1987-6-15',95007);
insert into Student values(5009,'钱民将','女','1987-6-23',95008);
insert into Student values(5010,'孙俪','女','1987-9-24',95002);
insert into Student values(108,'赵里','男','1987-6-15',95007);
insert into Student values(109,'丘处机','男','1987-6-23',95008);
insert into Student values(107,'杨康','男','1987-9-24',95001);
insert into Teacher values(1,'李卫','男','1957-11-5','教授','电子工程系');
insert into Teacher values(2,'刘备','男','1967-10-9','副教授','math');
insert into Teacher values(3,'关羽','男','1977-9-20','讲师','sc');
insert into Teacher values(4,'李修','男','1957-6-25','教授','elec');
insert into Teacher values(5,'诸葛亮','男','1977-6-15','教授','计算机系');
insert into Teacher values(6,'殷素素','女','1967-1-5','副教授','sc');
insert into Teacher values(7,'周芷若','女','1947-2-23','教授','sc');
insert into Teacher values(8,'赵云','男','1980-6-13','副教授','计算机系');
insert into Teacher values(9,'张敏','女','1985-5-5','助教','sc');
insert into Teacher values(10,'黄蓉','女','1967-3-22','副教授','sc');
insert into Teacher values(11,'张三','男','1967-3-22','副教授','sc');
insert into Course values('3-101','数据库',1);
insert into Course values('5-102','数学',2);
insert into Course values('3-103','信息系统',3);
insert into Course values('3-104','操作系统',4);
insert into Course values('3-105','数据结构',5);
insert into Course values('3-106','数据处理',5);
insert into Course values('4-107','pascal语言',6);
insert into Course values('4-108','C++',7);
insert into Course values('4-109','java',8);
insert into Course values('3-245','数据挖掘',10);
insert into Course values('3-111','软件工程',11);
insert into Score values(5001,'3-105',69);
insert into Score values(5001,'5-102',55);
insert into Score values(5003,'4-108',85);
insert into Score values(5004,'3-105',77);
insert into Score values(5005,'3-245',100);
insert into Score values(5006,'3-105',53);
insert into Score values(5003,'4-109',45);
insert into Score values(5008,'3-105',98);
insert into Score values(5004,'4-109',68);
insert into Score values(5010,'3-105',88);
insert into Score values(5003,'3-105',98);
insert into Score values(5005,'4-109',68);
insert into Score values(5002,'3-105',88);
insert into Score values(107,'3-105',98);
insert into Score values(108,'4-109',68);
insert into Score values(109,'3-105',88);
insert into Score values(109,'4-109',80);
insert into Score values(107,'3-111',88);
insert into Score values(5003,'3-111',80);
单表查询
以class降序输出student的所有记录(student表全部属性)
命令:select * from Student order by class desc;
列出教师所在的单位depart(不重复)。
命令:select distinct depart from Teacher;
列出student表中所有记录的name、sex和class列
命令:select name,sex,class from Student;
输出student中不姓王的同学的姓名。
命令:select name from Student except select name from Student where name like '王%';或
select name from Student where name not like '王%';
输出成绩为85或86或88或在60-80之间的记录(no,cno,degree)
命令:select no,cno,DEGREE from Score where degree=85 or degree=86 or degree=88 or degree between 60 and 80;
输出班级为95001或性别为‘女’ 的同学(student表全部属性)
命令:select * from Student where class=95001 or sex='女';
以cno升序、degree降序输出score的所有记录。(score表全部属性)
命令:select * from Score order by cno asc,degree desc;
输出男生人数及这些男生分布在多少个班级中
命令:select COUNT(*),count(distinct class) from Student where sex='男';
列出存在有85分以上成绩的课程编号。
命令:select distinct cno from Score where degree>85;
输出95001班级的学生人数
命令:select COUNT(*) from Student where class=95001;
输出‘3-105’号课程的平均分
命令:select avg(cast(degree as float)) from Score where cno='3-105';
输出student中最大和最小的birthday日期值
命令:select MAX(birthday),MIN(birthday) from Student;
显示95001和95004班全体学生的全部个人信息(不包括选课)。(student表全部属性)
命令:select * from Student where class=95001 or class=95004;
聚合查询
输出至少有5个同学选修的并以3开头的课程的课程号,课程平均分,课程最高分,课程最低分。
命令:select cno,avg(cast(degree as float)),MAX(degree),MIN(degree) from Score where cno like '3%' group by cno having COUNT(cno)>5;
或者:
select cno,AVG(cast(DEGREE as float)),MAX(degree),MIN(DEGREE) from Score group by cno having COUNT(cno)>=5 and cno like '3%'
输出所选修课程中最低分大于70分且最高分小于90分的学生学号及学生姓名
命令:select Student.no,name from Student join Score on Student.no=Score.no group by Student.no,name having MAX(Score.degree)<90 and MIN(Score.degree)>70;
显示所教课程选修人数多于5人的教师姓名
命令:select name from Teacher join Course on Teacher.no=Course.tno where Course.cno in(select cno from Score group by cno having COUNT(Score.cno)>5);
输出’95001’班级所选课程的课程号和平均分
命令:select cno,avg(cast(degree as float)) from Score where no in(select no from Student where class=95001) group by cno;
或者:
select cno,AVG(cast(degree as float)) from Score join Student on Score.no=Student.no group by cno,class having class='95001'
输出至少有两名男同学的班级编号。
命令:select class from Student where sex='男' group by class having COUNT(class)>=2;
或者:select a.class from (select * from Student where sex='男') a group by a.class having COUNT(a.class)>=2
多表查询
列出与108号同学同年出生的所有学生的学号、姓名和生日
命令:select no,name,birthday from Student where year(birthday) =(select year(birthday) from Student where no=108);
或者:select b.no,b.name,b.birthday from Student a join Student b on datediff(YEAR,a.birthday,b.birthday)=0 and a.no='108'
列出存在有85分以上成绩的课程名称
命令:select cname from Course where cno in (select distinct cno from Score where degree>85);或
select distinct cname from Course join Score on Course.cno=Score.cno where degree>85;
列出“计算机系”教师所教课程的成绩表(课程编号,