数据库系统原理——数据库定义与操作语言(一)

数据库定义与操作语言

一、ScoreDB中各个表

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、实验目的

理解和掌握数据库SQL DDL语句的语法;
掌握SQL 程序设计基本规范,熟练运用SQL语言实现数据基本查询语句、嵌套查询等;
熟悉数据库的数据更新操作;
熟练SQL有关视图的操作。

三、实验内容

导入ScoreDB数据库,按照“实验2数据”,修改四张表内各个字段的类型以及宽度。完成后,实现以下操作:
2.1数据查询

  1. 查询选修了“数据结构”课程的同学姓名。
SELECT StudentName
FROM Student,Course,Score
WHERE Student.studentNo=Score.studentNo AND Course.courseNo=Score.courseNo AND courseName='数据结构'
  1. 查询选修了课程号为’001’或’002’课程的学生学号、课程号和分数。
SELECT studentNo,courseNo,score
FROM Score
WHERE courseNo='001' OR courseNo='002'
  1. 查询学过课程号为’001’但是没有学过课程号为’002’的课程的学生学号、课程号和分数。
select studentNo,courseNo,score
from Score S1
where  courseNo!='002' and exists(select  * from Score as S2 where courseNo='001')

select studentNo, courseNo, score
from Score
where studentNo in (select studentNo from score where courseNo='001') and studentNo not
in (select studentNo from score where courseNo='002')

4.查询至少有一门课与学号为’0700001’的学生所学相同的学生的学号和姓名。

select distinct studentName,Student.studentNo
from Student,Score
where Student.studentNo=Score.studentNo and courseNo in(select courseNo from Score where studentNo='0700001')

select distinct a. studentNo, studentName
from Student a, score b
where a. studentNo=b. studentNo and courseNo in (select courseNo from score 
where studentNo ='0700001' ) and a. studentNo<>'0700001'

5.查询至少选修了学号为“0700001”学生所选修的所有课程的学生姓名。

select distinct studentName
from Student,Score x
where Student.studentNo=x.studentNo and not exists(select* from Score y where y.studentNo='0700001' and not exists(select* from Score z where z.studentNo=x.studentNo and z.courseNo=y.courseNo))

ps:
如果只需要查询学号可以用如下代码

select distinct studentNo
from Score x
where   not exists(select* from Score y where y.studentNo='0700001' and not exists(select* from Score z where z.studentNo=x.studentNo and z.courseNo=y.courseNo))

6.查询本月过生日的学生信息。

select *
from Student
where MONTH(birthday)=MONTH(getdate())

7.查询没有成绩的学生信息。(使用IN语句)

select *
from  Student
where studentNo in(select studentNo from Score where score is null)

8.查询(没有)选修过课程的学生姓名。(使用IN语句)

select studentName,studentNo
from Student
where studentNo not in(select studentNo from Score )

9.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩。

select Student.studentNo,studentName,S.avg_score
from Student,(select Score.studentNo,avg(score) avg_score from Score where score<60 group by Score.studentNo having count(*)>=2) as S
where S.studentNo=Student.studentNo

select a.studentNo, studentName, avg (score) avg_ score
from score a, student b
where a.studentNo in (select studentNo from score where score<60
group by studentNo
having count(*)>=2) and a. studentNo=b. studentNo
group by a. studentNo, studentName

10.查找选修过课程名中包含“系统”的课程的同学学号、姓名和所在班级。

select Student.studentNo,studentName,classNo
from Student,Score,Course
where Student.studentNo=Score.studentNo and Score.courseNo=Course.courseNo and courseName like'%系统%'

11.查找同时选修过“高等数学”和“离散数学”两门课程的同学
学号、姓名以及该同学所选修的所有课程的课程名和相应成绩,按学号(升序)、成绩(降序)排序输出。

select Student.studentNo,studentName,courseName,score
from Student,Score,Course
where Student.studentNo=Score.studentNo  and Score.courseNo=Course.courseNo and Student.studentNo IN(SELECT Score.studentNo from Score,Course
where courseName='高等数学'and Course.courseNo=Score.courseNo and studentNo in(select Score.studentNo from Score,Course where courseName='离散数学'
and Course.courseNo=Score.courseNo))
order by Student.studentNo ASC,score DESC

select s. studentNo, studentName, courseName, score
from Student s, Course c, Score sc
where s. studentNo =sc. studentNo and c. courseNo=sc. courseNo
and s. studentNo in (select studentNo
from Score
where courseNo in (select courseNo from Course where courseName ='
高等数学'))
and s. studentNo in(select studentNo
from Score
where courseNo in (select courseNo from Course where courseName='离
散数学'))
order by studentNo asc,score desc

12.查询所有学生的选修信息,显示信息包括学号,选修门数,平均分和总分。

select studentNo,COUNT(courseNo) cnt ,avg(score) avg_s,sum(score) sum_s
from Score
group by(studentNo)

13.查询先修课是“计算机基础”的课程。(分别用自连接和IN语句实现)
①自连接

select C1.courseNo
from Course C1,Course C2
where C1.priorCourse=C2.courseNo and C2.courseName='计算机基础'

②IN语句
方法1:

select courseNo
from (select C1.courseNo
from Course C1,Course C2
where C1.priorCourse=C2.courseNo and C2.courseName='计算机基础') as C

方法2:

select courseNo
from Course
where priorCourse in (select courseNo from Course where courseName='计算机基础')

14.查询至少有一门课程成绩超过85分的所有学生的信息,显示信息包括学号、课程号和分数。

select Student.studentNo,Score.courseNo,score
from Student,Score
where Student.studentNo=Score.studentNo and Score.studentNo in (select studentNo from Score where score>85)

select *
from score
where score>85

15.查询至少有两门课程成绩超过85分的所有学生的学号。

select studentNo 
from Score 
where score>85
group by (studentNo)
having count(*)>=2

16.查询平均成绩超过80分且选修3门及以上的所有学生的信息,显示信息包括学号、课程数目和分数。

select Student.studentNo,sc.courseNo,cnt_course,sc.score
from Student,(select s.studentNo,count(courseNo)cnt_course from Score s group by s.studentNo having avg(s.score)>80 and count(*)>=2) as S,Score sc
where s.studentNo=Student.studentNo and s.studentNo=sc.studentNo

select studentNo, avg (score) avg_ score, count (*) count_ _cno
from score
group by studentNo
having avg(score)>80 and count (*)>=3

17.查询所选修课程的成绩大于所有“002”号课程成绩的同学学号及相应课程的课程号和成绩。

select studentNo,courseNo,score
from Score
where score >(select MAX(score) from Score where courseNo='002')

select studentNo, courseNo, score
from Score
where (score>all(select score from Score where courseNo='002') )

18.查询选修了所有课程的学生姓名。

select studentName
from Student
where not exists(select *from Course where not exists (select *from Score where studentNo =Student.studentNo and courseNo=Course.courseNo))
  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值