数据库原理实验报告

这门课是大二学年数据库原理这门必修理论课中需要写报告的部分,要求学会使用sql语句,之前忘发了,今天想起来上传一下。

当时是和室友一起讨论着写完的,因为其中有几个问题实现起来还是比较复杂的,我推荐大家不要闷头自己写,否则万一写错了也不知道。但也不是推崇大家互相抄,我的意思是自己写完之后可以和同学对一下,双方互相改进。

这篇文章发出来的目的就是为了大家写完之后可以找一个标准的进行纠正。

正文:

实验题目 1:

从课程表(course)中查询所有课程信息。

答案:

select *
from course;
实验题目 2:

从课程表中(course)查询课程名。

答案:

select distinct title
from course;
实验题目 3:

从课程段(section)表中查询课程名称,要求消除值相同的那些行。

答案:

select distinct course_id
from section;
实验题目 4:

从学生(student)表中查询所有的信息,要求只显示查询结果的前 6 行数据。

答案:

select *
from student limit 6;
实验题目 5:

查询选了所有计算机学院开设课程的学生的姓名。

答案:

select name
from student natural join
(
select ID
from
(
select distinct ID,course_id
from takes
where course_id in(
select distinct course_id
from course natural join section
where dept_name="计算机"
)
)a
group by ID
having count(a.course_id)=(
select count(course_id)
from
(
select distinct course_id
from course natural join section
where dept_name="计算机"
)b
)
)c
实验题目 6:

查询 2019 年春季开课,但 2018 年不开课的课程的编号。

答案:

select course_id
from
(
select distinct course_id
from section
where semester="Spring" and year="2019"
)as y
where course_id not in(
select distinct course_id
from section
where year="2018"
);
实验题目 7:

假设毕业要求为修够 80 学分,请统计计算机学院内学生距离毕业要求还差多少学分,并按所

差分数的升序排列。

答案:

select ID,need
from
(
select ID,80 - tot_cred as need
from student
where dept_name="计算机"
)a
where need>0
order by need;
实验题目 8:

统计 2019 年春季所开课程段选课人数的最大值。

答案:

select max(ret)
from
(
select count(course_id)as ret
from
(
select course_id,sec_id
from takes
where semester="Spring" and year="2019"
)a
group by course_id,sec_id
)b
实验题目 9:

统计各个学院老师的平均年薪。

答案:

select dept_name,avg(salary)
from instructor
group by dept_name
实验题目 10:

请输出没有选择 2019 年春季开课的课程段的学生的姓名。

答案:

select name
from student
where ID not in (
select distinct ID
from takes
where semester="Spring" and year="2019"
)
实验题目 11:

请输出“张三”指导的学生在 2019 年春季开设课程段中所获得的总学分。(我们默认只要选择某

个 course 的一个 section,就可以获得这个 course 的学分;若选择同一个 course 的多个 section,

也只获得 1 次这个 course 的学分)。

答案:

select ID,sum(credits)
from (
select distinct ID,course_id,semester,year,credits
from takes natural join course
)c
where ID in(
select s_ID as ID
from advisor natural join(
select ID as i_ID
from instructor
where name="张三"
)a
)
group by ID
实验题目 11:

请输出“数据库系统原理”和“离散数学”的共同的先修课程的 ID。

答案:

select prereq_id
from (
select prereq_id
from prereq natural join course
where title="数据库系统原理"
)a
where prereq_id in(
select prereq_id
from prereq natural join course
where title="离散数学"
)
实验题目 12:

请统计 2016 年-2018 年,计算机学院每年开设的课程段的数量。

答案:

select year,count(*)
from(
select distinct year,course_id,sec_id
from section natural join course
where dept_name="计算机"
)a
group by year
having year in("2016","2017","2018")
实验题目 13:

查询课程编号不为“004”、“007”、“013”的课程编号和课程名称。

答案:

select course_id,title
from course
where course_id not in("004","007","013")
实验题目 14:

查询课程名以字母 D 开始,以“e”结尾的课程信息。

答案:

select *
from course
where title like "D%" and title like "%s"
实验题目 15:

查询课程名以“制作”两字作为中间字的课程信息。(要求“制作”不做开头和结尾)

答案:

select *
from course
where title like "%制作%"
实验题目 16:

查询姓名第二个字为“宝”的学生信息。

答案:

select *
from student
where name like "_宝%"
实验题目 17:

查询不姓“刘”的学生信息。

答案:

select *
from student
where ID not in(
select ID
from student
where name like "刘%"
)
实验题目 18:

查询那些在 2018 年有至少两个课程段的课程的 ID。

答案:

select course_id
from (
select course_id,sec_id
from section
where year="2018"
)a
group by course_id
having count(sec_id)>1
实验题目 19:

查询计算机学院学生选了非本学院老师开设的属于本学院的课程的情况,统计这些学生的 ID,

姓名,总学分。

答案:

select distinct ID,name,tot_cred
from (
select distinct ID,name,tot_cred,course_id,sec_id,semester,year
from takes natural join student
where dept_name="计算机"
)b
where (course_id,sec_id,semester,year)
in(
select course_id,sec_id,semester,year
from instructor natural join (
select ID,course_id,sec_id,semester,year
from teaches natural join course
where dept_name="计算机"
)a
where dept_name not in("计算机")
)
实验题目 20:

