SQL語句創(chuàng)建學生信息數(shù)據(jù)庫表的示例_第1頁
SQL語句創(chuàng)建學生信息數(shù)據(jù)庫表的示例_第2頁
SQL語句創(chuàng)建學生信息數(shù)據(jù)庫表的示例_第3頁
SQL語句創(chuàng)建學生信息數(shù)據(jù)庫表的示例_第4頁
SQL語句創(chuàng)建學生信息數(shù)據(jù)庫表的示例_第5頁
已閱讀5頁,還剩3頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、用SQL語句創(chuàng)建如下三個基本表:學生表(Student)、課程 表(Course)、學生選課表(SC),結(jié)構(gòu)如下所示 Student表結(jié)構(gòu) 列名 說明 數(shù)據(jù)類型 約束 Sno 學號 字符串,長度為7 主碼 Sname 姓名 字符串,長度為10 非空 Ssex 性別 字符串,長度為2 取男或女 Sage 年齡 整數(shù) 取值1545 Sdept 所在院系 字符串,長度為20 默認為計算機系 Create table Student ( Sno varchar(7) primary key, Sname varchar(10) not null, Ssex char (2) check(Ssex= 男

2、or Ssex=女), Sage int check(Sage between 15 and 45), Sdept varchar(20) default(計算機系) ) Course表結(jié)構(gòu) 列名 說明 數(shù)據(jù)類型 約束 Cno 課程號 字符串,長度為10 主碼 Cname 課程名 字符串,長度為20 非空 Ccredit 學分 整數(shù) 取值大于0 Semester 學期 整數(shù) 取值大于0 Period 學時 整數(shù) 取值大于0 Create table course ( Cno varchar(10) primary key, Cname varchar(20) not null, Ccredit

3、 int check(Sctedit0), Semester int check(Semester0), Period int check(Period0) ) SC表結(jié)構(gòu) 列名 說明 數(shù)據(jù)類型 約束 Sno 學號 字符串,長度為7 主碼,引用Student的外碼 Cno 課程號 字符串,長度為10 主碼,引用Course的外碼 Grade 成績 整數(shù) 取值0100 Create table SC ( Sno varchar(7) foreig n key referen ces stude nt(S no), Cno varchar(10) foreig n key references c

4、ourse(C no), Grade int check(Grade between 0 and 100), Primary key (Sn o,C no) ) 1. 查詢學生選課表中的全部數(shù)據(jù)。 SELECT * FROM SC go 2. 查詢計算機系學生的姓名、年齡。 Select Sn ame,Sage From Stude nt Where Sdept=計算機系 3. 查詢成績在7080分之間的學生的學號、課程號與成績。 Select Sno,Cno ,Grade From Course,Sc Where course、cno=sc、Cno and sc、Grade between

5、70 and 80 4. 查詢計算機系年齡在1820之間且性別為“男”的學生的姓 名與年齡。 Select Sn ame,Sage From Stude nt Where Sage betwee n 18 and 20 and Ssex= 男and Sdept=計算機系 go 5. 查詢課程號為“ C01”的課程的最高分數(shù)。 最高分 C01 Select top 1 Grade select max(Grade) as From Scfrom Sc Where Cno= C01where Cno= Order by Grade desc order by Grade desc 6. 查詢計算機

6、系學生的最大年齡與最小年齡。 Select max(Sage) as 年齡最大 ,min(Sage) as 年齡最小 From Student Where Sdept= 計算機系 7. 統(tǒng)計每個系的學生人數(shù)。 Select count(Sdept) as學生人數(shù) ,Sdept From Student Group by Sdept 8. 統(tǒng)計每門課程的選課人數(shù)與考試最高分。 Select count(Sno) as 選課人數(shù) ,c 、 Sno,max(Grade) as 最高分 From Course c left join Sc s on c、 cno=s 、Cno Group by c 、

