版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、(一) 新建以下幾個(gè)表student(學(xué)生表):snosnamesexdeptbirthage其中約束如下:(1) 學(xué)號(hào)不能存在相同的(2) 名字為非空(3) 性別的值只能是男或女(4) 系包括這幾個(gè):信息系,計(jì)算機(jī)科學(xué)系,數(shù)學(xué)系,管理系,中文系,外語(yǔ)系,法學(xué)系(5) 出生日期為日期格式(6) 年齡為數(shù)值型,且在0100之間create table student(sno smallint constraint a primary key,-設(shè)置學(xué)生學(xué)號(hào)為student的主鍵sname varchar(10) not null,sex varchar(2) constraint b check
2、(sex in('男','女'),-檢查約束性別的值只能是男或女dept varchar(20) constraint c check(dept in('信息系','計(jì)算機(jī)科學(xué)系','數(shù)學(xué)系','管理系','中文系','外語(yǔ)系','法學(xué)系'),-檢查約束系包括這幾個(gè):信息系,計(jì)算機(jī)科學(xué)系,數(shù)學(xué)系,管理系,中文系,外語(yǔ)系,法學(xué)系birth datetime,age smallint constraint d check(age between 0 and
3、 100)-檢查約束年齡為數(shù)值型,且在100之間)cs(成績(jī)表):snocnocj其中約束如下:(1)sno和cno分別參照student和course表中的sno,cno的字段(2)cj(成績(jī))只能在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(成績(jī))只能在
4、100之間,可以不輸入值constraint f primary key(sno,cno)-定義學(xué)生學(xué)號(hào)和課程號(hào)為sc表的主鍵)course(課程表)cnocname其約束如下:(1)課程號(hào)(cno)不能有重復(fù)的(2)課程名(cname)非空create table course(cno smallint not null constraint g primary key,-設(shè)置課程號(hào)為course的主鍵cname varchar(20) not null)(三)針對(duì)學(xué)生課程數(shù)據(jù)庫(kù)查詢(1) 查詢?nèi)w學(xué)生的學(xué)號(hào)與姓名。Select sno,sname from student(2) 查詢?nèi)w學(xué)
5、生的姓名、學(xué)號(hào)、所在系,并用別名顯示出結(jié)果。Select sname as '姓名',sno as '學(xué)號(hào)',dept as '所在地' from student(3) 查詢?nèi)w學(xué)生的詳細(xì)記錄。select * from student(4) 查全體學(xué)生的姓名及其出生年份。select sname,birth from student(5) 查詢學(xué)校中有哪些系。select distinct dept from student(6) 查詢選修了課程的學(xué)生學(xué)號(hào)。select sno from cs where cno is not null(7)
6、查詢所有年齡在20歲以下的學(xué)生姓名及其年齡。select sname,age from student where age < 20(8) 查詢年齡在2023歲(包括20歲和23歲)之間的學(xué)生的姓名、系別和年齡。select sname,dept,age from student where age between 20 and 23(9) 查詢年齡不在2023歲之間的學(xué)生姓名、系別和年齡。select sname,dept,age from student where age<20 or age>23(10) 查詢信息系、數(shù)學(xué)系和計(jì)算機(jī)科學(xué)系生的姓名和性別。select s
7、name,sex from student where dept='信息系' or dept='數(shù)學(xué)系' or dept='計(jì)算機(jī)科學(xué)系'(11) 查詢既不是信息系、數(shù)學(xué)系,也不是計(jì)算機(jī)科學(xué)系的學(xué)生的姓名和性別。select sname,sex from student where dept!='信息系' and dept!='數(shù)學(xué)系' and dept!='計(jì)算機(jī)科學(xué)系'(12) 查詢所有姓劉學(xué)生的姓名、學(xué)號(hào)和性別。select sname,sno,sex from student where s
8、name like('劉%')(13) 查詢學(xué)號(hào)為2009011的學(xué)生的詳細(xì)情況。(具體的學(xué)號(hào)值根據(jù)表中數(shù)據(jù)確定)select * from student where sno=5(14) 查詢姓“歐陽(yáng)”且全名為三個(gè)漢字的學(xué)生姓名select sname from student where sname like('歐陽(yáng)_')(15) 查詢名字中第2個(gè)字為“晨”字的學(xué)生的姓名和學(xué)號(hào)select sname,sno from student where sname like('_晨')(16) 查詢所有不姓劉的學(xué)生姓名。select sname,s
9、no from student where sname not like('劉%')(17) 查詢sql課程的課程號(hào)和學(xué)分。select cno from course where cname='sql'(18) 查詢以"DB_"開頭,且倒數(shù)第3個(gè)字符為 i的課程的詳細(xì)情況。select * from course where cname like('DB_%i_')(19) 查詢?nèi)鄙俪煽?jī)的學(xué)生的學(xué)號(hào)和相應(yīng)的課程號(hào)。select sno,cno from cs where cj is null(20) 查所有有成績(jī)的學(xué)生學(xué)號(hào)和
10、課程號(hào)。select sno,cno from cs where cj is not null(21) 查詢計(jì)算機(jī)系年齡在20歲以下的學(xué)生姓名。select sname from student where age < 20 and dept='計(jì)算機(jī)科學(xué)系'(22) 查詢信息系、數(shù)學(xué)系和計(jì)算機(jī)科學(xué)系學(xué)生的姓名和性別。(使用多個(gè)條件表達(dá)式)select sname,sex from student where dept='信息系' or dept='數(shù)學(xué)系' or dept='計(jì)算機(jī)科學(xué)系'(23) 查詢年齡在2023歲(包
11、括20歲和23歲)之間的學(xué)生的姓名、系別和年齡。(使用多個(gè)條件表達(dá)式)select sname,dept,age from student where age between 20 and 23 (24) 查詢選修了3號(hào)課程的學(xué)生的學(xué)號(hào)及其成績(jī),查詢結(jié)果按分?jǐn)?shù)降序排列。select sno,cj from cs where cno=3 order by cj desc(25) 查詢?nèi)w學(xué)生情況,查詢結(jié)果按所在系的系號(hào)升序排列,同一系中的學(xué)生按年齡降序排列。select * from student order by dept asc,age desc(26) 查詢學(xué)生總?cè)藬?shù)。select co
12、unt(*) from student(27) 查詢選修了課程的學(xué)生人數(shù)。select count(sno) from cs where cno is not null(28) 計(jì)算1號(hào)課程的學(xué)生平均成績(jī)。select avg(cj) from cs where cno=1(29) 查詢選修1號(hào)課程的學(xué)生最高分?jǐn)?shù)。select max(cj) from cs where cno=1(30) 求各個(gè)課程號(hào)及相應(yīng)的選課人數(shù)。select o,count(cs.sno) from course left join cson o=o group by o(31) 查詢選修了3門以上課程的學(xué)生學(xué)號(hào)。se
13、lect sno, count(cno) from cs group by sno having count(cno)>3(32) 查詢有3門以上課程是90分以上的學(xué)生的學(xué)號(hào)及(90分以上的)課程數(shù)。select sno, count(cno) as '課程數(shù)' from cs where cj>90 group by sno having count(cno)>=3(33) 查詢學(xué)生2006011選修課程的總學(xué)分。select sum(course) from course,cs where o=cs.sno and cs.sno=2006011(34) 查
14、詢每個(gè)學(xué)生選修課程的總學(xué)分。select sno,sum(cj)from cs,coursewhere o=ogroup by snounionselect sno, 0 from studentwhere sno not in (select sno from cs) (35) 查詢每個(gè)學(xué)生及其選修課程的情況。select cs.sno,course.* from cs,course where o=o (36) 查詢選修2號(hào)課程且成績(jī)?cè)?0分以上的所有學(xué)生的學(xué)號(hào)、姓名select sno,sname from student where sno=(select sno from cs wh
15、ere cno=2 and cj>90)(37) 查詢每個(gè)學(xué)生的學(xué)號(hào)、姓名、選修的課程名及成績(jī)。select student.sno,sname,course.course,cs.cj from student,course,cs where student.sno=cs.sno and o=o(38) 查詢與“劉晨”在同一個(gè)系學(xué)習(xí)的學(xué)生(分別用嵌套查詢和連接查詢)-嵌套查詢select * from student where dept in(select dept from student where sname='劉晨')-連接查詢select stu1.* fro
16、m student as stu1,student as stu2 where stu1.dept=stu2.dept and stu2.sname='劉晨'-exists查詢select * from student s1 where exists(select * from student s2 where s1.dept=s2.dept ands2.sname='劉晨' )(39) 查詢選修了課程名為“信息系統(tǒng)”的學(xué)生學(xué)號(hào)和姓名select sno,sname from student where sno in(select sno from cs whe
17、re cno in(select cno from course where cname='信息系統(tǒng)')(40) 查詢其他系中比信息系任意一個(gè)(其中某一個(gè))學(xué)生年齡小的學(xué)生姓名和年齡select sname,age from student where age <any(select age from student where dept='信息系')(41) 查詢其他系中比信息系所有學(xué)生年齡都小的學(xué)生姓名及年齡。分別用ALL謂詞和集函數(shù)-用ALLselect sname,age from student where age <all(select
18、age from student where dept='信息系')-聚合函數(shù)select sname,age from student where age <(select min(age) from student where dept='信息系')(42) 查詢所有選修了1號(hào)課程的學(xué)生姓名。(分別用嵌套查詢和連查詢)-嵌套查詢select sname from student where sno in(select sno from cs where cno=1)-連接查詢select sname from student,cs where stud
19、ent.sno=cs.sno and o=1(43) 查詢沒有選修1號(hào)課程的學(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 cs.sno=student.sno ando=o)(45) 查詢至少選修了學(xué)生95002選修的全部課程的學(xué)生號(hào)碼。sel
20、ect distinct sno from sc scx where not exists(select * from cs scy where scy.sno='95002' and not exists(select * from sc scz where scz.sno=scx.sno and o=o)(46) 查詢計(jì)算機(jī)科學(xué)系的學(xué)生及年齡不大于19歲的學(xué)生的信息。select * from student where dept='計(jì)算機(jī)科學(xué)系' or age<19(47) 查詢選修了課程1或者選修了課程2的學(xué)生的信息。select student.
21、* from student,cs where student.sno = cs.sno and (o=1 or o=2)(48) 查詢計(jì)算機(jī)科學(xué)系中年齡不大于19歲的學(xué)生的信息。select * from student where age<=19 and dept='計(jì)算機(jī)科學(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 student.sno=cs.sno and cno='003' and sno in(select sno from cs where cno='004')(50) 查詢計(jì)算機(jī)科學(xué)系的學(xué)生與年齡
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025版外銷合同范本:新能源產(chǎn)品海外銷售合作協(xié)議5篇
- 2025年個(gè)人二手車交易車輛交易咨詢及指導(dǎo)服務(wù)協(xié)議2篇
- 2025年度店鋪空間布局優(yōu)化施工合同范本
- 2025版新車銷售與車主關(guān)愛活動(dòng)合作合同范本2篇
- 2025年度城市綠化工程個(gè)人養(yǎng)護(hù)施工合同4篇
- 2025-2030全球電子合同智能管理服務(wù)行業(yè)調(diào)研及趨勢(shì)分析報(bào)告
- 2025-2030全球三環(huán)癸烷二甲醇二甲基丙烯酸酯行業(yè)調(diào)研及趨勢(shì)分析報(bào)告
- 2025年全球及中國(guó)口服滲透泵行業(yè)頭部企業(yè)市場(chǎng)占有率及排名調(diào)研報(bào)告
- 2024年遼寧中考數(shù)學(xué)臨考押題卷解析版
- 2024年全國(guó)高考語(yǔ)文試題分類匯編:詞語(yǔ)(成語(yǔ)、熟語(yǔ)等)含詳細(xì)解答
- 數(shù)學(xué)-山東省2025年1月濟(jì)南市高三期末學(xué)習(xí)質(zhì)量檢測(cè)濟(jì)南期末試題和答案
- 中儲(chǔ)糧黑龍江分公司社招2025年學(xué)習(xí)資料
- 2024-2025學(xué)年人教版三年級(jí)(上)英語(yǔ)寒假作業(yè)(九)
- 河南退役軍人專升本計(jì)算機(jī)真題答案
- 湖南省長(zhǎng)沙市2024-2025學(xué)年高一數(shù)學(xué)上學(xué)期期末考試試卷
- 船舶行業(yè)維修保養(yǎng)合同
- 駕駛證學(xué)法減分(學(xué)法免分)試題和答案(50題完整版)1650
- 2024年林地使用權(quán)轉(zhuǎn)讓協(xié)議書
- 物流有限公司安全生產(chǎn)專項(xiàng)整治三年行動(dòng)實(shí)施方案全國(guó)安全生產(chǎn)專項(xiàng)整治三年行動(dòng)計(jì)劃
- 2025屆江蘇省13市高三最后一卷生物試卷含解析
- 2023年漢中市人民政府國(guó)有資產(chǎn)監(jiān)督管理委員會(huì)公務(wù)員考試《行政職業(yè)能力測(cè)驗(yàn)》歷年真題及詳解
評(píng)論
0/150
提交評(píng)論