西北工業(yè)大學(xué)數(shù)據(jù)庫實驗報告2
《數(shù)據(jù)庫原理》實驗報告
題目:實驗二
基本表的數(shù)據(jù)操作
學(xué)號
姓名
班級
日期
2014302692
孟玉軍
10011402
16.10.13
一. 實驗內(nèi)容、步驟以及結(jié)果
1. 在圖形用戶界面中對表中的數(shù)據(jù)進行更新。(6分,每小題2分)
(1) 按照實驗一圖2.1-圖2.4中所示數(shù)據(jù),輸入SPJ數(shù)據(jù)庫所含四張表中的數(shù)據(jù)。
S表:
P表:
J表:
SPJ表:
(2) 修改S表的任意一條數(shù)據(jù)
把S表中S1的status數(shù)據(jù)進行了修改
(3) 刪除S表的任意一條數(shù)據(jù)。
刪除了S3的數(shù)據(jù)
2. 針對SPJ數(shù)據(jù)庫,用SQL語句完成下面的數(shù)據(jù)更新。(12分,每小題3分)
(1) 第五版教材第三章 第5題的8-11小題。
紅色零件顏色修改為藍色:
⑧UPDATE P
SET COLOR=’藍’
WHERE SNO=’紅’
⑨由S5供給J4的零件P6改為由S3提供:
Update SPJ
Set sno=’S3’
Where sno=’S5’and pno=’P6’and jno=’J4’
⑩從供應(yīng)商關(guān)系中刪除S2的記錄 ,從供應(yīng)關(guān)系表中刪除相應(yīng)的記錄:
Delete
From S
Where sno=’S2’
Delete
From SPJ
Where sno=’S2’
11.請將(S2,J6,P4,200)插入供應(yīng)情況關(guān)系:
Insert into SPJ
Values (‘S2’,’ P4’,’J6’,200)
3. 針對SPJ數(shù)據(jù)庫,用SQL語句完成下面的數(shù)據(jù)查詢。(52分,每小題4分)
(1) 第五版教材第三章 第4題。(5道小題)
①:
select distinct sno
from SPJ
where jno=’J1’
②:
select distinct sno
from SPJ
where jno=J1and pno=P1
③:
select sno
from SPJ,P
where jno=J1and SPJ.pno=P.pno and color=紅
④:
select distinct jno
from SPJ
where jno not in
(select jno
from SPJ,P,S
where SPJ.sno=S.sno and city=天津
and SPJ.pno=P.pno and color =紅
)
⑤:select jno
from SPJ
where pno in
(select pno from SPJ where sno=S1 )
group by jno
having count(pno)=(select count(pno) from SPJ where sno =J1)
(2) 第五版教材第三章 第5題的1-7小題。(7道小題)
①:
select sno,sname from S
②:
select pname ,color ,weight from P
③:
select distinct jno
from SPJ
where sno=S1
④:
select pname,qty
from SPJ,P
where spj.pno=p.pno and jno=J2
⑤:
select distinct pno
from S,SPJ
where spj.sno=s.sno and city=上海
⑥:
select distinct jname
from spj,j,s
where spj.sno=s.sno and s.city =上海
and spj.jno=j.jno
⑦:
select distinct jno
from spj,s
where jno not in (select jno from spj,s
where spj.sno=s.sno and city=天津)
(3) 查詢這樣的工程:供給該工程的零件P1的平均供應(yīng)量大于供給工程J1的任何一種零件的最大供應(yīng)量:
select spj.jno,jname
from j,spj
where spj.jno=j.jno and pno=P1 and qty > (select MAX(qty)
from spj
where spj.jno=J1)
4. 針對Student數(shù)據(jù)庫用SQL語句完成下面的數(shù)據(jù)查詢。(12分,每小題4分)
(1) 查詢不選修“C語言”課程的學(xué)生學(xué)號。
select distinct sno
from sc
where sno not in (select sno
from sc,c
where cname=C語言 and c.cno=sc.cno)
(2) 查詢每個學(xué)生已經(jīng)獲得的學(xué)分的總分(成績及格表示獲得該門課的學(xué)分),并按照所獲學(xué)分由高到低的順序輸出學(xué)號,姓名,所獲學(xué)分。
select SC.sno,sname ,sum(ccredit) as ccredit_sum
from C,S,SC
where sc.cno=c.cno and grade>60 and sc.sno=s.sno
group by SC.sno,sname
order by ccredit_sum desc
(3) 查詢這樣的學(xué)生姓名:該學(xué)生選修了全部課程并且其中一門課在90分以上。
select sc.sno,sname
from sc ,s
where sc.sno=s.sno
group by sc.sno,sname
having max(grade)>90 and count(*)=(select count(*)from c)
沒有符合要求的數(shù)據(jù)項
5. 針對Student數(shù)據(jù)庫用至少三種不同的SQL語句進行查詢:查詢選修了課程名為“數(shù)據(jù)庫原理”的學(xué)生學(xué)號和姓名,然后自己設(shè)計實驗,用數(shù)據(jù)比較分析三種查詢的效率。(18分)
(1). 自然連接
select sc.sno,sname
from s,sc,c
where s.sno=sc.sno and sc.cno=c.cno and cname =數(shù)據(jù)庫
(2).嵌套子查詢+自然連接
select distinct sc.sno,sname
from s,sc
where s.sno=sc.sno and sc.sno =(select sno from c,sc
where sc.cno=c.cno and cname=數(shù)據(jù)庫 )
(3).嵌套查詢
select sno,sname
from s
where sno in
(select sno
from sc
where cno in
(select cno
from c
where cname =數(shù)據(jù)庫) )
結(jié)論:自然連接代碼最短,但是速度最快
嵌套查詢速度次之
自然連接和嵌套查詢結(jié)合慢
二. 實驗中出現(xiàn)的問題以及解決方案(對于未解決問題請將問題列出來)
除了標題內(nèi)容以外,該部分內(nèi)容中還可以寫對于實驗的一些感受,建議,意見等。
1.熟悉了一些具體的操作,查詢操作的一些注意事項
2.掌握了利用datediff函數(shù)求查詢運行時間
批閱者:
批閱日期:
實驗成績:
批注: