数据库练习(参考答案)

更新时间:2024-05-26 03:14:01 阅读量: 综合文库 文档下载

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

根据给出的表结构完成以下题目 表1-1 Student表结构 列名 说明 数据类型 约束 Sno 学号 字符串,长度为7 主码 Sname 姓名 字符串,长度为10 非空 Ssex 性别 字符串,长度为2 取‘男’或‘女’ Sage 年龄 整数 取值15~45 Sdept 所在系 字符串,长度为20 默认为‘计算机系’ 表3-1 Student表数据 Sno Sname Ssex Sage Sdept

9512101 李勇 男 19 计算机系 9512102 刘晨 男 20 计算机系 9512103 王敏 女 20 计算机系 9521101 张立 男 22 信息系 9521102 吴宾 女 21 信息系 9521103 张海 男 20 信息系 9531101 钱小平 女 18 数学系 9531102 王大力 男 19 数学系

------------------------------------------------------------------------------------------------------------------- 表1-2Course表结构 列名 说明 Cno 课程号 Cname 课程名 Ccredit 学分 Semster 学期 Period 学时

数据类型 约束

字符串,长度为10 主码 字符串,长度为20 非空 整数 取值大于0 整数 取值大于0 整数 取值大于0

Semester

1

表3-2 Course表数据 Cno Cname Ccredit C01 计算机文化学 3 C02 VB 2 3 C03 计算机网络 C04 数据库基础 C05 高等数学 C06 数据结构

4 6 8 5

7 6 2 4

表1-3 SC表结构 列名 说明 Sno 学号 Cno 课程名 Grade 成绩

数据类型 约束 字符串,长度为7 主码,引用Student的外码 字符串,长度为10 主码,引用Course 整数 取值0~100

表 3-3 SC表数据 Sno Cno Grade XKLB 9512101 c01 90 必修 9512101 c02 86 选修 9512101 c06 必修 9512102 c02 78 选修 9512102 c04 66 必修 9521102 c01 82 选修 9521102 c02 75 选修 9521102 c04 92 必修 9521102 c05 50 必修 9521103 c02 68 选修 9521103 c06 必修 9531101 c01 80 选修 9531101 c05 95 必修 9531102 c05 85 必修

题1:查询没有选修课程“c01”的学生姓名和所在系。

答案:select sname,sdept from student where sno not in (select sno from sc where cno='C01') select sname,sdept from student,sc

where (student.sno=sc.sno) and (sc.sno != “co1”)

题2:为SC表添加“选课类别”列,此列的定义为XKLB char(4)。 答案:alter table sc add(XKLB varchar2(4)); 题3:将新添加的XKLB的类型改为char(6)。 答案:alter table sc modify(XKLB varchar2(6)); 题4:删除Course表的Period列。

答案:alter table course drop column period; 题5:删除计算机系不及格学生的选课记录。 答案:

delete from sc where grade<60 and sno in (select sno from student where sdept='计算机系');

题6:查询全体学生的学号与姓名。 答案:select sno,sname from student;

题7:查询全体学生的姓名,学号和所在系。 答案:select sname,sno,sdept from student; 题8:查询全体学生的记录。 答案:select * from student;

题9:查询全体学生的姓名及其出生年份。 答案:select sname,2014-sagefrom student;

或select sname,(to_char(sysdate,'YYYY')-sage) from student;

题10:查询全体学生的姓名和出生年份,比在出生年份列前加入一个列,此列的每行数据均为“Year of Birth”常量值。

答案:select sname,'Year of Birth',(to_char(sysdate,'YYYY')-sage) from student;

题11:在选课表(SC)中查询有哪些学生选修了课程,并列出学生的学号。 答案:select distinct sno from sc;

题12:查询计算机系全体学生的姓名。

答案:select distinct sname from student where sdept='计算机系'; 题13:查询所有年龄在20岁以下的学生的姓名及年龄。 答案:select sname,sage from student where sage<20; 题14:查询考试成绩不及格的学生的学号。 答案:select distinct sno from sc where grade<60;

题15:查询年龄在20~23岁之间的学生的姓名,所在系和年龄。

答案:select sname,sdept,sage from student where sage>=20 and sage<=23; 题16:查询年龄不在20~23之间的学生的姓名,所在系和年龄。

答案:select sname,sdept,sage from student where sage < 20 or sage >23; 题17:查询信息系,数学系和计算机系学生的姓名和性别。

答案:select sname,ssex from student where sdept IN (‘信息系’ , ‘数学系’ , ‘计算机系’);

题18:查询既不属于信息系,数学系,也不属于计算机系的学生的姓名和性别。 答案:select sname,ssex from student where sdept not in( '信息系','数学系','数学系'); 题19:查询姓“张”的学生的详细信息。

答案:select * from studentwhere sname like ‘张%’; 题20:查询学生表中姓“张”,姓“李”和姓“刘”的学生的情况。 答案: select * from student where sname like '张%' or sname like '李%' or sname like '刘%'; 或者 select * from student where sname like '[张李刘]%'; //错误

