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