What’s more

山东大学 2020级数据库系统 实验一
山东大学 2020级数据库系统 实验二
山东大学 2020级数据库系统 实验三
山东大学 2020级数据库系统 实验四
山东大学 2020级数据库系统 实验五
山东大学 2020级数据库系统 实验六
山东大学 2020级数据库系统 实验七
山东大学 2020级数据库系统 实验八、九

写在前面

做数据库实验一定要静得下心来,才能发现其中的错误然后进行改正。同时,如果发现 SQL 语句总是报错,“一定是你错了,只是不知道错在哪里”

其次,SQL 语句中较为复杂的点博主都进行了注释,希望大家一定要看懂思路后自己写一遍,而不是盲目的 Ctrl+C,Ctrl+V,切记切记!!

实验二

注意:实验二使用的表是:pub.student_course, pub.student, pub.course 哦,别搞错了!!

检索查询部分应该算是数据库中较为困难的一部分了,每道题我会先写出思路,同时在 SQL 语句中进行注释,希望能够看懂。

  • 2-1 找出没有选修任何课程的学生的学号、姓名(即没有选课记录的学生)
    思路:

    1. 找出有选课记录的同学的学号,不妨记为关系 A;
    2. 使用 not in 在 pub.student 中筛选不在 A 中有记录的学生的学号及姓名;
create view test2_01 asselect sid, namefrom pub.studentwhere sid not in(select sid           -- 选出有选课记录的同学的学号from pub.student_course)
  • 2-2 找出至少选修了学号为“200900130417”的学生所选修的一门课的学生的学号、姓名(不包含这名同学)。
    思路 1:

    1. 选出该同学所选修了的课程号 cid,作为集合 A;
    2. 使用 = some 来对 pub.student_course 中每个同学的课程号 cid 进行筛选;
    3. 最后在 pub.student 中选出对应的学号 sid 及姓名 name

    思路 2:

    1. 选出该同学所选修了的课程号 cid,作为集合 A;
    2. 使用 in 根据 A 筛选出课程号在集合 A 中的同学对应的学号及姓名;
    3. 集合运算 minus(except) 减去该同学的学号及姓名
---------------思路 1---------------------
create view test2_02 asselect sid, namefrom pub.studentwhere sid in(select sidfrom pub.student_coursewhere sid <> '200900130417'        -- 不包含这名同学and cid = some               -- 课程号至少有一个与 A 中元素相同的才满足条件(select cidfrom pub.student_coursewhere sid = '200900130417'))
---------------思路 2---------------------
create view test2_02 as(select sid, name                    -- 选出对应的学生的学号及姓名(此时包括学号为‘200900130417’的这名同学)from pub.studentwhere sid in(select sid                   -- 选出至少有一个 cid 在 A 中的同学的 sidfrom pub.student_coursewhere cid in(select cid              -- 集合 Afrom pub.student_coursewhere sid = '200900130417')))except--(Oracle 中是 minus 哦~~)     -- 除去这名同学的信息(select sid, namefrom pub.studentwhere sid = '200900130417')
  • 2-3 找出至少选修了一门其先行课程号为“300002”号课程的学生的学号、姓名
    思路:

    1. 根据先行课程号 fcid 在 pub.course 中选出对应的选修课程号 cid,记为集合 A;
    2. 在 pub.student_course 中根据集合 A 筛选出所选课程在 A 中的学生的 sid,记为集合 B;
    3. 在 pub.student 中根据集合 B 筛选出对应学生的 sid 和 name;

    第 2 步以及第 3 步可以使用 natural join 进行连接。

create view test2_03 asselect sid, namefrom pub.student natural join pub.student_coursewhere cid in(select cidfrom pub.coursewhere fcid = '300002')
  • 2-4 找出选修了“操作系统”并且也选修了“数据结构”,但是没有选修“程序设计语言”的学生的学号、 姓名
    思路:

    1. 找出选修了“操作系统”的学生的 sid,记为集合 A;
    2. 找出选修了“数据结构”的学生的 sid,记为集合 B;
    3. 找出没有选修“程序设计语言”的学生的 sid,记为集合 C;
    4. (A intersect B) except C 即可;

    (注意 Oracle 中将 except 换为 minus 就行)

create view test2_04 asselect sid, namefrom pub.studentwhere sid in((select sidfrom pub.student_coursewhere cid in(select cidfrom pub.coursewhere name = '操作系统'))intersect(select sidfrom pub.student_coursewhere cid in(select cidfrom pub.coursewhere name = '数据结构'))except(Oracle 中是 minus 哦~~)(select sidfrom pub.student_coursewhere cid in(select cidfrom pub,coursewhere name = '程序设计语言')))
  • 2-5 查询 20 岁的所有有选课的学生的学号、姓名、平均成绩(avg_score,此为列名,下同)(平均成绩四舍 五入到个位)、总成绩(sum_score)
    思路:

    1. 使用 natural join 将 pub.student 和 pub.student_course 连接起来;
    2. 计算对应的信息即可;

    注意:函数 round(number, precision) 可以将 number 按照 precision 精确到对应位。round(avg(number), 0) 即代表将 score 的平均值保留到整数位。

