数据库的修改练习
2013-11-08 11:59
78 查看
这次的5道题涉及的均为对数据库的修改,所用的表依然是之前的那张表,链接为http://blog.csdn.net/cygeek_dut/article/details/13090743。
废话不多说,我们来看看这5道题:
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.
1. Insert each student as an instructor ofdepartment ‘拳脚学院’, with salary=40000
翻译过来,题目的意思是把每个学生插入并作为拳脚学院的老师,而且每个人的薪水为40000。
代码为:
值得注意的是where的语句必须加上,因为学生的ID和老师的有重复,另外ID又是主键,所以必须将该条件加上。不然新表中会出现重复的主键数据,这是不允许的。
2. Now delete all the newly added "instructors" above (note: already existing instructors who happened to have salary=40000 should not get deleted)
意思是:删除刚刚加入的那些数据。(千万不要误删噢)
参考答案:
结果和原本的instructor表相同。
在这里我们删除的是以学生的名字,并且salary为40000的(其实此题里满足学生名字的条件就够了,加个salary为了再降低风险)。
3. Update the salary of each instructor to 80000 times the number of course sections they have taught.
题目的含义是:更新教师表中的薪水值,其值等于他们教的section数量的80000倍。
正确的答案是:
这道题的子查询里面推荐使用单表,用多表会提示子查询的返回值不值不止一个,从而查询出错。如果子查询一个表满足不了要求的话,那么就用with as语句建立一个新表吧。(笔者暂时只能这么解释和解决这种错误了)
4. 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可以被通过的等级刷新。现在我们要列出那些有挂科成绩并且没有被刷新的学生。对于这些学生,需要包含的信息有:学生的ID,学生的姓名,学生的得F的个数。
代码:
此题比较简单,有些细节部分多试试就能得出正确的结果。
5. 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’.
这道题的意思是:列出所有没教课的老师,所有没选课的学生。对每个人,需要包含的信息有ID号,姓名,以及身份(老师或是学生)。
我的答案:
思路就是,我先查看了一下左连接的表,发现了如果没选课(或者教课)的,那么选课表的栏目均为null,由此启发。
另外附上标准答案:
供读者自行琢磨。
运行的结果:
废话不多说,我们来看看这5道题:
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.
1. Insert each student as an instructor ofdepartment ‘拳脚学院’, with salary=40000
翻译过来,题目的意思是把每个学生插入并作为拳脚学院的老师,而且每个人的薪水为40000。
代码为:
insert into instructor select ID,name,'拳脚学院',40000 from student where ID not in ( select ID from instructor) select * from instructor
值得注意的是where的语句必须加上,因为学生的ID和老师的有重复,另外ID又是主键,所以必须将该条件加上。不然新表中会出现重复的主键数据,这是不允许的。
2. Now delete all the newly added "instructors" above (note: already existing instructors who happened to have salary=40000 should not get deleted)
意思是:删除刚刚加入的那些数据。(千万不要误删噢)
参考答案:
delete from instructor where name in ( select name from student ) and salary=40000 select * from instructor
结果和原本的instructor表相同。
在这里我们删除的是以学生的名字,并且salary为40000的(其实此题里满足学生名字的条件就够了,加个salary为了再降低风险)。
3. Update the salary of each instructor to 80000 times the number of course sections they have taught.
题目的含义是:更新教师表中的薪水值,其值等于他们教的section数量的80000倍。
正确的答案是:
update instructor set salary = ( select count(*)*80000 from teaches group by ID having teaches.ID = instructor.ID ) select * from instructor
这道题的子查询里面推荐使用单表,用多表会提示子查询的返回值不值不止一个,从而查询出错。如果子查询一个表满足不了要求的话,那么就用with as语句建立一个新表吧。(笔者暂时只能这么解释和解决这种错误了)
4. 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可以被通过的等级刷新。现在我们要列出那些有挂科成绩并且没有被刷新的学生。对于这些学生,需要包含的信息有:学生的ID,学生的姓名,学生的得F的个数。
代码:
select student.ID,name,COUNT(*) as num from student,takes where student.ID=takes.ID and grade='F' group by student.id,name
此题比较简单,有些细节部分多试试就能得出正确的结果。
5. 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’.
这道题的意思是:列出所有没教课的老师,所有没选课的学生。对每个人,需要包含的信息有ID号,姓名,以及身份(老师或是学生)。
我的答案:
select student.ID,student.name,'student' as type from student left outer join takes on student.ID=takes.ID where ISNULL(takes.ID,0)=0 union select instructor.ID,instructor.name,'instructor' as type from instructor left outer join teaches on instructor.ID=teaches.ID where ISNULL(teaches.ID,0)=0
思路就是,我先查看了一下左连接的表,发现了如果没选课(或者教课)的,那么选课表的栏目均为null,由此启发。
另外附上标准答案:
select * from instructor left outer join teaches on instructor.ID=teaches.ID select ID,name,'instructor' as playrole from instructor where ID not in ( select distinct(ID) from teaches ) union select ID,name,'student' as playrole from student where ID not in ( select distinct(ID) from takes )
供读者自行琢磨。
运行的结果:
相关文章推荐
- AJAX 自学练习 无刷新提交并修改数据库数据并显示
- 数据库小练习---删除表内的重复部分、添加字段、删除字段、修改表名
- AJAX自学练习三:无刷新提交并修改数据库数据并显示
- AJAX 自学练习 无刷新提交并修改数据库数据并显示
- Javascript验证练习。请求师兄修改bug 。出错时候为什么不能显示 “确定”按钮??
- 在linux上安装完oracle数据库后,如何修改ORACLE_HOSTNAME
- 修改数据库参数,使其可以提供共享功能
- MySQL中新建用户,新建数据库,用户授权,删除用户,修改密码的相关操作
- 用SQL语句添加删除修改字段、一些表与字段的基本操作、数据库备份等
- plsql 通过修改配置文件的方式实现数据库的连接
- C# 连接数据库(添加,修改,删除)
- 读取文件 分割 后 修改数据库
- RAC修改数据库的spfile位置
- 修改数据库用户密码
- 报告论文:建立数据库 练习SQL语言进行数据定义、操作、控制
- JDBC的练习:用Statement对象往数据库里插入数据
- [置顶] 批量修改,删除数据库中 表名是以特定字符开头的表!
- ASP.NET中数据库的操作初步----增加、删除、修改
- Compiere修改数据库连接对话窗的图像
- SqlServer修改数据库文件存放位置