查询 2019 年春季开设的,选课人数少于 25 并且多于 15 人的课程段信息。

答案:

select *
from section natural join (
select course_id,sec_id,semester,year
from (
select ID,course_id,sec_id,semester,year
from takes
where semester="Spring" and year ="2019"
)a
group by course_id,sec_id
having count(*)>15 and count(*)<25
)b
实验题目 21:

查询 2018 年由非本学院开设的课程段的总数。如果没有,输出结果为 0。

答案:

不好意思老师,我无法理解这道题,按照教材数据库的设计,有一个课程段被开设,那肯定是
由本学院开设的,不可能存在由非本学院开设的课程段,所以结果为 0,但是我无法用 dml
语句描述,以下是我的一个语句,可以看出以我的数据库的设计无法得到结果:
select * from section natural join course where year="2018"
实验题目 22:

查询报名人数大于 25 或者少于 15 人的课程信息,要求查询结果按照报名人数降序排列。

答案:

select *
from course natural join (
select course_id,count(ID) as total_people
from(
select ID,course_id
from takes
)a
group by course_id
having count(ID)<15 or count(ID)>25
)c
order by total_people desc
实验题目 23:

查询与计算机学院同处于一座大楼的其他学院的老师的平均工资。

答案:

select dept_name,avg(salary)
from instructor
where dept_name in (
select dept_name
from department
where building in(
select building
from department
where dept_name="计算机"
) and dept_name<>"计算机"
)
group by dept_name
实验题目 24:

给在“匡亚明”大楼内办公的老师的工资增加到原来工资的 1.5 倍。

答案:

update instructor
set salary=salary*1.5
where dept_name in(
select dept_name
from department
where building="匡亚明大楼"
)
实验题目 25:

统计各个学院的学生的数量。

答案:

select dept_name,count(*)
from student
group by dept_name
order by count(*)
实验题目 26:

统计计算机学院中所获总学分排名前 10 位的学生的信息。

答案:

select *
from student
where dept_name="计算机" order by tot_cred desc
limit 10
实验题目 27:

查询“李四”老师在 2017-2019 年开设课程段的数量。

答案:

select count(*)
from teaches
where ID in (
select ID
from instructor
where name="李四"
) and year in("2017","2018","2019")
实验题目 28:

查询计算机学院老师中比生物学院工资最高的老师工资低,但是比生物学院最低工资高的老师

的信息。

答案:

select *
from instructor
where dept_name="计算机" and salary>(
select min(salary)
from instructor
where dept_name="生物"
) and salary<(
select max(salary)
from instructor
where dept_name="生物"
)
实验题目 29:

请统计 2019 年计算机学院比生物学院多开设了几个课程段。

答案:

select count(*)-(
select count(*)
from course natural join (
select *
from section
where year="2019"
)b
where dept_name="生物"
)
from course natural join (
select *
from section
where year="2019"
)a
where dept_name="计算机"
实验题目 30:

请统计 2019 年春季,没有学生选课的课程段的数量。若没有,请输出 0。

答案:

select count(*)
from section
where semester="Spring" and year="2019" and (course_id,sec_id)
not in (
select distinct course_id,sec_id
from takes
where semester="Spring" and year="2019"
)
实验题目 31:

请输出计算机学院学生的指导老师的姓名,去掉重复信息。

答案:

select name
from instructor
where ID in (
select distinct i_ID
from advisor
where s_ID in(
select ID
from student
where dept_name="计算机"
)
)
实验题目 32:

将低于整个学校的平均预算的各个学院的预算提高到原来的 1.2 倍。

答案:

update department
set budget=budget*1.2
where budget<(select avg(budget) from(select *from department)a)
实验题目 33:

统计“李四”老师指导的学生数量。若没有,请输出 0。

答案:

select count(*)
from advisor
where i_ID in(
select ID
from instructor
where name="李四"
)
实验题目 34:

查询已经选了“数据库原理“这门课的所有直接先修课程的学生的信息

答案:

select *
from student natural join(
select distinct ID
from takes
where course_id in(
select prereq_id
from prereq
where course_id in(
select course_id
from course
where title="数据库原理"
)
)
group by ID
having count(*)=(
select count(*)
from prereq
where course_id in(
select course_id
from course
where title="数据库原理"
)
)
)a
实验题目 35:

查询 2019 年春季选了自己指导教师开设的课程段的学生的姓名、指导老师的姓名和课程段的

ID。

答案:

select s_name,i_name,course_id,sec_id
from (
select name as s_name,ID as s_ID,course_id,sec_id,semester,year
from takes natural join student
where semester="Spring" and year="2019"
)a natural join(
select name as i_name,ID as i_ID,course_id,sec_id,semester,year
from teaches natural join instructor
)b
where (s_ID,i_ID)in(
select *
from advisor
)

附录:

其中表的插入语句,表的内容语句在此不赘述,请大家自行撰写。

  • 8
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HereIs_linwuwu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值