create view test2_05 asselect sid, name, round(avg(score), 0) avg_score, sum(score) sum_scorefrom pub.student natural join pub.student_coursewhere age = 20group by sid, name
  • 2-6 查询所有课的最高成绩、最高成绩人数,test2_06 有四个列:课程号 cid、课程名称 name、最高成绩 max_score、最高成绩人数 max_score_count(一个学生同一门课成绩都是第一,只计一次,需要考虑刷 成绩情况,一个同学选了一个课程多次,两次都是最高分。如果结果错一行,可能就是没有考虑这种情 况,这里是为了考核“去重复计数”知识点的)。如果没有学生选课,则最高成绩为空值,最高成绩人 数为零
    此题应该算是实验二中最难的一道了。希望我能将思路说清楚
    主要用到的表如下:

    1. t1:得出 cid,name
    2. t2:得出max_score
    3. t5:得出max_score_count

    思路:
    1. t1, t2 表的求解过程不用多说,简单的分组查询(查询时一定要加上 cid ,因为后面要将它们连接起来);
    2. 由于需要数最高分的人数,因此首先要在查询 max_score_count 部分的 from 子句中计算出 max_score (t4 表);
    3. 然后针对 score 对学生进行筛选,让 t3.cid = t4.cid 以及 t3.score = t4.max_score,得出每门课的最高分的学生的信息;
    4. 再进行分组后 count 得到 max_score_count;
    5. 最后将这些表的信息连起来 t1.cid = t2.cid and t2.cid = t5.cid;

create view test2_06 asselect t1.cid, name, max_score, max_score_countfrom (select cid, namefrom pub.coursegroup by cid, name) t1,(select cid, max(score) max_scorefrom pub.student_coursegroup by cid) t2,(select t3.cid, count(distinct sid) max_score_countfrom pub.student_course t3,(select cid, max(score) max_scorefrom pub.student_coursegroup by cid) t4where t3.cid = t4.cidand t3.score = t4.max_scoregroup by t3.cid) t5where t1.cid = t2.cidand t2.cid = t5.cid
  • 2-7 查询所有不姓张、不姓李、也不姓王的学生的学号 sid、姓名 name
    思路 1:

    1. 直接使用函数 substr(string, p1, p2),该函数可以针对取得字符串相对应部分的字符。如 substr(name, 0, 1) 就可以取得学生姓名 name 的第一个字符,即为姓。
    2. 然后接着判断取得的字符串是否是“张”、“李”、“王”即可。

    思路 2:

    1. 使用 not like 子句对字符串进行处理即可;
------------------思路 1-------------------
create view test2_07 as select sid, namefrom pub.studentwhere substr(name, 0, 1) <> '张'and substr(name, 0, 1) <> '李'and substr(name, 0, 1) <> '王'
------------------思路 2-------------------
create view test2_07 asselect sid, namefrom pub.studentwhere name not like '张%'and name not like '李%'and name not like '王%'
  • 2-8 查询学生表中每一个姓氏及其人数(不考虑复姓),test2_08 有两个列:second_name、p_count
    思路:

    1. 使用 substr 函数来取得学生的姓,然后进行 count 即可;
create view test2_08 asselect substr(name, 0, 1) second_name, count(*) p_countfrom pub.studentgroup by substr(name, 0, 1)
  • 2-9 查询选修了 300003 号课程的学生的 sid、name、score
    思路:

    1. 使用 natural join 将 pub.student 和 pub.student_course 进行连接;
    2. 从中寻找对应的 sid, name 以及 score;
create view test2_09 asselect sid, name, scorefrom pub.student natural join pub.student_coursewhere cid = '300003'
  • 2-10 找出同一个同学同一门课程有两次或以上不及格的所有学生的学号、姓名(即一门课程需要补考两次 或以上的学生的学号、姓名)
    思路 1:

    1. 先选出 score < 60 的学生,并将此时的 pub.student_course 记为 S;
    2. 然后将子查询中的 pub.student_course 记为 T,在 S.cid = T.cid, T.score < 60 的条件下,查找是否还存在记录;
    3. 若存在,则满足条件;不满足则不输出;

    思路 2:

    1. 选出分数 < 60 的学生的 sid 以及 cid,并按照这两个标准对 pub.student_course进行分组;
    2. 在分组的基础上使用 count 来数出每一组的人数。若人数 > 2,则满足条件,记录其 sid;反之则不记录;
