sql語句練習題及答案_第1頁
sql語句練習題及答案_第2頁
sql語句練習題及答案_第3頁
sql語句練習題及答案_第4頁
全文預覽已結(jié)束

下載本文檔

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

文檔簡介

1、一 在數(shù)據(jù)庫 school 中建立 student , sc, course 表。學生表、課程表、選課表屬于數(shù)據(jù)庫School ,其各自的數(shù)據(jù)結(jié)構(gòu)如下:學生 Student (Sno,Sname,Ssex,Sage,Sdept)序號列名含義數(shù)據(jù)類型長 度1Sno學號字符型(char)62Sname姓名字 符 型(varchar)83Ssex性別字符型(char)24Sage年齡整數(shù)(smallint)5sdept系科字 符 型(varchar)15課程表 course(Cno,Cname,Cpno,Ccredit)序 號列名含義數(shù)據(jù)類型長 度1Cno課程 號字符型(char)42cname課程

2、 名字 符 型(varchar)203Cpno先修 課字符型(char)44Ccredit學分短 整 數(shù)(tinyint)學生選課 SC(Sno,Cno,Grade)序 號列名含義數(shù)據(jù)類型長 度1Sno學號字符型(char)62Cno課程 號字符型(char)43Grade成績小數(shù)(decimal)12,2二設(shè)定主碼1 Student 表的主碼:sno2 Course表的主碼:cno3 Sc表的主碼:sno, cno1寫出使用 Create Table 語句創(chuàng)建表 student , sc, course 的SQL語句create table student(sno char(6), snam

3、e varchar(8), ssex char(2), sage smallint, sdept varchar(15), primary key(sno);Create table course( Cno char(4) primary key, Cname varchar(20), Cpno char(4), Ccredit tinyint )create table sc (sno char(6), cno char(4), grade decimal(12,2), primary key(sno,cno);cvalues(' 4001insert into studentval

4、ues(' 4002趙茵,男,20, ' SX')楊華,女,21, ' JXJ')2在student表中插入信息學號姓名性別年齡系科4001趙茵男20SX4002楊華女21JSJinsert into student4Delete1刪除所有JSJ系的男生'JSJ' and Ssex='男';Cno fromCourse where Cname 數(shù)據(jù)庫原理');delete from Student where Sdept=2刪除“數(shù)據(jù)庫原理”的課的選課紀錄delete from SCwhere Cno in (se

5、lectUpdate1修改0001學生的系科為:JSJUpdate student set sdept= ' JSJ' where sno= ' 0001 '2把陳小明的年齡加1歲,性別改為女。Update student set age=age+1,ssex= ' 女' where sname=' 陳小明'Select 查詢語句單表1 查詢年齡在19 至 21 歲之間的女生的學號, 姓名 , 年齡 , 按年齡從大到小排列。select sno, sname, sage from studentwhere ssex= 女 and

6、sage between 19 and 21 order by sage desc;2 查詢姓名中第2 個字為“明”字的學生學號、性別。select sno, ssex from student where sname like _明 % ;3 查詢 1001 課程沒有成績的學生學號、課程號select sno, cno from scwhere grade is null and cno= 1001 ;4查詢JSJ、SX、WL系的年齡大于25歲的學生學號,姓名,結(jié)果按系排列 select sno, snamefrom studentwhere sdept in ( JSJ , SX , WL

7、 ) and sage>25group by sdept;5 查詢 student 表中的學生共分布在那幾個系中。( distinct )select distinct sdept from student ;6 查詢 0001 號學生 1001, 1002 課程的成績。select gradefrom scwhere sno= 0001 and (cno= 1001 or cno= 1002 ) ;二 統(tǒng)計1 查詢姓名中有“明”字的學生人數(shù)。select count(*) from student where sname like %明 % ;2計算JSJ'系的平均年齡及最大年

8、齡。select avg(sage),max(sage) from student where sdept= JSJ ;3 計算每一門課的總分、平均分,最高分、最低分,按平均分 由高到低排列select cno,sum(grade),avg(grade),max(grade),min(grade) from scgroup by cnoorder by avg(grade) desc ;4計算 1001,1002 課程的平均分。select cno, avg(grade) from sc where cno in( 1001 , 1002 )group by cno ;5 查詢平均分大于80

9、分的學生學號及平均分select ,avg(grade) from scgroup byhaving avg(grade)>80 ;6 統(tǒng)計選修課程超過2 門的學生學號select sno from sc group by sno having count(*)>2 ;7 統(tǒng)計有 10 位成績大于85 分以上的課程號。select cno from sc where grade>85 group by cno having count(*)=10 ;8 統(tǒng)計平均分不及格的學生學號select sno from sc group by sno having avg(grade)<60 ;9 統(tǒng)計有大于兩門課不及格的學生學號select sno from sc where grade<60 group by sno having count(*)>2 ;三 嵌套、相關(guān)及其他10 查詢平均分不及格的學生人數(shù)select count(*) from student where sno in( select sno from sc group by sno having avg(grade)<60)11 查詢沒有選修1002 課程的學生的學生姓名select sname from student where sno not in(

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論