データベースの修正練習

4257 ワード

今回の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

数据库的修改练习_第1张图片
注意すべきは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

この問題のサブクエリでは、単一のテーブルを使用することを推奨します.複数のテーブルを使用すると、サブクエリの戻り値が1つ以上値しないことを示すため、クエリがエラーになります.サブクエリが1つのテーブルで要求を満たすことができない場合は、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
)

読者が自分で考えさせる.
実行結果:
数据库的修改练习_第2张图片