




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、實(shí)驗(yàn)二 sql語言的基本操作實(shí)驗(yàn)?zāi)康暮鸵螅赫莆绽胹ql語句完成各種查詢操作的能力。重點(diǎn)掌握用select語句進(jìn)行各種查詢; 掌握insert語句的用法。 實(shí)驗(yàn)內(nèi)容:用sql語句完成一下的要求:1.查詢信息系(is)的所有學(xué)生信息select * from student where sdept=is2.查詢選修了“數(shù)學(xué)”課的所有學(xué)生名單select s.sno,sname from student s,course c,scwhere s.sno=sc.sno and o=o and cname=數(shù)學(xué)3.查詢至少選修了一門其直接先行課為5號課程的學(xué)生的姓名。select
2、sname from student s, sc, course cwhere s.sno=sc.sno and o=o and pcno=54.查詢?nèi)w學(xué)生的姓名和出生年份。select sname,year(now()-sage as 出生年份 from student5.查詢所有姓王的學(xué)生。select * from student where sname like 王%6.查詢選修了3號課程的學(xué)生姓名及成績,并按成績降序排序。select sname,gradefrom student s, scwhere s.sno=sc.sno and o=3order
3、 by grade desc7.查詢?nèi)w學(xué)生情況,查詢結(jié)果按所在系的系號升序排列,同一系中的學(xué)生按年齡降序排列。select *from studentorder by sdept asc,age desc8.計(jì)算2號課程的平均成績。select avg(grade)from scwhere cno=29.查詢選修了2號課程的學(xué)生的最高成績。select max(grade) from sc where cno=210.求各個(gè)課程號及相應(yīng)的選課人數(shù)。select cno as 課程號,count(sno) as 人數(shù)from scgroup by cno11.查詢至少選修了3門課程以上的學(xué)生學(xué)
4、號。select snofrom sc group by sno having count(*)212.查詢“數(shù)據(jù)庫”的間接先行課。select amefrom course c1,course c2,course c3where c1.cpno=o and ame=數(shù)據(jù)庫 and c2.cpno=o13.查詢平均成績最高的學(xué)生的學(xué)號和姓名。select top 1 sno,avg(grade)from scgroup by snoorder by avg(grade) desc14.查詢數(shù)學(xué)成績最高的學(xué)生的學(xué)號和姓名。select top 1 s.sn
5、o,sname,gradefrom student s,course c, scwhere s.sno=sc.sno and o=o and cname=數(shù)學(xué)order by grade desc15.查詢出成績最低學(xué)號最大的學(xué)生學(xué)號。select top 1 sc.sno,gradefrom scorder by grade asc,sno desc16.查詢成績高于學(xué)生平均成績的記錄。select *from scwhere grade(select avg(grade)from sc )17.查詢至少選修了1號課程和3號課程的學(xué)生學(xué)號。select sc1.snofro
6、m sc sc1,sc sc2where sc1.sno=sc2.sno and o=1 and o=318.查詢只選修了1號課程和3號課程的學(xué)生學(xué)號。select snofrom sc where cno=1 and sno in(select sno from sc where cno=3)and sno in(select sno from sc group by sno having count(cno)=2)19.查詢沒有選修1號課程的學(xué)生姓名。select distinct s.snamefrom student s, scwhere s.sno=sc.sn
7、o and o!=120.查詢選修了全部課程的學(xué)生姓名。select snamefrom student swhere not exist (select * from course c where not exist (select * from sc where s.sno=sc.sno and o=o)21.查詢至少選修了95002所選修的全部課程的學(xué)生學(xué)號。 select sc1.sno from sc sc1 where not exist (select * from sc sc2 where sc2.sno=95002 and not exist( s
8、elect * from sc sc3 where o=o and sc1.sno=sc3.sno)22.查詢沒有不及格課程的學(xué)生的學(xué)號和姓名。 select distinct sc.sno,s.snamefrom sc,student swhere sc.sno=s.sno and not exists (select * from sc sc2 where sc.sno=sc2.sno and sc2.grade60)23.查詢沒有不及格學(xué)生的課程的課程號和課程名。 select distinct o,amefrom sc ,course cwhe
9、re o=o and not exists (select * from sc sc2 where o=o and sc2.grade60)24.建立信息系學(xué)生視圖,并從視圖中查詢年齡最大的學(xué)生記錄。gocreate view is_student(sno,sname,sage)as select sno,sname,sagefrom s where sdept=isselect max(sage)from is_student1.用sql語句定義表student(sno,sname,ssex,sage,sdept),并加入如下約束:主鍵:sno;sna
10、me有唯一約束;sname,ssex,sage都不允許空;create table student (sno char(10) not null unique, sname char(20) not null unique, ssex char(2) not null, sage int not null, sdept char(20) not null, primary key (sno)2.用sql語句定義表course(cno,cname,cpno,credit),并加入如下約束:主鍵:cno;cname不允許空;create table course(cno char(10) not
11、null unique, cname char(20) not null, cpnochar(10), credit char(10), primary key (cno)3.用sql語句定義表sc(sno,cno,cj),并加入如下約束:主鍵:sno,cno;為sno定義名為lsno的默認(rèn)參照完整性;為cno定義名為lcno的默認(rèn)參照完整性;create table sc(snochar(10) not null, cnochar(10) not null, gradeint, primary key (sno,cno), constraint lsno foreign key (sno)
12、references student(sno), constraint lcno foreign key (cno) references course(cno);4.用sql語句向student表輸入如下元組:(95001,李勇,男,20,cs); (95002,劉晨,女,21,is);insert into studentvalues (95001,李勇,男,20,cs);另一組數(shù)據(jù)同上進(jìn)行插入。用sql語句向course表輸入如下元組:(1,數(shù)據(jù)庫,5,4);(2,數(shù)學(xué),null,2);insert into coursevalues (1,數(shù)據(jù)庫,5,4);另一組數(shù)據(jù)同上進(jìn)行插入。用s
13、ql語句向sc表輸入如下元組:(95001,1,92);(95001,2,85); (95002,2,90);insert into scvalues (95001,1,92);其它組數(shù)據(jù)同上進(jìn)行插入。5.執(zhí)行下列語句,并查看執(zhí)行結(jié)果。如果不能正確執(zhí)行給出錯(cuò)誤原因。insert into student values(95001,張力,男,20,cs);不能執(zhí)行,student中sno屬性為unique,student中已經(jīng)有學(xué)號為95001的學(xué)生信息了,所以不能再插入相同學(xué)號的學(xué)生信息。insert into student values(95003,李勇,男,20,cs);不能執(zhí)行,stu
14、dent中cname屬性為unique,student中已經(jīng)有姓名為李勇的學(xué)生信息了,所以不能再插入相同姓名的學(xué)生信息。insert into sc values(95004,1,92);不能執(zhí)行,根據(jù)參照完整性,在student表中沒有95004的信息,所以不能插入。delete from student where sno=95001;不能執(zhí)行,因?yàn)樵趕c表中有95001的信息。update course set cno=3 where cno=2;不能執(zhí)行,因?yàn)閟c表中有cno=2的信息。6.給student表的ssex列添加名為fm的約束,使其取值只能取男或女。alter table
15、studentadd constraint fm check (ssex in (男,女)執(zhí)行insert into student values(95005,張力,f,20,cs),查看執(zhí)行結(jié)果。不能進(jìn)行插入,因?yàn)?,所輸入的信息中性別必須是男或女。7.給student表的sage列添加約束,使其年齡不得超過20歲。查看約束是否能正確添加,并分析其原因。alter table studentadd constraint age check (sage 20)不能正確添加,alter table 語句與 column check 約束 age 沖突。該沖突發(fā)生于數(shù)據(jù)庫 學(xué)生信息,表 student, column sage,因?yàn)楸頂?shù)據(jù)有sage 20的信息。8.刪除約束lsno和lcno。alter table scdrop constraint lsno,lcno9.為sc表添加在列sno上的外鍵約束lsno1,并定義為級聯(lián)刪除。執(zhí)行delete from student where sno=95001;查看執(zhí)行結(jié)果。alter table scadd constraint lsno1 foreign key (sno) references student(sno)on delete cascade;由于是級聯(lián)刪除,所以除
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 私人裝修住房合同范本
- 10《父母多愛我》(教學(xué)設(shè)計(jì))2023-2024學(xué)年統(tǒng)編版道德與法治三年級上冊
- 防疫用餐合同范本
- 業(yè)務(wù)代管合同范本
- 11我是一張紙 教學(xué)設(shè)計(jì)-2023-2024學(xué)年道德與法治二年級下冊統(tǒng)編版
- 銀行注銷合同范本
- Module 11 unit 2 In England,you usually drink tea with milk 英文版教學(xué)設(shè)計(jì) 2024-2025學(xué)年外研版八年級英語上冊
- 理財(cái)擔(dān)保合同范本
- 2025屆高考英語語法填空解題技巧教學(xué)設(shè)計(jì)
- 4 上學(xué)路上(教學(xué)設(shè)計(jì))-部編版道德與法治一年級上冊
- 2024年江蘇食品藥品職業(yè)技術(shù)學(xué)院單招職業(yè)技能測試題庫有完整答案
- 2024年高考改革新方案
- 學(xué)前教育考題及答案
- 建筑施工現(xiàn)場環(huán)境保護(hù)與治理培訓(xùn)
- 第1課《我們的閑暇時(shí)光》教學(xué)設(shè)計(jì)
- 《中國傳統(tǒng)民歌欣賞》課件
- 模塊1鐵道線路養(yǎng)護(hù)與維修認(rèn)知《鐵道線路養(yǎng)護(hù)與維修》教學(xué)課件
- 高鐵無砟軌道精調(diào)精測課件
- 2024年企業(yè)規(guī)章制度修訂方案
- 聚焦任務(wù)的學(xué)習(xí)設(shè)計(jì)作業(yè)改革新視角
- 血管活性藥物靜脈輸注護(hù)理方法(中華護(hù)理學(xué)會團(tuán)體標(biāo)準(zhǔn)T CNAS 22-2021)
評論
0/150
提交評論