sql練習(xí)題+答案_第1頁
sql練習(xí)題+答案_第2頁
sql練習(xí)題+答案_第3頁
sql練習(xí)題+答案_第4頁
sql練習(xí)題+答案_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、(一)新建以下幾個表student(學(xué)生表):snosnamesexdeptbirthage其中約束如下:(1)學(xué)號不能存在相同的(2)名字為非空(3)性別的值只能是男或女(4)系包括這幾個:信息系,計算機科學(xué)系,數(shù)學(xué)系,管理系,中文系,外語系,法學(xué)系(5)出生日期為日期格式(6)年齡為數(shù)值型,且在0100之間create table student (sno smallint constraint a primary key,-設(shè)置學(xué)生學(xué)號為 student的主鍵sname varchar(10) not null,sex varchar(2) constraint b check(sex

2、in('男','女'),-檢查約束一一性別的值只能是男或女dept varchar(20) constraint c check(dept in('信息系','計算機科 學(xué)系',數(shù)學(xué)系,管理系,中文系,外語系,法學(xué)系'),-檢查約 束一一系包括這幾個:信息系,計算機科學(xué)系,數(shù)學(xué)系,管 理系,中文系,外語系,法學(xué)系birth datetime ,age smallint constraint d check(age between 0 and 100)-檢查 約束一一年齡為數(shù)值型,且在 100之間cs(成績表):snocno

3、cj其中約束如下:(1) sno和cno分別參照student和course表中的sno,cno的字段(2) cj(成績)只能在0100之間,可以不輸入值create table cs(sno smallint not null references student (sno),定義成外 鍵cno smallint not null references course(cno),定義成外 鍵cj smallint constraint e check(cj between 0 and 100),檢 查約束一一cj(成績)只能在100之間,可以不輸入值constraint f primary k

4、ey(sno,cno)-定義學(xué)生學(xué)號和課程 號為scg的主鍵)course (課程表)cnocname其約束如下:(1)課程號(cno)不能有重復(fù)的(2)課程名(cname)非空create table course(cno smallint not null constraint g primary key,-設(shè)置課程號為course的主鍵cname varchar(20) not null)(三)針對學(xué)生課程數(shù)據(jù)庫查詢(1) 查詢?nèi)w學(xué)生的學(xué)號與姓名Select sno,sname from student(2) 查詢?nèi)w學(xué)生的姓名、學(xué)號、所在系,并用別名顯示出結(jié)果。Select snam

5、e as'姓名',sno as'學(xué)號',dept as'所在地'from student(3) 查詢?nèi)w學(xué)生的詳細記錄。select * from student(4) 查全體學(xué)生的姓名及其出生年份。select sname,birth from student(5) 查詢學(xué)校中有哪些系select distinct dept from student(6) 查詢選修了課程的學(xué)生學(xué)號。select sno from cs where cno is not null(7) 查詢所有年齡在20歲以下的學(xué)生姓名及其年齡。select sname,age

6、 from student where age < 20(8) 查詢年齡在2023歲(包括20歲和23歲)之間的學(xué)生的姓名、系別和年 齡。select sname,dept ,age from student where age between 20and 23(9) 查詢年齡不在202聆之間的學(xué)生姓名、系別和年齡。select sname,dept ,age from student where age<20 or age>23(10)查詢信息系、數(shù)學(xué)系和計算機科學(xué)系生的姓名和性別。select sname,sex from student where dept ='

7、;信息系'or dept=' 數(shù)學(xué)系or dept='計算機科學(xué)系(11)查詢既不是信息系、數(shù)學(xué)系,也不是計算機科學(xué)系的學(xué)生的姓名和性別。select sname,sex from student where dept !='信息系'anddept!=數(shù)學(xué)系'and dept!=計算機科學(xué)系'(12)查詢所有姓劉學(xué)生的姓名、學(xué)號和性別。select sname,sno,sex from student where sname like('劉')(13)查詢學(xué)號為2009011的學(xué)生的詳細情況。(具體的學(xué)號值根據(jù)表中數(shù)據(jù)確定

8、)select * from student where sno=5(14)查詢姓“歐陽”且全名為三個漢字的學(xué)生姓名select sname from student where sname like('歐陽)(15)查詢名字中第2個字為“晨”字的學(xué)生的姓名和學(xué)號select sname,sno from student where sname like(晨')(16)查詢所有不姓劉的學(xué)生姓名。select sname,sno from student where sname not like('劉 ')(17)查詢sq牌程的課程號和學(xué)分。select cno

9、from course where cname='sql(18)查詢以"DB"開頭,且倒數(shù)第3個字符為i的課程的詳細情況。select * from course where cname like('DB_%i_')(19)查詢?nèi)鄙俪煽兊膶W(xué)生的學(xué)號和相應(yīng)的課程號select sno,cno from cs where cj is null(20)查所有有成績的學(xué)生學(xué)號和課程號。select sno,cno from cs where cj is not null(21)查詢計算機系年齡在20歲以下的學(xué)生姓名。select sname from stu