题21:查询名字中第2个字为“小”或“大”字的学生的姓名和学号。 答案: select sname ,sno from student where sname like '_小%' or sname like '_大%'; 或者 select sname ,sno from student where sname like '_[小大]%'; 题22:查询所有不姓“刘”的学生。

答案:select * from student where sname not like '刘%';

题23:从学生表中查询学号的最后一位不是2,3,5的学生的情况。 答案: select * from student where sno not like '%2' and sno not like '%3' and sno not like '%5'; 或者 select * from student where sno not like '%[235]'; //错误 题24:

查询无考试成绩的学生的学号和相应的课程号。 答案:select sno,cno from sc where grade is null;

题25:查询所有有考试成绩的学生的学号和课程号。 答案:select sno,cno from sc where grade is not null;

题26:查询计算机系年龄在20岁以下的学生的姓名。

答案:select sname from student where sdept = '计算机系' and sage <= 20; 题27:将学生按年龄升序排序。

答案:select * from student order by sage asc;

题28:查询选修了课程“c02”的学生的学号及其成绩,查询结果按成绩降序排列。 答案:select sno,grade from sc where cno = 'C02' ORDER BY GRADE DESC;

题29:查询全体学生的信息,查询结果按所在系的系名升序排列,同一系的学生按年龄降序排列。

答案:select * from student order by sdept,sage DESC;

题30:统计学生总人数。

答案:SELECT Count(*) from student; 题31:统计选修了课程的学生的人数。 答案:SELECT count(distinct sno) from sc;

题32 :计算学号为9512101的学生的考试总成绩之和。 答案:select SUM(GRADE) from sc where SNO = '9512101'; 题33:计算课程“c01”的学生的考试平均成绩。 答案:select avg(GRADE) from sc where cno = 'C01';

题34:查询选修了课程“c01”的学生的最高分和最低分。 答案:select MAX(GRADE),MIN(GRADE) from sc where cno = 'C01'; 题35:统计每门课程的选课人数,列出课程号和人数。 答案:select cno,count(sno) from sc group by cno; 题36:查询每名学生的选课们数和平均成绩。

答案:SELECT SNO,COUNT(CNO),sum(GRADE)/COUNT(CNO) FROM SC group by sno; 题37:查询选修了3门以上课程的学生的学号。

答案:select sno,count(*) from sc group by snohaving count(*)>3;

题38:查询选课门数等于或大于4门的学生的平均成绩和选课门数。

答案:select sno,avg(Grade),count(*) from sc group by sno having count(*)>= 4; 题39:查询每个学生的情况及其选课的情况。 答案: select student.sno,sname,ssex,sage,cn.cno,cn.grade from student , sc

where student.sno=sc.sno; 题40:去掉例38中的重复列。 答案: select distinct avg(grade),count(sno) 选课门数 from sc group by sno having count(sno)>3;

题41:查询计算机系学生的选课情况,要求列出学生的名字,所修课的课程号和成绩。 答案: select student.sname,sc.cno,sc.grade from sc left join student on student.sno=sc.sno where sc.sdept='计算机系';

题42:查询信息系选修VB课程的学生的成绩,要求列出学生姓名,课程名和成绩。 答案: select student.sname,course.cno,sc.grade from student join sc on student.sno=sc.sno join course on sc.cno=course.cno where sc.sdept='信息系' and course.cname='VB';

题43:查询所有选修了VB课程的学生的情况,要求列出学生姓名和所在的系。 答案:

select student.sname,sc.sdept from student join sc on student.sno=sc.sno join course on sc.cno=course.cno where course.cname='VB';

题44:查询与刘晨在同一个系学习的学生的姓名和所在系。 答案: select s2.sname, s2.sdept from student s1 , student s2 where s1.dept = s2.dept and s1.sname = '刘晨' and s2.sname != '刘晨';

题45:查询学生的选课情况,包括选修课程的学生和没有修课的学生。 答案:select * from student left join sc on student.sno=sc.sno; 题46:查询与刘晨在同一个系的学生。

答案:select sno,sname from student st1, student st2 where st1.sno = st2.sno and st2.sname='刘晨';

题47:查询成绩大于90分的学生的学号和姓名。

答案:select sno,sname,grade from student,sc where grade in (select grade from sc where grade>90)

题48:查询选修了“数据库基础”课程的学生的学号和姓名。 答案: select sno,sname from student where sno in (select sno from sc where cno in (select cno from course where cname='数据库基础'))

题49:查询选修了课程“c02”且成绩高于次课程的平均成绩的学生的学号和成绩。 答案: select sno,grade from sc where grade > ( select AVG(grade) from sc where cno='C02' group by cno)

题50:查询选修了课程“c01”的学生姓名。 答案: select sname from student where sno in (select sno from sc where cno='C01')

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

Top