----------------思路 1-----------------
--思路 1有点缺陷,建议思路 2--
create view test2_10 asselect sid, namefrom pub.studentwhere sid in(select sidfrom pub.student_course Swhere score < 60and exists(select sidfrom pub.student_course Twhere S.cid = T.cidand T.score < 60and S.score <> T.score))
----------------思路 2-----------------
create view test2_10 asselect sid, namefrom pub.studentwhere sid in(select sidfrom (select sid, cid, count(*) s_countfrom pub.student_coursewhere score < 60group by sid, cid)where s_count >= 2)

再次强调:一定是看懂思路之后自己实践哈~~
有问题还请斧正!

山东大学 2020级数据库系统 实验二相关推荐

  1. 山东大学 2020级数据库系统 实验八、九

    What's more 山东大学 2020级数据库系统 实验一 山东大学 2020级数据库系统 实验二 山东大学 2020级数据库系统 实验三 山东大学 2020级数据库系统 实验四 山东大学 202 ...

  2. 山东大学 2020级数据库系统 实验七

    What's more 山东大学 2020级数据库系统 实验一 山东大学 2020级数据库系统 实验二 山东大学 2020级数据库系统 实验三 山东大学 2020级数据库系统 实验四 山东大学 202 ...

  3. 山东大学 2020级数据库系统 实验六

    What's more 山东大学 2020级数据库系统 实验一 山东大学 2020级数据库系统 实验二 山东大学 2020级数据库系统 实验三 山东大学 2020级数据库系统 实验四 山东大学 202 ...

  4. 山东大学 2020级数据库系统 实验五

    What's more 山东大学 2020级数据库系统 实验一 山东大学 2020级数据库系统 实验二 山东大学 2020级数据库系统 实验三 山东大学 2020级数据库系统 实验四 山东大学 202 ...

  5. 山东大学 2020级数据库系统 实验四

    What's more 山东大学 2020级数据库系统 实验一 山东大学 2020级数据库系统 实验二 山东大学 2020级数据库系统 实验三 山东大学 2020级数据库系统 实验四 山东大学 202 ...

  6. 山东大学 2020级数据库系统 实验三

    What's more 山东大学 2020级数据库系统 实验一 山东大学 2020级数据库系统 实验二 山东大学 2020级数据库系统 实验三 山东大学 2020级数据库系统 实验四 山东大学 202 ...

  7. 山东大学 2020级数据库系统 实验一

    What's more? 山东大学 2020级数据库系统 实验一 山东大学 2020级数据库系统 实验二 山东大学 2020级数据库系统 实验三 山东大学 2020级数据库系统 实验四 山东大学 20 ...

  8. 山东大学2018级操作系统实验二

    题目描述 设有二元函数 f(x,y) = f(x) + f(y), 其中,f(x)=f(x-1)*x (x>1), f(x)=1 (x=1) f(y) = f(y-1) + f(y-2) (y& ...

  9. 数据库系统实验二作业-SQL实验报告

    实验二 交互式SQL 1(DDL语言实践) 1 实验目的 a) ****熟悉数据库的交互式SQL工具****.如:MySQL 查询分析器. b) *掌握SQL语言的DDL子语言,在MySQL环境下实现 ...

最新文章

  1. 【Qt】在ubuntu上打包发布Qt程序,可以不依赖Qt环境
  2. 关于JSON CSRF的一些思考
  3. 【渝粤教育】国家开放大学2018年春季 0529-22T高级英语阅读(1) 参考试题
  4. static关键字_一题搞定static关键字
  5. ASP.NET和ASP程序防止在IE中进行缓存
  6. GalGame汉化教程(一)——Ollydbg修改编码和字体
  7. 第四届江西省高校网络安全技能大赛初赛WebMisc—Writeup
  8. Java将json格式数据转换为对象
  9. mysql安装步骤图解5.0_MySQL5.0安装图解
  10. phpspider 简单使用
  11. 前端中适配各种手机模式的一种解决办法
  12. springcloud服务假死
  13. Garbled Circuits介绍 - 4 混淆电路的优化
  14. deepfake 图片_找到那张假照片!对抗Deepfake之路
  15. 对比企业邮箱服务的好处
  16. php 通过sendcloud发送邮件附件功能
  17. 中国都市女性抗衰洞察:Z世代抗衰意识普遍觉醒,“纹”题成抗衰重点
  18. pgsql修改表中字段属性
  19. ATF 安全启动过程
  20. 【正则表达式大全-固定格式字符】

热门文章

  1. SpringBoot整合oss实现文件的上传,查看,删除,下载
  2. 无符号右移负数_关于负数的右移与无符号右移运算小结
  3. ecshop根目录调用_ECSHOP各文件夹功能说明
  4. docker停止容器后配置_Docker 自学笔记 - april_aaa
  5. 动态规划算法之数塔问题
  6. thinkphp三级分销小程序源码_山东谷道微信小程序商城源码带后台 公众号平台三级分销系统...
  7. java开发怎么优化代码,代码优化,该如何处理
  8. 的主机名_如何在Mac 上更改电脑的名称或本地局域网主机名?
  9. Java中常用的类,包,接口
  10. git学习入门~~~