SQL语句与关系代数习题--答案已做

更新时间:2024-06-20 02:27:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

1

一、选择题

1、SQL语言是( )语言。——(SQL特点)(易) A)层次数据库 B)网络数据库 C)关系数据库 D)非数据库 答案:C

2、SQL语言具有两种使用方式,分别称为交互式SQL和( )。

——(SQL语言使用方式)(易)

A) 提示式SQL B)多用户SQL C)嵌入式SQL D)解释式SQL 答案:C

3、( )包括数据库模式定义和数据库存储结构与存取方法定义。( )实现对DB的操作,包括查询、插入、删除、修改数据库中的数据。( )用于数据保护,包括数据的安全性,完整性,并发控制和恢复等。——(数据库语言 DDL DML DCL)(中)

A) 数据控制子语言 B)数据定义子语言 C)数据操纵子语言 D)数据库语言 答案:B C A

4、下列SQL语句中,实现数据检索的语句是( ),修改表结构的是( ),修改属性值的是( ),删除表结构的是( ),删除表记录的是( )。

——(DROP TABLE, ALTER TABLE,UPDATE, DELETE,SELECT 语句)(难)

A)SELECT B)DROP C)UPDATE D)ALTER E)DELETE 答案:A D C B E

二、用关系代数表达式及SQL语句描述关系查询

1、 设有如下关系表R、S和T: ——(易) R(BH, XM, XB, DWH) S(DWH, DWM)

T(BH, XM, XB, DWH)

写出实现下列关系代数的SQL语句: 1) 2) 3) 4) 5)

?DWH?'100'(R)

???XM,XB(R)

(?XB?'女'(R))

XM,DWHR?S

XM,XB,DWH(?XB?'男'(R?S))

解:

1) SELECT * FROM R WHERE DWH=’100’; 2) SELECT XM,XB FROM R;

3) SELECT XM,DWH FROM R WHERE XB=’女’;

4) SELECT R.*,S.DWM FROM R, S WHERE R.DWH=S.DWH;

5) SELECT XM,XB,DWH FROM R,S WHERE R.DWH=S.DWH AND XB=’男’;

2

2、设有如下关系模式:

student(NO, NAME , SEX ,BIRTHDAY, CLASS)

teacher(NO,NAME,SEX,BIRTHDAY,PROF,DEPART) PROF为职称,DEPART为系别 course(CNO, CNAME, TNO)

score(NO, CNO, DEGREE) DEGREE 为成绩 写出实现以下各题功能的SQL语句:

(1) 查询至少有2名男生的班号;——(难)

Select CLASS from student where SEX=’男’ Group by SEX Having Count(*)>=2

(2) 查询不姓“王”的同学记录;——(易)

Select * from student where NAME NOT Like ‘王%’

(3) 查询每个学生的姓名和年龄;——(难)

Select NAME year(date())-year(BIRTHDAY) as age From student

(4) 查询学生中最大和最小的birthday日期值;——(中)

Select max(BIRTHDAY), min(BIRTHDAY) From student

(5) 查询学生表的全部记录并按班号和年龄从大到小的顺序;——(中)

Select * from student order by CLASS DESC, BIRTHDAY DESC

(6) 查询男教师及其所上的课程;——(中)

Select NO,NAME, CNAME From teacher , course where teacher.NO= course.TNO and SEX=’男’

(7) 查询最高分同学的学号,课程号和成绩;——(中)

Select * from score where DEGREE=

(select max(DEGREE) from score)

(8) 查询和“李军”同性别并同班的所有同学的姓名;——(中)

Select NAME from student where SEX=

(select SEX from student where NAME=’ 李军’ ) AND CLAEE= (select CLASS from student where NAME=’ 李军’ )

(9) 查询选修“数据库系统概论”课程的男同学的成绩表;——(中)

①Select NO,NAME, CNO, CNAME DEGREE from student, course, score

Where student.NO= score.NO and course.CNO= score.CNO

and CNAME=’ 数据库系统概论’ and SEX=’男’

②Select * from score where NO in

(select NO from student where Sex=’ 男’) AND CNO= (select CNO from course where CNAME=’ 数据库系统概论’)

