您的位置:首页 > 数据库

数据库的修改练习

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。

代码为:

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
)


供读者自行琢磨。

运行的结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息