文档详情

SEU数据库第二次实验.docx

发布:2018-12-12约5.9千字共8页下载文档
文本预览下载声明
实验目的: 了解SQL语言的特点和基本概念。 能够针对某种具体的DBMS(本实验采用Access2003),熟练地运用单表查询、连接查询、嵌套查询、集合查询等各种SQL查询语句对数据库中的表进行操作。 对相同的查询要求,能够采用多种查询方法实现,并能分析各种方法的优劣, 从中选择合适的方法。 实验过程: 实验一:Print the names of professors who work in departments that have fewer than 50 PhD students. 解:(1)分析:本题是查询在博士生人数少于50个人的系工作的教师名字。查询教授名字可以通过prof表,而所查询的教授名字是有限制条件的, 他所工作的系名要属于特定的集合(所有系名的一个子集),显然这个子集可以通过查询dept表获得,所以带有谓词in的嵌套子查询可以实现题目要求。 (2)语句实现:SELECT pname FROM Prof WHERE dname IN (SELECT dname FROM Dept WHERE numphds 50); (3)查询结果: 实验二: Print the name(s) of student(s) with the lowest gpa 解:(1)分析:本题是查询成绩点最低的学生的名字。最低的成绩点可以在student表中通过函数min(gpa)获得,而所查询学生的名字的限制为成绩点等于min(gpa), 因此可用如下嵌套子查询实现。 (2)语句实现:SELECT sname FROM student WHERE gpa IN (SELECT min(gpa) FROM student); (3)查询结果: 实验三:For each Computer Sciences class, print the cno, sectno, and the average gpa of the student enrolled in the class. 解:(1)分析:本题是查询计算机科学系的所有班的课程号、分班号、班上学生的平均绩点。计算机科学系的所有班可以通过section表获得, 而通过enroll表可以由section表中的dname, cno, sectno获得班上所有学生的sid,而通过sid可以在student表中查得学生成绩点,最后由cno, sectno进行分组,并用函数avg(gpa),获得每组的平均成绩。所以可用三个表的连接查询,并适当分组实现查询要求。 (2)语句实现:SELECT section.cno, section.sectno, Avg(gpa) AS gpa之平均值 FROM [section], enroll, student WHERE ( (section.dname =Computer Sciences) and (section.cno = enroll.cno) and (enroll.sid=student.sid) ) GROUP BY section.cno, section.sectno; (3)查询结果: 实验四:Print the course names, course numbers and section numbers of all classes with less than six students enrolled in them. 解:(1)分析:本题是查询所有班级人数少于6的课程名,课程号,分班号。通过section表可以查询出所有的班,其中的课程名可由查询所得的dname, cno在course表中确定,因为与班级人数有关,还需将section表和enroll表做连接,并按section.cno, section.dname, section.sectno分组以获取所有班的人数。所以可用连接查询、嵌套查询,并适当分组来实现查询要求。 (2)语句实现:SELECT (select cname from course where cno=section.cno and dname=section.dname) AS cname, section.cno, section.sectno FROM [section], enroll WHERE (section.cno = enroll.cno ) GROUP BY section.cno, section.sectno, section.dname HAVING count(*) 6; (3)查询结果:没有低于6个的 实验五:Print the name(s) and sid(s) of student(s) e
显示全部
相似文档