1. 程式人生 > >資料庫_實驗二

資料庫_實驗二

實驗二
(一)包括排序、分組的單表查詢
1.求數學系學生的學號和姓名。
select Sno,Sname from Student where Sdept='MA';

2.求選修了課程的學生學號。
select distinct Sno from SC;

3.求選修課程號為‘2’的學生號和成績,並要求對查詢結果按成績的降序排列,如果成績相同按學號的升序排列。
select Sno,Grade from SC where Cno='2' order by Grade desc,Sno asc;

4.求選修課程號為’2’且成績在80~90之間的學生學號和成績,並將成績乘以0.8輸出。
select Sno,0.8*Grade from SC where Cno='2' and Grade between 80 and 90;

*5.求數學系或計算機系姓張的學生的資訊。
select * from Student where Sdept in('MA','CS') or Sname like '張_%';

*6.求缺少了成績的學生的學號和課程號。
select Sno,Cno from SC where Grade is null;

*7.查詢各個課程號與相應的選課人數。
select cno,count(*) from SC group by cno;

(二) 多表連線查詢
1.查詢每個學生的情況以及他所選修的課程。
select * from Student,SC where Student.Sno=SC.Sno;

2.求學生的學號、姓名、選修的課程及成績。
select Student.Sno,Student.Sname,SC.Cno,SC.Grade from Student,SC where Student.Sno=SC.Sno;

3.求選修課程號為‘1’且成績在90分以上的學生學號、姓名和成績。
select Student.Sno,Student.Sname,SC.Grade from Student,SC where Student.Sno=SC.Sno and SC.Cno='1';

*4.查詢每一門課程的間接先行課。
Select  First.Cno,Second.Cpno From Course First,Course Second Where First.Cpno=Second.Cno;

*5.查詢與’劉晨’在同一個系學習的學生。
select s1.* from Student s1,Student s2 where s1.Sdept=s2.Sdept and s2.Sname='劉晨';

*6.查詢選修了課程名為‘資訊系統‘的學生學號和姓名。
select Student.Sno,Student.Sname from Student,SC,Course where Student.Sno=SC.Sno and SC.Cno=Course.Cno and Course.Cname='資訊系統';

7.查詢平均成績在80分以上的學生學號和平均成績。
select Sno,avg(Grade) from SC group by Sno having avg(Grade)>80;

8.查詢選修了1門以上課程的學生的學號。
select Sno,count(*) from SC group by Sno having count(*)>1;

(三) 巢狀查詢
1.求選修了資訊系統的學號和姓名。
select Sno,Sname from Student where Sno in(select Sno from SC where Cno=(select Cno from Course where Cname='資訊系統'));

2.查詢與劉晨在同一個系學習的學生。
select * from Student where Sdept in(select Sdept from Student where Sname='劉晨');

3.求選修1號課程的成績高於劉晨的成績(指劉晨選修的所有的課程的成績)的學生學號及成績。
select Sno,Grade from SC where Cno='1' and Grade> (select max(Grade) from SC where Sno=(select Sno from Student where Sname='劉晨'));

4.求其他系中比計算機系某一學生年齡小的學生(即年齡小於計算機系年齡最大者的學生)。
select * from Student where Sdept!='CS' and Sage < (select max(Sage) from Student where Sdept='CS');

5.求其他系中比計算機系學生年齡都小的學生姓名及年齡。
select Sname,Sage from Student where Sdept!='CS' and Sage < (select min(Sage) from Student where Sdept='CS');

6.求沒有選修3號課程的學生姓名。
select Sname from Student where Sno in(select Sno from SC where Cno!='3');

*7.查詢選修了全部課程的學生姓名。
select Sname from Student where not exists(select * from Course where not exists(select * from SC where Sno=Student.Sno and Cno=Course.Cno));

SQL語言中沒有全稱量詞∨(,all)。但是可以把帶有全稱量詞的謂詞轉換為等價的帶有存在量詞的謂詞。(∨x)P≡∟(exists x(∟P))
試做:查詢所有學生都選修的課程名
select Cname from Course where not exists(select * from Student where not exists(select * from SC where Sno=Student.Sno and Cno=Course.Cno));

*8.求至少選修了學號為“200215121”的學生所選修全部課程的學生學號和姓名。
select Sno,Sname from Student where Sno in(select distinct Sno from SC s1 where not exists(select * from SC s2 where s2.Sno='200215121' and not exists(select * from SC s3 where s1.Sno=s3.Sno and s2.Cno=s3.Cno)));

9.求選修課程超過2門的學生的學號和姓名。
select Sno,Sname from Student where Sno in(select Sno from SC group by Sno having count(*)>2);

(四) 檢視
1. 建立資訊系學生的檢視。並查詢此檢視,觀察結果。
create view StuView (Sno,Sname,Ssex,Sage) as select Sno,Sname,Ssex,Sage from Student where Sdept='IS';
select * from StuView;

2.(在檢視上建立)建立資訊系選修了1號課程的學生的檢視。查詢此檢視,並觀察結果。
create view StuView_1 (Sno,Sname,Ssex,Sage,Grade) as select StuView.Sno,Sname,Ssex,Sage,Grade from StuView,SC where Cno='1';
select * from StuView_1;

3.將學生的學號及其平均成績定義為一個檢視。查詢此檢視,觀察結果。
create view StuAvg (Sno,Avg) as select Sno,avg(Grade) from SC group by Sno;
select * from StuAvg;

4.將Student表中所有女生記錄定義為一個檢視F_stu(sno,sname,sdept,sex),並設定其更新限制with check option。
create view F_stu (sno,sname,sdept,sex) as select Sno,Sname,Sdept,Ssex from Student where Ssex='女' with check option;

5.對4中的檢視進行insert操作,將sno為200215129,sname為‘smith’,sdept為‘MA’插入檢視中,結果如何?
insert into F_stu (sno,sname,sdept,sex) values ('200215129','smith','MA');      插入失敗

6. 對4中的檢視進行insert操作,將sno為200215129,sname為‘smith’,sdept為‘MA’,sex為‘女’插入檢視中,結果如何?
insert into F_stu (sno,sname,sdept,sex) values ('200215129','smith','MA','女');     插入成功

5.6有什麼區別?

提示內容:
Create View <檢視名>[(<列名>[,<列名>]…)
 AS  <子查詢>
 [ With CHECK OPTION];
檢視:檢視子查詢中允許任意複雜的SELECT語句,但通常不允許含有Order By子句和distinct短語。
組成檢視的屬性列名或者全部省略,或者全部指定。
一下三種情況必須明確指定組成檢視的所有列名:
1)某個目標列不是單純的屬性名,而是聚集函式或列表達式。
2)多表連線時選出了幾個同名列作為檢視的欄位。
3)需要為某個列啟用新的更合適的名字。
With CHECK OPTION 表示對檢視進行update、insert、delete操作時要保證更新、插入或刪除的行滿洲子查詢中的條件表示式。

(五)思考題:
1.Where子句中能否用聚集函式作為條件表示式。
2.多表連線查詢中如果顯示的某一屬性不止一個表中出現,應如何處理。(例:(二)多表連線查詢中的第2題)。
3.在巢狀查詢中,什麼情況下用“IN”和“=”都可以。