10、dent where age < 20 and dept='計算機科學(xué)系(使用多個條(22)查詢信息系、數(shù)學(xué)系和計算機科學(xué)系學(xué)生的姓名和性別件表達式)select sname,sex from student where dept='信息系'or dept ='數(shù)學(xué)系'or dept ='計算機科學(xué)系,(23)查詢年齡在202聆(包括20歲和23歲)之間的學(xué)生的姓名、系別和年 齡。(使用多個條件表達式)select sname,dept,age from student where age between 20and 23(24)查詢選修了

11、 3號課程的學(xué)生的學(xué)號及其成績,查詢結(jié)果按分數(shù)降序排列。select sno,cj from cs where cno=3 order by cj desc(25)查詢?nèi)w學(xué)生情況,查詢結(jié)果按所在系的系號升序排列, 同一系中的學(xué) 生按年齡降序排列。select * from student order by dept asc,age desc(26)查詢學(xué)生總?cè)藬?shù)。select count (*) from student(27)查詢選修了課程的學(xué)生人數(shù)。select count (sno) from cs where cno is not null(28)計算1號課程的學(xué)生平均成績selec

12、t avg(cj) from cs where cno=1(29)查詢選修1號課程的學(xué)生最高分數(shù)。select max(cj) from cs where cno=1(30)求各個課程號及相應(yīng)的選課人數(shù)。select ,count from course left join cson = group by(31)查詢選修了 3門以上課程的學(xué)生學(xué)號。select sno, count (cno) from cs group by sno havingcount (cno)>3(32)查詢有3門以上課程是90分以上的學(xué)生的學(xué)號及(90分以上的)課程數(shù)。select sno, count(cn

13、o) as'課程數(shù)'from cs where cj>90group by sno having count (cno)>=3(33)查詢學(xué)生2006011選修課程的總學(xué)分。select sum(course) from course,cs where = and =2006011(34)查詢每個學(xué)生選修課程的總學(xué)分。select sno,sum(cj)from cs,coursewhere =group by snounionselect sno, 0 from studentwhere sno not in (select sno from cs)(35)查詢每

14、個學(xué)生及其選修課程的情況。select ,course.* from cs,course where =(36)查詢選修2號課程且成績在90分以上的所有學(xué)生的學(xué)號、姓名select sno,sname from student where sno=(select sno from cswhere cno=2 and cj>90)(37)查詢每個學(xué)生的學(xué)號、姓名、選修的課程名及成績select ,sname,from student ,course,cs where = and =(38)查詢與“劉晨”在同一個系學(xué)習(xí)的學(xué)生(分別用嵌套查詢和連接查詢)-嵌套查詢select * from s

15、tudent where dept in(select dept from student where sname='劉晨')-連接查詢select stul .* from student as stul ,student as stu2where = and ='劉晨'-exists 查詢select * from student s1 where exists(select * from student s2 where = and='劉晨')(39)查詢選修了課程名為“信息系統(tǒng)”的學(xué)生學(xué)號和姓名select sno,sname from

16、student where sno in(select sno from cs where cno in(select cno from course wherecname='信息、系統(tǒng),)(40)查詢其他系中比信息系任意一個(其中某一個)學(xué)生年齡小的學(xué)生姓名和 年齡select sname,age from student where age <any(select age from student where dept='信息系')(41)查詢其他系中比信息系所有學(xué)生年齡都小的學(xué)生姓名及年齡。分別用ALL胃詞和集函數(shù)用 ALLselect sname,age

17、from student where age <all(select age from student where dept='信息系')聚合函數(shù)select sname,age from student where age <(select min (age) from student where dept ='信息系')(42)查詢所有選修了 1號課程的學(xué)生姓名。(分別用嵌套查詢和連查詢)-嵌套查詢select sname from student where sno in(select sno from cs where cno=1)-連接查詢

18、select sname from student ,cswhere = and =1(43)查詢沒有選修1號課程的學(xué)生姓名。select sname from student where sno in(select sno from cs where cno!=1)(44)查詢選修了全部課程的學(xué)生姓名。select sname from student where not exists(select * from course where not exists(select * from cs where=and=)(45)查詢至少選修了學(xué)生95002選修的全部課程的學(xué)生號碼。select

19、distinct sno from sc scxwhere not exists (select * from cs scywhere ='95002' and not exists (select * from sc sczwhere = and =)(46)查詢計算機科學(xué)系的學(xué)生及年齡不大于19歲的學(xué)生的信息。select * from student where dept='計算機科學(xué)系 'or age<19(47)查詢選修了課程1或者選修了課程2的學(xué)生的信息。select student .* from student ,cs where = an

20、d =1 or =2)(48)查詢計算機科學(xué)系中年齡不大于19歲的學(xué)生的信息。select * from student where age<=19 and dept ='計算機科學(xué)系'(49)查詢既選修了課程1又選修了課程2的學(xué)生的信息select * from student where sno in(select sno from cs where cno='003' and sno in(select sno from cs where cno='004')-用exists查詢select * from student where exists (select * from cs where = andcno='003' and sno in(select sno from cs where cno='004')(50)查詢計算機科學(xué)系的學(xué)生與年齡不大于19歲的學(xué)生的差集。select * from student

溫馨提示

  • 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論