内容发布更新时间 : 2025/2/2 7:58:38星期一 下面是文章的全部内容请认真阅读。
数据库原理与应用实验答案
【篇一:数据库原理与应用实验作业参考答案】
=txt>实验1 数据库的建立修改与删除
实验2 表结构的建立修改删除及完整性约束条件定义 实验3 数据查询与更新
(一) 规定内容 1. 单表查询 use st
--[3_1] 查询全体学生的详细记录。 select * from student
--[3_2] 查询选修了课程的学生学号。 select distinct sno from sc
--[3_3] 将“学生”表中的sno、sname这2列合并为1列snosname输出(不改变表中存储的内容),其余列不变。 select sno+sname snosname,ssex,sage,sdept from student
--[3_4] 查询年龄不在20~23岁之间的学生姓名、系别和年龄。 方法1:
select sname,sdept,sage fromstudent
where sage not between 20 and 23; 方法2:
select sname,sdept,sage fromstudent
where sage20 or sage23;
--[3_5] 查询计算机科学系(cs)、数学系(ma)和信息系(is)学生的姓名和性别。 select sname,ssex from student
where sdept in ( cs,ma,is );
--[3_6] 查询所有姓“刘”学生的姓名、学号和性别。 select sname,sno,ssex from student
where sname like 刘%;
--[3_7] 查询名字中第2个字为阳字的学生的姓名和学号。 select sname,sno from student
where sname like _阳%;
--[3_8] 某些学生选修课程后没有参加考试,所以有选课记录,但没有考试成绩。
--查询缺少成绩的学生的学号和相应的课程号。 select sno,cno from sc where grade is null;
--[3_9] 查询计算机系年龄在20岁以下的学生姓名。 select sname from student
where sdept=cs and sage20;
--[3_10] 查询选修了3号课程的学生的学号及其成绩,查询结果按分数降序排列。 select sno,grade from sc
where cno=3
order by grade desc;
--[3_11] 查询全体学生情况,查询结果按所在系升序排列,同一系中的学生按性别降序排列。 select *
from student
order by sdept,sage desc; --使用集函数
--[3_12] 查询学生总人数。
select count(*) 学生总人数 from student; --[3_13] 查询选修了课程的学生人数。
select count(distinct sno) 选课人数 --注:用distinct以避免重复计算学生人数 from sc;
--[3_14] 计算1号课程的最高、最低及平均成绩。
select max(grade) 1号课程最高分, max(grade) 1号课程最低分, avg(grade) 1号课程平均成绩 from sc
where cno=1
--[3_15] 查询学生200215121选修课程的平均成绩。 select avg(grade) 学生200215121平均成绩 from sc
where sno=2002151 21
--[3_16] 查询学生200215122选修课程的总学分数。
select sum(ccredit) 学生200215122学分 from sc,course
where sc.cno=course.cno and sno=200215122; --使用group by子句分组
--[3_17] 求各个课程号及相应的选课人数。 select cno 课程号,count(sno) 选课人数 fromsc
group by cno;
--使用having短语筛选最终分组结果
--[3_18] 查询选修了3门(含3)以上课程的学生学号。 select sno from sc
group by sno
having count(*)=3 --也可为:count(cno)=3
--[3_19] 查询有2门以上课程是80分以上的学生的学号及(80分以上的)课程数 select sno 学号,count(*) 80分以上的课程数 fromsc
where grade=80
group by sno having count(*)=2 2. 连接查询 --自然连接
--[3_20] 查询每个学生的学号、姓名、课号及成绩。 select student.sno,sname,cno,grade from student,sc
where student.sno = sc.sno --左外连接
--[3_21] 查询每个学生的学号、姓名、课号及成绩(包括没有选修课程的学生)。 方法1:
select student.sno,sname,cno,grade from student,sc
where student.sno *= sc.sno 方法2:
select student.sno,sname,cno,grade
from student left join sc on student.sno = sc.sno 运行结果:
sno sname cno grade --------- -------- ---- ------- 200215121 李勇 1 97.0