CREATE TABLE student_t(
sno Char(7) PRIMARY KEY,--学号
sname Varchar(20) NOT NULL,--姓名
ssex CHAR(2) NOT NULL, --性别
sage Smallint, --年龄
CLON CHAR(5) --学生所在班级的编号
);
CREATE TABLE course_t(
cno CHAR(1) PRIMARY KEY, --课程编号
cname Varchar(20) NOT NULL, --课程名称
credit SMALLINT -- 学分
);
CREATE TABLE CLASS_t(
clno CHAR(5) PRIMARY KEY, --班级号
speciality VARCHAR(20) NOT NULL, --编辑所在专业
inyear CHAR(4) NOT NULL, --入校年份
cNUM INTEGER, -- 班级人数
MONITOR_no CHAR(7) -- 班长学号
);
CREATE TABLE grade_t(
sno char(7), --学号
cno CHAR(1) NOT NULL, --课程号
gmark NUMERIC(4,1) --成绩
);
INSERT INTO student_t (sno,sname,ssex,sage, CLON) VALUES (‘2000101‘,‘李勇‘, ‘男‘, 20,‘00311‘);
INSERT INTO student_t (sno,sname,ssex,sage, CLON) VALUES (‘2000102‘,‘刘诗晨‘,‘女‘, 19,‘00311‘);
INSERT INTO student_t (sno,sname,ssex,sage, CLON) VALUES (‘2000103‘,‘王一鸣‘,‘男‘, 20,‘00312‘);
INSERT INTO student_t (sno,sname,ssex,sage, CLON) VALUES (‘2000104‘,‘张婷婷‘,‘女‘, 21,‘00312‘);
INSERT INTO student_t (sno,sname,ssex,sage, CLON) VALUES (‘2001101‘,‘李勇敏‘,‘女‘, 19,‘01311‘);
INSERT INTO student_t (sno,sname,ssex,sage, CLON) VALUES (‘2001102‘,‘贾向东‘,‘男‘, 22,‘01311‘);
INSERT INTO student_t (sno,sname,ssex,sage, CLON) VALUES (‘2001103‘,‘陈宝玉‘,‘男‘, 20,‘01311‘);
INSERT INTO student_t (sno,sname,ssex,sage, CLON) VALUES (‘2001104‘,‘张逸凡‘,‘男‘, 21,‘01311‘);
--增加以下这条数据目的是为了12.9查询到数据
INSERT INTO student_t (sno,sname,ssex,sage, CLON) VALUES (‘2000105‘,‘折挺‘, ‘男‘, 20,‘00311‘);
INSERT INTO course_t ( cno,cname,credit) VALUES (‘1‘,‘数据库‘, 4);
INSERT INTO course_t ( cno,cname,credit) VALUES (‘2‘,‘离散数学‘, 3);
INSERT INTO course_t ( cno,cname,credit) VALUES (‘3‘,‘管理信息系统‘,2);
INSERT INTO course_t ( cno,cname,credit) VALUES (‘4‘,‘操作系统‘, 4);
INSERT INTO course_t ( cno,cname,credit) VALUES (‘5‘,‘数据结构‘, 4);
INSERT INTO course_t ( cno,cname,credit) VALUES (‘6‘,‘数据处理‘, 2);
INSERT INTO course_t ( cno,cname,credit) VALUES (‘7‘,‘C语言‘, 4);
INSERT INTO CLASS_t (clno, speciality,inyear,cNUM,MONITOR_no) VALUES (‘00311‘,‘计算机软件‘, ‘2000‘,120, ‘2000101‘);
INSERT INTO CLASS_t (clno, speciality,inyear,cNUM,MONITOR_no) VALUES (‘00312‘,‘计算机应用‘, ‘2000‘,140, ‘2000103‘);
INSERT INTO CLASS_t (clno, speciality,inyear,cNUM,MONITOR_no) VALUES (‘01311‘,‘计算机软件‘, ‘2001‘,220, ‘2001103‘);
INSERT INTO grade_t (sno, cno,gmark) VALUES (‘2000101‘,‘1‘, 92);
INSERT INTO grade_t (sno, cno,gmark) VALUES (‘2000101‘,‘3‘, 88);
INSERT INTO grade_t (sno, cno,gmark) VALUES (‘2000101‘,‘5‘, 86);
INSERT INTO grade_t (sno, cno,gmark) VALUES (‘2000102‘,‘1‘, 78);
INSERT INTO grade_t (sno, cno,gmark) VALUES (‘2000102‘,‘6‘, 55);
INSERT INTO grade_t (sno, cno,gmark) VALUES (‘2000103‘,‘3‘, 65);
INSERT INTO grade_t (sno, cno,gmark) VALUES (‘2000103‘,‘6‘, 78);
INSERT INTO grade_t (sno, cno,gmark) VALUES (‘2000103‘,‘5‘, 66);
INSERT INTO grade_t (sno, cno,gmark) VALUES (‘2000104‘,‘1‘, 54);
INSERT INTO grade_t (sno, cno,gmark) VALUES (‘2000104‘,‘6‘, 83);
INSERT INTO grade_t (sno, cno,gmark) VALUES (‘2001101‘,‘2‘, 70);
INSERT INTO grade_t (sno, cno,gmark) VALUES (‘2001101‘,‘4‘, 65);
INSERT INTO grade_t (sno, cno,gmark) VALUES (‘2001102‘,‘2‘, 80);
INSERT INTO grade_t (sno, cno,gmark) VALUES (‘2001102‘,‘4‘, 90);
INSERT INTO grade_t (sno, cno,gmark) VALUES (‘2000103‘,‘1‘, 83);
INSERT INTO grade_t (sno, cno,gmark) VALUES (‘2000103‘,‘2‘, 76);
INSERT INTO grade_t (sno, cno,gmark) VALUES (‘2000103‘,‘4‘, 56);
INSERT INTO grade_t (sno, cno,gmark) VALUES (‘2000103‘,‘7‘, 88);
--12.1 找出所有被学生选修了的课程号
SELECT DISTINCT cno FROM grade_t gt ORDER BY gt.cno;
--12.2 找出01311班女学生的个人信息
SELECT * FROM student_t stu WHERE stu.clon = ‘01311‘ AND stu.ssex = ‘女‘;
--12.3 找出01311班和01312班的学生姓名、性别、出生年份
SELECT STU.SNAME,
STU.SSEX,
(TO_DATE(TO_CHAR(SYSDATE, ‘YYYY/MM/DD‘), ‘YYYY/MM/DD‘) - STU.SAGE) AS 出生年份
FROM STUDENT_T STU
WHERE STU.CLON IN (‘01311‘, ‘01312‘);
--12.4 找出所有姓李的学生的个人信息
SELECT * FROM student_t stu WHERE stu.sname LIKE ‘李%‘;
--12.5 找出学生李勇所在班级的学生人数
SELECT ct.cnum FROM student_t stu JOIN class_t ct ON stu.clon = ct.clno WHERE stu.sname = ‘李勇‘;
--或者是子查询(首先查询出李勇所在的班级号,注意要用distinct,防止有多个李勇报错)
SELECT ct.cnum FROM class_t ct WHERE ct.clno = (SELECT DISTINCT stu.clon FROM student_t stu WHERE stu.sname = ‘李勇‘);
--12.6 找出课程名为操作系统的平均成绩、最高分、最低分
SELECT to_char(AVG(GT.GMARK),‘99999999999999.99‘) AS 平均成绩,
MAX(GT.GMARK) AS 最高分,
MIN(GT.GMARK) AS 最低分
FROM COURSE_T COU JOIN GRADE_T GT ON COU.CNO = GT.CNO
WHERE COU.CNAME = ‘操作系统‘;
--12.7 找出选修了课程的学生人数
SELECT COUNT(1) FROM (SELECT DISTINCT gt.sno FROM grade_t gt);
--或者
SELECT count(1) FROM (SELECT gt.sno, COUNT(gt.sno) FROM grade_t gt GROUP BY gt.sno);
--12.8 找出选修了课程为操作系统的学生人数
SELECT COUNT(1) FROM course_t ct JOIN grade_t gt ON ct.cno = gt.cno WHERE ct.cname = ‘操作系统‘;
--12.9 找出2000级计算机软件班的成绩为空的学生姓名
SELECT STU.SNAME
FROM STUDENT_T STU
LEFT JOIN CLASS_T CT ON STU.CLON = CT.CLNO
LEFT JOIN grade_t gt ON gt.sno = stu.sno
WHERE CT.INYEAR = ‘2000‘
AND CT.SPECIALITY = ‘计算机软件‘
AND gt.gmark IS NULL;
--13.1 找出与李勇在同一个班级的学生信息
SELECT * FROM student_t stu WHERE stu.clon = (SELECT clon FROM student_t WHERE sname = ‘李勇‘);
--13.2 找出所有与学生李勇有相同选修课程的学生信息
SELECT DISTINCT stu.*
FROM STUDENT_T STU
JOIN GRADE_T GT
ON STU.SNO = GT.SNO
WHERE GT.CNO IN
(SELECT GT.CNO
FROM GRADE_T
WHERE SNO =
(SELECT SNO FROM STUDENT_T WHERE SNAME = ‘李勇‘));
--13.3 找出年龄介于学生李勇和25岁之间的学生的信息
SELECT * FROM student_t stu WHERE stu.sage BETWEEN (SELECT sage FROM student_t WHERE sname = ‘李勇‘) AND 25;
--13.4 找出选修了课程操作系统的学生的学号和姓名
SELECT stu.sno,stu.sname
FROM STUDENT_T STU
JOIN GRADE_T GT
ON STU.SNO = GT.SNO
JOIN COURSE_T COU
ON COU.CNO = GT.CNO
WHERE COU.CNAME = ‘操作系统‘;
--13.5 找出没有选修1号课程的所有学生姓名
SELECT stu.sname FROM student_t stu WHERE stu.sno NOT IN(SELECT gt.sno FROM grade_t gt WHERE gt.cno = 1);
--13.6 找出选修了全部课程的学生的姓名
SELECT stu.sname
FROM STUDENT_T STU
JOIN GRADE_T GT
ON STU.SNO = GT.SNO
GROUP BY STU.SNO,STU.SNAME
HAVING COUNT(1) = (SELECT COUNT(1) FROM COURSE_T);
--14.1 查询选修了3号课程的学生学号及其成绩,并按照成绩的降序排列
SELECT stu.sno,gt.gmark FROM STUDENT_T STU JOIN GRADE_T GT ON STU.SNO = GT.SNO WHERE gt.cno = 3 ORDER BY gt.gmark;
--14.2 查询全体学生信息,要求查询结果按班级号升序排列,同一班级学生按年龄降序排列
SELECT * FROM student_t stu ORDER BY stu.clon,stu.sage DESC;
--14.3 求每个课程号及相应的选课人数
SELECT cou.cno,COUNT(1) FROM course_t cou LEFT JOIN grade_t gt ON cou.cno = gt.cno GROUP BY cou.cno ORDER BY cou.cno;
--14.4 找出选修了3门以上课程的学生学号
SELECT gt.sno FROM grade_t gt GROUP BY gt.sno HAVING count(1)>3;
西安科技大学高新学院 计科1001班 折挺
本文出自 “聚沙成塔” 博客,请务必保留此出处http://sheting.blog.51cto.com/4838048/1597021
数据库原理 西安电子科技大学(第三版) 付婷婷 第三章 课后习题答案
原文地址:http://sheting.blog.51cto.com/4838048/1597021