数据库系统原理与设计(考试重点)

  • 格式:doc
  • 大小:90.50 KB
  • 文档页数:5

2.7 答案:

(1) (2)

2.8 答案

图1是学生成绩管理数据库ScoreDB的模式导航图,该关系数据库由4个关系组成,带下划线的属性集为关系的主码,斜体属性为关系的外码。请用关系代数表达(1)~(9),用SQL语言表达(10)~(14)。

班级Class classNo 班级编号

className 班级名称

institute 所属学院

grade 年级

classNum 班级人数

图1

(1)查找籍贯为“上海”的全体学生

(1)σnative=’上海’(Student)

(2)查找1992年元旦以后出生的全体男同学

(2) σyear(birthday)>=1992∧ sex=’男’(Student) studentName Birthday

李小勇 1990-12-21

王红 1992-04-26 studentNo courseNo term score

0701001 CN028 07081 85

0701001 CS012 07082 88

0701001 CS015 08091 92

0701008 AC001 07081 76

0701008 CN028 07081 86

0701008 CS012 07082 93

0701008 CS015 08091 96

0802005 AC001 09101 88

0802005 CS012 08092 90

0802005 CS015 09101 87

学生 Student

studentNo 学号

studentName 姓名

sex 性别

birthday 出生日期

ative 籍贯

nation 民族

classNo 所属班级成绩Score

studentNo 学号

courseNo 课程号

term 开课学期

score 成绩 课程Course

courseNo 课程号

courseName 课程名

creditHour 学分

courseHour 课时数

priorCourse 先修课程 (3)查找信息学院非汉族同学的学号、姓名、性别及民族

(3)ПstudentNo,studnetName,sex,nation(σinstitute=’信息学院’ ∧ nation!=’汉族’(Student∞Class))

(4)查找08-09学年第二学期(08092)开出课程的编号、名称和学分

(4) ПcourseNo,courseName,creditHour(σterm=’08092’(Course∞Score))

(5)查 找选修了“操作系统”的学生学号、成绩及姓名

(5) ПstudentNo,score,studentName(σcourseName=’操作系统’(Student∞Course∞Score))

(6)查找班级名称为“会计学08(3)班”的学生在07-08学年第一学期(07081)选课情况,要求显示学生姓名、课程号、课程名和成绩

(6) ПstudentName,courseNo,courseName,score(σclassName=’会计学08(3)班’ ∧term=’07081’(Class∞Studnet∞Course∞Scor e))

(7)查找至少选修了一门其直接先修课编号为CS012的课程的学生学号和姓名

(7) ПstudentName,studentNo(σpriorcourse=’CS012’(Student∞Score∞Course))

(8)查找选修了08-09学年第一学期(08091)开出的全部课程的学生学号和姓名

(8) ПstudentNo,studentName((Studnet∞Score)÷ПcourseNo(σterm=’08091’(Score)))

(9)查找至少选修了学号为0703010的学生所选课程的学生学号和姓名

(9) ПstudentNo,studentName((Student∞Score) ÷(ПcourseNo(σstudentNo=’0703010’(Score)))

(10)在成绩表Score表中查询成绩在60~80分之间的学生学号、课程号和相应成绩。

Select studentNo,courseNo,score from Score where score between 60 and 90

(11)在成绩Score表中查询选修了001、005或002课程的学生学号、课程号和相应成绩。

Select studentNo,courseNo,score from Score where courseNo IN(‘001’,’005’,’002’)

(12)在学生表Student中查询所有姓王且全名为3个汉字的学生学号和姓名。

Select studentNo,studentName from Student where studentName LIKE ‘王_ _’

(13)查询所选修课程的成绩大于所有002号课程成绩的同学学号及相应课程的课程号和成绩。

Select studentNo,courseNo,score

from Score

where score>all(select score from Score where courseNo=’002’)

(14)查询学号为0800005同学所选修课程的总学分

Select sum(creditHour) 总学分

From score a,Course b

Where studentNo=’0800005’ and a.courseNo=b.courseNo

(15)查询同时选修了001号和005号课程的同学的学号和姓名

Select a.studentNo,studentName

From Student a,Score b

Where a.studentNo=b.studnetNo and courseNo=’001’

Intersect

Select a.studnetNo,studnetName

From Student a,Score b

Where a.studentNo=b.studnetNo and courseNo=’005’

Select a.studentNo,studentName

From Student a,Score b

Where a.studentNo=b.studentNo and courseNo=’001’

And a.studentNo In(select studentNo from Score where courseNo=’005’)

(16)删除学号为0800001同学的选课记录 Delete from Score where studentNo=’0800001’

(17)删除平均分在60~70之间的同学选课记录

Delete from Score where studentNo IN(

Select studentNo from Score Group by studentNo Having avg(score) between 60 and 70)

(18)将一个新学生元组(‘0700006’, ‘李相东’, ‘男’, ‘1991-10-21 00:00’, ‘云南’, ‘撒呢族’, ‘CS0701’)插入到学生表Student中。

INSERT INTO Student

VALUES ( '0700006', '李相东', '男', '1991-10-21 00:00', '云南', '撒呢族', 'CS0701' )

(19) 将刘方晨同学选修的005课程的成绩改为88分。

UPDATE Score

SET score=88

WHERE courseNo='005' AND studentNo IN (

SELECT studentNo FROM Student

WHERE studentName='刘方晨'

2.9答案

见书上

另外的查询:查找会计学院全体同学的学号、姓名、籍贯、班级编号和所在班级名称。

分别用关系代数表达式表达,然后用SQL语言来表达

ПstudentNo,studentName,native,classNo,className((σinstitute=’会计学院’(Class))∞Student))

SELECT studentNo, studentName, native,

Student.classNo, className

FROM Student, Class

WHERE Student.classNo=Class.classNo AND institute='会计学院'

一个子查询块嵌入只能放到另一个查询块的WHERE子句或HAVING子句中,如果要将子查询块放到From子句后,那么必须给子查询块的结果重新命名。

Having

子句与Where

子句的区别?

HAVING只针对分组的结果进行选择,仅输出满足条件的组。该子句必须与GROUP

BY子句配合使用。而Where子句只针对元组进行选择。

或者这样回答:

(1) WHERE子句:作用于整个查询对象,对元组进行过滤。

(2) HAVING子句:仅作用于分组,对分组进行过滤。