7、 Cno 9. 統(tǒng)計每個學生的選課門數(shù)與考試平均成績, 并按學號的升序 顯示結(jié)果。 Select sno,avg(grade) as平均成績 ,count (cno) as 選課門數(shù) From sc Group by sno Order by sno 10. 查詢總成績超過 200分的學生 ,要求列出學號、總成績。 Select sno,sum(grade) From sc Group by sno Having sum(grade)200 11. 查詢選修了課程“ C02的學生的姓名與所在系。 Select sname,sdept From student s1,sc s2 Where s1

8、 、sno=s2 、sno and s2 、 cno= c02 12. 查詢成績在 80 分以上的學生的姓名、 課程號與成績 , 并按成 績的降序排列結(jié)果。 Select s1 、 sname,s2 、 cno,s2 、 grade From student s1,sc s2 Where s1 、 sno=s2 、 sno and grade 80 Order by grade desc 13. 查詢哪些課程沒有人選修、 要求列出課程號與課程名。 Select c 、cno,c 、 cname From course c left join sc s on c Group by c 、 cno

9、,c 、 cname Having count(s 、 sno)=0 cno=s 、cno 14. 用子查詢實現(xiàn)如下查詢 (1) 查詢選修了課程“ C01”的學生的姓名與所在系。 Select sname,sdept ,sno From student Where sno in ( Select sno From sc Where cno= c01 (2) 查詢信息系成績在 80 分以上的學生的學號、姓名。 Select sno,sname From student Where sdept= 外語系 and sno in( Select sno From sc Where grade80 (3

10、) 查詢計算機系考試成績最高的學生的姓名。 Select s1 、 sname from students Where sdept= 計算機系 and sno in (select sno from sc Where grade in (select max(Grade)from sc) ) 15. 刪除選課成績小于 50分的學生的選課記錄。 Delete from sc Where grade70 Select* from sc 驗證 16. 將所有選修了課程“ C01”的學生的成績加10分: Update sc Set grade=grade+10 Where cno= c01 17. 將

11、計算機系所有選修了課程 “計算機文化基礎(chǔ)” 課程的學生 的成績加 10分。 Select*from sc Update sc Set grade=grade+10 Where cno in (select cno from course Where cn ame=計算機文化基礎(chǔ)) 18. 創(chuàng)建查詢學生的學號、姓名、所在系、課程號、課程名、課 程學分的視圖。 Select* from course Select* from students Select* from sc Create view 學生基本信息 As Select students 、 sno,sname,sdept,sc 、 c

12、no,cname,ccredit From course,sc,students Where course 、cno=sc、cno And sc 、 cno=students 、 sno 19. 創(chuàng)建查詢每個學生的平均成績的視圖 , 要求列出學生學號及 平均成績。 Create view s_avg As Select sno,avg(Grade)as 平均成績 from sc Group by sno 20. 創(chuàng)建查詢每個學生的選課學分的視圖 , 要求列出學生學號及 總學分。 Create view s_sc As Select students 、 sno,sum(ccredit)as 總

13、學分 from Students,sc,course Where students 、 sno=sc 、sno And sc 、 cno=course 、 cno Group by students 、 sno 21. 用SQL語句創(chuàng)建一個名為f_1的函數(shù),該函數(shù)能夠求出3 到 100 之間的所有素數(shù)之與。 Create function f_1() Returns int As Begin Declare a int,b int,i int,sum int Set i=3 Set sum=0 While i101 Begin Set b=0 While ax2 Return max End

14、Select dbo 、 f_2(2,6) 23. 用 SQL 語句創(chuàng)建一個名為 pro_get_stu_information 的存 儲過程,該存儲過程能夠根據(jù)用戶指定的Sno(學號)求出與 該學號對應的學生姓名、課程名、成績。 Create procedure pro_get_stu_information m char(6) output As Select sname,cname,grade from students,sc,course Where students 、 sno=sc 、sno and sc 、cno=course 、 cno and sc 、 sno=m Exec pro_get_stu_information 24. 為“學生”表創(chuàng)建一

溫馨提示

  • 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

提交評論