(10) 查询所有未讲课的教师的姓名和所在系别;——(难)

Select NAME DEPART from teacher where NOT EXISTS (select * from score where teacher.NO=score.TNO)

(11) 查询“计算机系”教师所教课程的成绩表;——(难)

①Select * from score where CNO IN (select CNO from course where TNO in

(select TNO from teacher where DEPART=’ 计算机系’))

② Select Student.NO, Student.NAME, score.CNO, course.CNAME, Teacher.NO, Teacher.NAME, from student,

3

teacher,course,score where student.NO=score.NO and Course.CNO=score.CNO and Teacher.NO=Course.TNO and DEPART=’计算机系’

(12) 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录;——(难) Select student.NO, NAME, score.CNO,CNAME, DEGREE where score.CNO=’3-105’ and DEGREE >

(select DEGREE from score where NO=’109’ and CNO=’3-105’ ) and student.NO=score.NO and

score.CNO=course.CNO

(13) 查询最低分大于70,最高分小于90的学生的学号;——(中) Select NO from score Group by NO having min(DEGREE)>70 and max(DEGREE)<90 (14) 查询成绩在60到80之间的所有记录;——(中) Select * from score where DEGREE BETWEEN 60 AND 80

(15) 查询成绩比该课程平均成绩低的同学的成绩表;——(相关子查询)(难)

Select * from score x where DEGREE <(select AVE(DEGREE) from score y where x.CNO=y.CNO Group by y.CNO)

(16) 查询所有女教师和女同学的姓名、性别和生日;——(中) Select NAME, SEX, BIRTHDAY from student where SEX=’女’ UNION

Select NAME, SEX, BIRTHDAY from teacher where SEX=’女’

(17) 查询“计算机系”和“无线电系”不同职称的教师的姓名和职称;——(中)

Select NAME, PROF from teacher where DEPART=’计算机系’ OR DEPART =’无线电系’ order by PROF;

(以上是我自己做的答案) 参考答案:

解:(1)SELECT CLASS FROM student WHERE SEX=‘男’

GROUP BY CLASS HAVING COUNT(*)>=2;

(2)SELECT * FROM student WHERE NAME NOT LIKE ‘王*’; (3)SELECT NAME,year(date())-year(birthday) as age FROM student; (4)SELECT MAX(BIRTHDAY), MIN(BIRTHDAY) FROM student ; (5)SELECT * FROM student ORDER BY CLASS,BIRTHDAY DESC;

(6)SELECT x.name, y.cname FROM teacher x, course y WHERE x.no=y.tno and x.sex=’男’; (7)SELECT * FROM score WHERE degree=(SELECT max(degree) FROM score);

(8)SELECT name FROM student WHERE sex=(SELECT sex FROM student WHERE name=’李军’ ) and

class=(SELECT class FROM student WHERE name=’李军’);

(9)SELECT * FROM score WHERE no IN(SELECT no FROM student WHERE sex=‘男’) and cno=(SELECT

cno FROM course WHERE cname=‘数据库系统概论’);

(10)SELECT name, depart FROM teacher t WHERE NOT EXIST (SELECT * FROM course c WHERE

c.tno=t.no);

(11)SELECT * FROM score s, teacher t, course c WHERE t.depart=’计算机系’ and t.no=c.tno and

c.cno=score.cno;

(12)SELECT * FROM student s, score sc WHERE s.no=sc.no and cno=’3-105’ and degree>(SELECT degree

FROM sc WHERE no=’109’ and cno=’3-105’);

4

(13)SELECT no FROM score GROUP BY no HAVING min(degree)>70 and max(degree)<90; (14)SELECT * FROM score WHERE degree BETWEEN 60 AND 80;

(15)SELECT * FROM score a WHERE degree < (SELECT avg(degree) FROM score b WHERE b.cno=a.cno group by b.cno);

(16)SELECT name, sex, birthday FROM teacher WHERE sex=‘女’UNION SELECT name, sex, birthday FROM

student WHERE sex=‘女’;

(17) SELECT name, prof FROM teacher WHERE depart=’计算机系’ OR depart=’无线电系’ order by prof;

本文来源:https://www.bwwdw.com/article/ns13.html

Top