【Advanced SQL-1 】大连理工软件学院_数据库第四次上机答案
来源:互联网 发布:mac版百度云好友分享 编辑:程序博客网 时间:2024/05/21 11:47
这次还是之前的数据库,以下五道题对应教材第三章结尾部分
Using the university schema that you have write the following queries. In some cases you might need to insert extra data to show the effect of a particular feature.
//有些时候你需要修改数据库
第一题:
Insert each student as an instructor of department ‘拳脚学院’, with salary=40000
插入操作,没什么好说的,注意插之前判断一下教师表里是否已经存在这个人了
insert into instructorselect S.ID, S.name, '拳脚学院', 40000from student Swhere S.ID not in ( select ID from instructor );
第二题:
Now delete all the newly added "instructors" above (note: already existing instructors who happened to have salary=40000 should not get deleted)
删掉第一个问插入的数据
delete from instructorwhere ID in ( select ID from student ) anddept_name = '拳脚学院' andsalary = 40000;
第三题:
Update the salary of each instructor to 10000 times the number of course sections they have taught.
将每个讲师的工资更新为:他所教section数 * 10000
update instructorset salary = 10000 * (select COUNT(*)from teacheswhere teaches.ID = instructor.ID)直接执行代码,会发生错误:“UPDATE语句与***约束冲突”,原因是讲师表里对salary属性设置了CHECK约束,必须是numeric(8, 2),10000可能是默认int型并不符合规范,查阅微软MSDN提供的官方说明:
https://msdn.microsoft.com/zh-cn/library/aa292216(VS.71).aspx
我们可以获得解决这一问题的方法,如下:
右键数据库设计中发生CHECK冲突的列,选择CHECK约束
将下图所示项设为“否”
再次执行代码,操作成功!
第四题:
The university rules allow an F grade to be overridden by any pass grade (for example, A). Now, lists students who have fail grades that have not been overridden. For each student as such, information displayed (in one row) should involve:
·Identifier of student
·Name of student
·Count of F grades that have not been overridden.
找出那些挂科了,并且补考也没过或者还没参加补考的人,以及他们各自挂了几科。
统计出通过课程表,那些得了F并且不在通过课程表里的,这些就是补考仍然没过或者还没补考的。最后再COUNT统计数目即可。
with pass(ID, course_id) as(select distinct S1.ID, T1.course_idfrom student S1, takes T1where S1.ID = T1.ID andT1.grade != 'F'),not_pass(ID, name, course_id) as(select S.ID, S.name, T.course_idfrom student S, takes Twhere S.ID = T.ID andT.grade = 'F' andS.ID not in (select pass.ID from passwhere T.course_id = pass.course_id))select ID, name, COUNT(*) as '未通过科目数'from not_passgroup by ID, name;
第五题:
In one result, list the instructors who have never taught any courses and the students who have never registered for any courses. For each person, information displayed (in one row) should involve:
·Id of the person
·Name of the person
·Role of the person. The value of role should be ‘student’ or ‘instructor’.
找出那些一门课都没有选的学生,和一门课都不教的讲师,显示在同一个表里。
各自找出来后,使用集合并运算“union”即可。
select S.ID, S.name, 'student' as 'Role'from student Swhere S.ID not in ( select ID from takes)unionselect I.ID, I.name, 'instructor' as 'Role'from instructor Iwhere I.ID not in ( select ID from teaches)
- 【Advanced SQL-1 】大连理工软件学院_数据库第四次上机答案
- 【Intermediate SQL-1 】五道较难的sql练习_大连理工软院数据库上机答案
- c++第四次上机实验-1
- 再接再厉 第四次上机
- 第四次上机实验
- 第四次上机实验
- 第四次上机实验
- 第四次上机报告
- 第四次上机作业
- 第四次上机作业
- 第四次上机实验
- 第四次上机作业
- 第四次上机实验
- 第四次上机实验
- 第四次上机实验
- 第四次上机实验
- 第四次上机报告
- 第四次上机报告
- 我的新书《编程之法:面试和算法心得》终于上市 [内含勘误]
- MongDB shell 命令
- Windows共享文件夹的相关服务要启动
- OkHttp资料推荐 和 下载进度监听开源
- java中wait 和 sleep 区别
- 【Advanced SQL-1 】大连理工软件学院_数据库第四次上机答案
- x&(x-1)作用
- 【转】QPushButton的背景图片和文字同时显示
- Hibernate配置文件hibernate.cfg.xml中配置信息详解
- hicharts 每次显示一个图例的数据
- 自学第二天(2)
- java Compare
- [置顶] Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)
- Android开发相关的Blog推荐——跟随大神的脚步才能成长为大神