數(shù)據(jù)庫(kù)概論安徽工業(yè)大學(xué)試驗(yàn)報(bào)告書2017_第1頁(yè)
數(shù)據(jù)庫(kù)概論安徽工業(yè)大學(xué)試驗(yàn)報(bào)告書2017_第2頁(yè)
數(shù)據(jù)庫(kù)概論安徽工業(yè)大學(xué)試驗(yàn)報(bào)告書2017_第3頁(yè)
數(shù)據(jù)庫(kù)概論安徽工業(yè)大學(xué)試驗(yàn)報(bào)告書2017_第4頁(yè)
數(shù)據(jù)庫(kù)概論安徽工業(yè)大學(xué)試驗(yàn)報(bào)告書2017_第5頁(yè)
已閱讀5頁(yè),還剩31頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、數(shù)據(jù)庫(kù)系統(tǒng)概論實(shí)驗(yàn)報(bào)告書專業(yè)班級(jí) 網(wǎng)151學(xué)號(hào) 159144247姓名 張永輝指導(dǎo)教師測(cè)試成績(jī)安徽工業(yè)大學(xué)計(jì)算機(jī)科學(xué)與技術(shù)學(xué)院實(shí)驗(yàn)一:數(shù)據(jù)定義語(yǔ)言實(shí)驗(yàn)日期年 月 日實(shí)驗(yàn)?zāi)康氖煜racle上機(jī)環(huán)境及Oracle客戶端的配置;熟練掌握和使用DDL語(yǔ)言,建立、修改和刪除數(shù)據(jù)庫(kù)表、主鍵、外鍵約束關(guān)系和索引。實(shí)驗(yàn)內(nèi)容Oracle上機(jī)環(huán)境以及 Oracle客戶端的配置參見附錄。1. SQL數(shù)據(jù)定義語(yǔ)句:例1-1 :(建立數(shù)據(jù)庫(kù)表)建立教學(xué)數(shù)據(jù)庫(kù)的四個(gè)數(shù)據(jù)庫(kù)表,其中Student表中不包含SSEX(C,2)字段,Sname字段為Sname(C,8)且可為空。例1-2 :(修改數(shù)據(jù)庫(kù)表) 在Student表

2、中增加SEX(C,2)字段。例1-3:(修改列名) 將Student表中列名 SEX修改為SSEX例1-4 :(修改數(shù)據(jù)庫(kù)表) 將Student表中把Sname字段修改為Sname(C,10)且為非空。例1-5 :(建立索引)為Score表按課程號(hào)升序、分?jǐn)?shù)降序建立索引,索引名為SC_GRADE例1-6:(刪除索引)刪除索引SC_GRAD>E例1-7:(建立數(shù)據(jù)庫(kù)表) 建立數(shù)據(jù)庫(kù)表 S1(SNO,SNAME,SD,SA)其字段類型定義與Student表中的相應(yīng)字段(SNO,SNAME,SDEPT,SAG的數(shù)據(jù)類型定義相同。例1-8:(修改數(shù)據(jù)庫(kù)表)刪除成績(jī)表Score的參照完整性約束關(guān)系

3、。例1-9:(修改數(shù)據(jù)庫(kù)表)添加成績(jī)表Score的參照完整性約束關(guān)系。例1-10 :(修改數(shù)據(jù)庫(kù)表名) 將數(shù)據(jù)庫(kù)表S1改名為Student_Temp。實(shí)驗(yàn)要求 熟悉Oracle上機(jī)環(huán)境,掌握 Oracle客戶端的配置; 建立數(shù)據(jù)庫(kù)表,修改數(shù)據(jù)庫(kù)表結(jié)構(gòu),建立、刪除索引;實(shí)驗(yàn)方法 按照附錄中的操作步驟進(jìn)行客戶端的配置;將實(shí)驗(yàn)需求用SQL語(yǔ)句表示;執(zhí)行SQL語(yǔ)句; 查看執(zhí)行結(jié)果,如果結(jié)果不正確,進(jìn)行修改,直到正確為止。實(shí)驗(yàn)總結(jié)SQL語(yǔ)句以及執(zhí)行結(jié)果;對(duì)重點(diǎn)實(shí)驗(yàn)結(jié)果進(jìn)行分析; 實(shí)驗(yàn)中的問(wèn)題和提高;收獲與體會(huì)。1-1create table Student(Sno char(5),Sname varch

4、ar (10),Sdept char (2) not null ,Sclass char (2) not null ,SAge number (2),constraint Snfo_pk primary key(Sno);create table Course(Cno char(3),Cname char(16),Ctime number(3),constraint Cno_pk primary key(Cno);create table teach(tname char(16) not null,tsex char(3) not null,cno char(3) not null ,tdat

5、e date not null,tdept char(2) not null,constraint teach_pk primary key(cno),constraint teach_cno_fk foreign key(cno) references course(cno);create table Score(Sno char(5),Cno char(3),score number(5,2),constraint Scaore_pk primary key(Sno,Cno),constraint Cnfgo_fk foreign key(Cno) references Course(Cn

6、o),constraint Seseno_fk foreign key(Sno) references Student(Sno);1-2alter table Student add SEX char(2);1-3alter table Student RENAME column sex to ssex;1-4alter table Student modify (sname char(10) not null);1-5create unique index SC_GRADE on score(sno asc,score desc);1-6drop index SC_GRADE;1-7crea

7、te table S1(Sno char(5),Sname varchar (10),SD char (2),SA number (2);1-8alter table score drop constraints Cnfgo_fk;alter table score drop constraints Seseno_fk;1-9alter table score add constraints Cnfgo_fk foreign key(Cno)references Course(Cno);alter table score add constraints CSeseno_fk foreign k

8、ey(Sno)references Student(Sno);1-10rename s1 to student_temp;實(shí)驗(yàn)二:數(shù)據(jù)操縱語(yǔ)言實(shí)驗(yàn)日期年 月 日實(shí)驗(yàn)?zāi)康脑趯?shí)驗(yàn)一的基礎(chǔ)上,熟練掌握和使用DM皿言,對(duì)數(shù)據(jù)進(jìn)行增加、修改和刪除操作。實(shí)驗(yàn)內(nèi)容2. SQL數(shù)據(jù)操縱語(yǔ)句:例2-1 :(插入數(shù)據(jù))按前面各表中的數(shù)據(jù)分別插入到教學(xué)數(shù)據(jù)庫(kù)的四個(gè)數(shù)據(jù)庫(kù)表中。將自己的信息作為數(shù)據(jù)插入到學(xué)生表(學(xué)號(hào)的后5位作為學(xué)生表的學(xué)號(hào)),向Score表中插入相應(yīng)的選課數(shù)據(jù)。例2-2:(多行插入)將表Student中在計(jì)算機(jī)系(C6)的學(xué)生數(shù)據(jù)插入到表 Student_Temp中。(以上操作中,注意用COMMITS

9、交數(shù)據(jù),將數(shù)據(jù)保存到數(shù)據(jù)庫(kù)服務(wù)器)例2-3:(利用查詢來(lái)實(shí)現(xiàn)表的定義與數(shù)據(jù)插入)求每一個(gè)學(xué)生的平均成績(jī),把結(jié)果存入數(shù)據(jù)庫(kù)表2。Student_Gr 中。例2-4 :(修改數(shù)據(jù))將Student_Temp表中所有學(xué)生的年齡加例2-5 :(修改數(shù)據(jù))將Course表中 程序設(shè)計(jì)課時(shí)數(shù)修改成與 數(shù)據(jù)結(jié)構(gòu)的課時(shí)數(shù)相同。例2-6 :(插入數(shù)據(jù))例2-7 :(插入數(shù)據(jù))向Score表中插入數(shù)據(jù)(向Score表中插入數(shù)據(jù)(98001', 001', 95 ),根據(jù)返回信息解釋其原因。97001', 010', 80 ),根據(jù)返回信息解釋其原因。例2-8 :(刪除數(shù)據(jù))刪除St

10、udent表中學(xué)號(hào)為96001'的學(xué)生信息,根據(jù)返回信息解釋其原因。例2-9 :(刪除數(shù)據(jù))刪除Course表中課程號(hào)為003的課程信息,根據(jù)返回信息解釋其原因。例2-10:(刪除數(shù)據(jù))刪除學(xué)生表Student_Temp中學(xué)號(hào)以96打頭的學(xué)生信息。(此操作后,注意用 ROLLBACK1退可能更新的數(shù)據(jù))例2-11 :(刪除數(shù)據(jù))刪除數(shù)據(jù)庫(kù)表Student_Temp中所有學(xué)生的數(shù)據(jù)。例2-12 :(刪除表) 刪除數(shù)據(jù)庫(kù)表 Student_Temp和Student_Gr 。實(shí)驗(yàn)要求對(duì)數(shù)據(jù)庫(kù)表進(jìn)行插入、修改和刪除數(shù)據(jù)的操作。實(shí)驗(yàn)方法 按照附錄中的操作步驟進(jìn)行客戶端的配置;將實(shí)驗(yàn)需求用SQL語(yǔ)

11、句表示;執(zhí)行SQL語(yǔ)句; 查看執(zhí)行結(jié)果,如果結(jié)果不正確,進(jìn)行修改,直到正確為止。實(shí)驗(yàn)總結(jié)SQL語(yǔ)句以及執(zhí)行結(jié)果;對(duì)重點(diǎn)實(shí)驗(yàn)結(jié)果進(jìn)行分析; 實(shí)驗(yàn)中的問(wèn)題和提高;收獲與體會(huì)。2-1INSERT INTO Student(SNO,SNAME,SDEPT,SCLASS,SSEX,SAGE) VALUES(,96001',' 馬小燕','CS','01','女',21);INSERT INTO Student(SNO,SNAME,SDEPT,SCLASS,SSEX,SAGE) VALUES(,96002',' 黎明&

12、#39;,CS','01',' 男',18);INSERT INTO Student(SNO,SNAME,SDEPT,SCLASS,SSEX,SAGE) VALUES(,96003','劉東明','MA','01','男',18);INSERT INTO Student(SNO,SNAME,SDEPT,SCLASS,SSEX,SAGE) VALUES(,96004','趙志勇','IS','02','男',20)

13、;INSERT INTO Student(SNO,SNAME,SDEPT,SCLASS,SSEX,SAGE) VALUES(,97001',' 馬蓉','MA','02',' 女',19);INSERT INTO Student(SNO,SNAME,SDEPT,SCLASS,SSEX,SAGE) VALUES(,97002',' 李成功','CS','01',' 男',20);INSERT INTO Student(SNO,SNAME,SDEPT,SC

14、LASS,SSEX,SAGE) VALUES(,97003','黎明,'IS','03','女',19);INSERT INTO Student(SNO,SNAME,SDEPT,SCLASS,SSEX,SAGE) VALUES(,97004','李麗','CS','02','女',19);INSERT INTO Student(SNO,SNAME,SDEPT,SCLASS,SSEX,SAGE) VALUES(,96005','司馬志明'

15、,'CS','02',' 男',18);INSERT INTO Course VALUES('001','數(shù)學(xué)分析,144);INSERT INTO Course VALUES('002','普通物理,144);INSERT INTO Course VALUES('003','微機(jī)原理,72);INSERT INTO Course VALUES('004','數(shù)據(jù)Z構(gòu),72);INSERT INTO Course VALUES('005'

16、;,'操作系統(tǒng),64);INSERT INTO Course VALUES('006','數(shù)據(jù)庫(kù)原理,64);INSERT INTO Course VALUES('007','DB_Design',48);INSERT INTO Course VALUES('008',' 程序設(shè)計(jì)',56);INSERT INTO Teach VALUES('王成剛,男','004','05-9 月-1999','CS');INSERT INTO Te

17、ach VALUES('李正科','男','003','05-9 月-1999','CS');INSERT INTO Teach VALUES('嚴(yán)敏','女','001','05-9 月-1999','MA');INSERT INTO Teach VALUES('趙高','男','004','05-9 月-1999','IS');INSERT INTO T

18、each VALUES('李正科','男','003','23-2 月-00','MA');INSERT INTO Teach VALUES('劉玉蘭','女','006','23-2月-00','CS');INSERT INTO Teach VALUES('王成剛,男','004','23-2 月-00','IS');INSERT INTO Teach VALUES(&#

19、39;馬悅','女','008','06-9 月-00','CS');INSERT INTO Teach VALUES('王成剛,男','007','05-9 月-1999','CS');INSERT INTO Score VALUES('96001','001',77.5);INSERT INTO Score VALUES('96001','003',89);INSERT INTO Score

20、VALUES('96001','004',86);INSERT INTO Score VALUES('96001','005',82);INSERT INTO Score VALUES('96002','001',88);INSERT INTO Score VALUES('96002','003',92.5);INSERT INTO Score VALUES('96002','006',90);INSERT INTO Score VAL

21、UES('96005','004',92);INSERT INTO Score VALUES('96005','005',90);INSERT INTO Score VALUES('96005','006',89);INSERT INTO Score VALUES('96005','007',76);INSERT INTO Score VALUES('96003','001',69);INSERT INTO Score VALUES(&

22、#39;96001','001',96);/INSERT INTO Score VALUES('96001','008',95);INSERT INTO Score VALUES('96004','001',87);INSERT INTO Score VALUES('96003','003',91);INSERT INTO Score VALUES('96002','003',91);INSERT INTO Score(SNO,CNO) VAL

23、UES('96002','004');INSERT INTO Scoreselect*;/INSERT INTO Score VALUES('96004','005',90);INSERT INTO Score VALUES('96004','006',85);INSERT INTO Score VALUES('96004','008',75);INSERT INTO Score VALUES('96003','001',59);INS

24、ERT INTO Score VALUES('96003','003',58);/*alter table Teach modify (Tsex char (3) check (Tsex in ('男','女');ALTER TABLE teach DROP CONSTRAINT Tdept;constraint Cno_fk foreign key(Cno) references Course(Cno);ALTER TABLE teach DROP CONSTRAINT Teach_pk;insert into Teach(TN

25、AME,TSEX,CNO,TDATE,TDEPT) values(' 王成剛','男','004',to_date('1999-9-5','yyyy-mm-dd'),'CS');insert into Teach(TNAME,TSEX,CNO,TDATE,TDEPT) values('李正科','男','003',to_date('1998-3-23','yyyy-mm-dd'),'CS');insert

26、 into Teach(TNAME,TSEX,CNO,TDATE,TDEPT) values('嚴(yán)敏','女','001',to_date('1993-5-21','yyyy-mm-dd'),'MA');insert into Teach(TNAME,TSEX,CNO,TDATE,TDEPT) values('趙高','男','002',to_date('1994-3-27','yyyy-mm-dd'),'IS&

27、#39;);insert into Teach(TNAME,TSEX,CNO,TDATE,TDEPT) values('李正科','男','005',to_date('1998-3-23','yyyy-mm-dd'),'MA');insert into Teach(TNAME,TSEX,CNO,TDATE,TDEPT) values(' 劉玉蘭','女','006',to_date('2000-2-23','yyyy-mm-dd

28、'),'CS');insert into Teach(TNAME,TSEX,CNO,TDATE,TDEPT) values('王成剛','男','009',to_date('1999-9-5','yyyy-mm-dd'),'IS');/insert into Teach(TNAME,TSEX,CNO,TDATE,TDEPT) values('馬躍','女','008',to_date('2000-9-6',

29、9;yyyy-mm-dd'),'CS');insert into Teach(TNAME,TSEX,CNO,TDATE,TDEPT) values('王成剛','男','007',to_date('1999-9-5','yyyy-mm-dd'),'CS');*/2-2insert into student_temp select sno,sname,sdept,sage fromstudent wherestudent.sdept = 'CS'2-3creat

30、e table student_Gr(sno char(5) primary key,avgscore number(5,2);insert into student_Gr select sno,avg(score) from Score group bysno;2-4update student_temp set SA = SA+3;2-5update course set ctime = (select ctime from course where cname='數(shù)據(jù)Z構(gòu)')where cname =' 程序設(shè)計(jì)'主鍵sno不存在2-6insert int

31、o score values('98001','001','95'); /2-7insert into score values('97001','001','95');違反完整約束條件2-8delete from student where sno ='96001'(SYSTEM.SESENO_FK)-已找至 U 子 t己錄2-9delete from course where cno='003'2-10delete from student_temp where

32、sno like '96%'2-11delete from student_temp;2-12drop table student_gr;drop table student_temp;實(shí)驗(yàn)三:數(shù)據(jù)查詢語(yǔ)言實(shí)驗(yàn)日期年 月 日實(shí)驗(yàn)?zāi)康捏w會(huì)SQL語(yǔ)言數(shù)據(jù)查詢功能的豐富和復(fù)雜。實(shí)驗(yàn)內(nèi)容3. SQL數(shù)據(jù)查詢語(yǔ)句:例3-1 :(選擇表中白若干列)求全體學(xué)生的學(xué)號(hào)、姓名、性別和年齡。例3-2 :(不選擇重復(fù)行)求選修了課程的學(xué)生學(xué)號(hào)。例3-3:(選擇表中白所有列)求全體學(xué)生的詳細(xì)信息。例3-4 :(使用表達(dá)式)求全體學(xué)生的學(xué)號(hào)、姓名和出生年份。例3-5 :(使用列的別名)求學(xué)生的學(xué)號(hào)和出生

33、年份,顯示時(shí)使用別名“學(xué)號(hào)”和“出生年份”例3-6 :(比較大小條件)求年齡大于19歲的學(xué)生的姓名和年齡。例3-8 :(確定范圍條件)例3-9 :(確定范圍條件)求年齡不在19歲與22歲之間的學(xué)生的學(xué)號(hào)和年齡。例3-10 :(確定集合條件)求在下列各系的學(xué)生信息:數(shù)學(xué)系、計(jì)算機(jī)系。例3-11 :(確定集合條件)求不是數(shù)學(xué)系、計(jì)算機(jī)系的學(xué)生信息。例3-7:(比較大小條件)求計(jì)算機(jī)系或信息系年齡大于18歲的學(xué)生的姓名、系和年齡。求年齡在19歲與22歲(含20歲和22歲)之間的學(xué)生的學(xué)號(hào)和年齡。例3-12 :(匹配查詢)求姓名是以“李”打頭的學(xué)生。例3-13:(匹配查詢)求姓名中含有“志”的學(xué)生。例

34、3-14:(匹配查詢)求姓名長(zhǎng)度至少是三個(gè)漢字且倒數(shù)第三個(gè)漢字必須是“馬”的學(xué)生。例3-15 :(匹配查詢)求選修課程001或003,成績(jī)?cè)?0至90之間,學(xué)號(hào)為96xxx的學(xué)生的學(xué)號(hào)、 課程號(hào)和成績(jī)。例3-16:(匹配查詢)求課程名中包含_'字符的課程號(hào)、課程名和學(xué)時(shí)數(shù)。例3-17:(涉及空值查詢)求缺少學(xué)習(xí)成績(jī)的學(xué)生的學(xué)號(hào)和課程號(hào)。例3-18:(控制行的顯示順序)求選修003課程或004課程的學(xué)生的學(xué)號(hào)、課程號(hào)和分?jǐn)?shù),要求按 課程號(hào)升序、分?jǐn)?shù)降序的順序顯示結(jié)果。例3-19 :(組函數(shù))求學(xué)生總?cè)藬?shù)。例3-20 :(組函數(shù))求選修了課程的學(xué)生人數(shù)。例3-21 :(組函數(shù))求計(jì)算機(jī)系學(xué)

35、生的平均年齡。例3-22 :(組函數(shù))求選修了課程001的最高、最低與平均成績(jī)。例3-23 :(分組查詢)求各門課程的平均成績(jī)與總成績(jī)。例3-24 ;(分組查詢)求各系、各班級(jí)的人數(shù)和平均年齡。例3-25:(分組查詢)輸入以下查詢語(yǔ)句并執(zhí)行,觀察出現(xiàn)的其結(jié)果并分析其原因。SELECT SNAME, SDEPT COUNR*) FROM STUDENTWHERE SDEPT='CS' GROUP BY SDEPT;例3-26:(分組查詢)分析以下語(yǔ)句為什么會(huì)出現(xiàn)錯(cuò)誤。并給出正確的查詢語(yǔ)句。SELECT SAGE FROM STUDENT GROUP BY SNO;例3-27:(分

36、組查詢)求學(xué)生人數(shù)不足 3人的系及其相應(yīng)的學(xué)生數(shù)。例3-28 :(分組查詢)求各系中除01班之外的各班的學(xué)生人數(shù)。例3-29:(涉及空值的查詢)分別觀察各組函數(shù)、行的顯示順序以及分組查詢與空值的關(guān)系。例3-30:(連接查詢)求選修了課程 001且成績(jī)?cè)?0分以下或成績(jī)?cè)?90分以上的學(xué)生的姓名、課 程名稱和成績(jī)。例3-31 :(連接查詢與表的別名)求選修了課程的學(xué)生的學(xué)生姓名、課程號(hào)和成績(jī)。例3-32 :(自身連接查詢)求年齡大于李麗的所有學(xué)生的姓名、系和年齡。例3-33:(外部連接查詢) 求選修了課程002或003的學(xué)生的學(xué)號(hào)、課程號(hào)、課程名和成績(jī),要求 必須將002和003課程的相關(guān)信息顯

37、示出來(lái)。例3-34 :(子查詢) 求與 黎明年齡相同的學(xué)生的姓名和系。例3-35:(子查詢)求選修了課程名為數(shù)據(jù)結(jié)構(gòu)的學(xué)生的學(xué)號(hào)和姓名。例3-36 :(子查詢ANY)求比數(shù)學(xué)系中某一學(xué)生年齡大的學(xué)生的姓名和系。例3-37 :(子查詢ALL)求比數(shù)學(xué)系中全體學(xué)生年齡大的學(xué)生的姓名和系。例3-38:(子查詢EXISTS)求選修了課程004的學(xué)生的姓名和系。例3-39:(返回多列的子查詢) 求與 黎明同系且同齡的學(xué)生的姓名和系。例3-40 :(多個(gè)子查詢)求與 黎明同系,且年齡大于李麗的學(xué)生的信息。例3-41 :(子查詢中使用表連接)求數(shù)學(xué)系中年齡相同的學(xué)生的姓名和年齡。例3-42:(連接或嵌套查詢

38、)檢索至少選修王成剛老師所授課程中一門課程的女學(xué)生姓名。例3-43 :(嵌套與分組查詢)檢索選修某課程的學(xué)生人數(shù)多于3人的教師姓名。例3-44 :(集合查詢)列出所有教師和同學(xué)的姓名和性別。例3-45:(相關(guān)子查詢) 求未選修課程004的學(xué)生的姓名。例3-46 :(相關(guān)子查詢)求選修了全部課程的學(xué)生的姓名。例3-47:(相關(guān)子查詢) 求至少選修了學(xué)生96002'所選修的全部課程的學(xué)生的學(xué)號(hào)。例3-48:(相關(guān)子查詢)求成績(jī)比所選修課程平均成績(jī)高的學(xué)生的學(xué)號(hào)、課程號(hào)、和成績(jī)。例3-49 :(相關(guān)子查詢)查詢被一個(gè)以上的學(xué)生選修的課程號(hào)例3-50 :(相關(guān)子查詢查詢所有未選課程的學(xué)生姓名和

39、所在系。實(shí)驗(yàn)要求對(duì)數(shù)據(jù)庫(kù)表進(jìn)行各種查詢操作。實(shí)驗(yàn)方法將實(shí)驗(yàn)需求用SQL語(yǔ)句表示;執(zhí)行SQL語(yǔ)句; 查看執(zhí)行結(jié)果,如果結(jié)果不正確,進(jìn)行修改,直到正確為止。實(shí)驗(yàn)總結(jié)SQL語(yǔ)句以及執(zhí)行結(jié)果;對(duì)重點(diǎn)實(shí)驗(yàn)結(jié)果進(jìn)行分析;實(shí)驗(yàn)中的問(wèn)題和提高;收獲與體會(huì)。3-1select sno,sname,ssex,sage from student;3-2select distinct sno from score;3-3select * from student;3-4select sno,sname,2017-sage as "year of birth" from student;3-5sel

40、ect sno as " 學(xué)號(hào)",2017 - sage as "出生年份"from student;3-6select sname,sage from student where sage > 19;3-7select sname,sdept,sage from student where sage > 18 and sdeptin( 'IS' , 'CS');3-8select sno,sage from student where sage between 19 and 22;3-9select sno,

41、sage from student where sage not between 19 and 22;3-10select * from student where sdept in ('CS','MA');3-11select * from student where sdept not in ('CS','MA');3-12select * from student where sname like ' 李'3=13select * from student where sname like '% 志&

42、#39;3-14select * from student where sname like '% 馬_'3-15select sno,cno,score from score where sno like '96%' and cno in('001', '003' ) and score between 80 and 90;3-16select cno,cname,ctime from course where cname like '%_%'escape ''3-17select sno,cno

43、 from from Score where score is null;3-18select sno,cno,score from score order by sno,score desc;3-19select count(*) from student;3-20select count(distinct sno) from score;3-21select avg(sage) from student where sdept = 'cs'3-22select max(score) ,min(score) ,avg(score) from score where cno =

44、'001'3-23select cno,avg(score),sum(score) from score group by cno;3-24select sdept,sclass,count(sno),avg(sage) from student group bysdept,sclass;3-25SELECT SNAMESDEPSNAME FROM STUDENT WHERE SDEPT=;/GROUPBY表達(dá)式只能是集函數(shù)或者 GROUP B件用于分組的列名。3-26SELECT SNO FROM Student GROUP BY SAGE;3-27select sdept,

45、count(sno) from student group by sdept havingcount(sno) < 3;/having的使用方法3-28select sdept,sclass,count(sno) from student where sclass <>'01' group by sdept,sclass;/求每個(gè)班的人數(shù),以班為單位,所以分組最小到班級(jí),只到sdept是遠(yuǎn)遠(yuǎn)不夠的3-293-30select sname,cname,score from student,score,course wherestudent.sno = score

46、.sno and o = o;3-31select sname,cno,score from student,score where student.sno =score.sno;/ 兩個(gè)不同的表進(jìn)行連接3-32/*select sname,sdept,sage from student where sage > any(selectsage from student where sname =' 李麗');*/ 這個(gè)查不了,母雞難道是只有一個(gè)表名造成系統(tǒng)識(shí)別錯(cuò)誤select x.sname,x.sdept,x.sage from student x,student y

47、wherey.sname ='李麗and x.sage > y.sage;/同表連接查詢,者B起個(gè)另U名,很好解決上述問(wèn)題666 !3-33select sno,o,cname,score from score,course whereo (+) = o and o in('002','003');select sno,o,cname,score from score,course whereo = o(+) and o in('002','003');/* (+)表示外連接。條件關(guān)聯(lián)時(shí),一般只列出表中滿足連接條件的

48、數(shù)據(jù)。如果條件的一邊出現(xiàn)(+),則可列出該表中在條件另一側(cè)的數(shù)據(jù)為空的那些記錄。比如兩個(gè)表:?jiǎn)T工表和工資表。員工表中有總經(jīng)理、A、B、C四條記錄,工資表中只有 A、B C三人的記錄。如果寫如下語(yǔ)句:select姓名,工資from 員工表,工資表 where 員工表.姓名=工資表.姓名那么只可能但出A、B、C三人的記錄。如果寫成:select姓名,工資from 員工表,工資表 where 員工表.姓名(+)=工資表.姓名則可列出總經(jīng)理及 A、B、C三人的記錄,只不過(guò)查詢結(jié)果中總經(jīng)理對(duì)應(yīng)的”工資“列值為空。*/3-34select sname,sdept from student where sa

49、ge in (select sage fromstudent where sname =' 黎明');3-35select sno,sname from student where sno in (select sno fromscore where cno in (select cno from course where cname ='數(shù)據(jù)結(jié)構(gòu)');select sno,sname from student where exists (select * from scorewhere exists (select * from course where cn

50、ame ='數(shù)據(jù)結(jié)構(gòu)'and兩種語(yǔ)句格式均student.sno = score.sno and cno = o );/可以3-36select sname,sdept from student where sage > any(select sagefrom student where sdept = 'MA');3-37select sname,sdept from student where sage > all(select sagefrom student where sdept = 'MA');3-38select sna

51、me,sdept from student where exists(select * from scorewhere sno = student.sno and cno = '004');3-39/select sname,sdept from student where (sdept= and sage=);主select sname,sdept from student where sdept = (select sdept fromstudent where sname =' 李麗')and sage = (select sage fromstudent

52、 where sname =' 李麗');3-40select * from student where sdept = (select sdept from studentwhere sname =' 黎明')and sage > all (select sage from studentwhere sname ='李麗'); error單個(gè)子查詢返回多個(gè)行select * from student where sdept = 'CS' and sage > all (selectsage from student

53、where sname =' 李麗');3-41select sname,sage from student si where sdept = 'MA' and exists(select * from student s2 where sno<>s1.sno and sdept = 'MA' andsage = sl.sage);/未選定行/select sname,sage from student si where sdept = 'MA' andexists(select * from student s2 w

54、here sno!=s1.sno and sage =sl.sage);/select sname, sage from studentwhere sno in (select s1.sno from student s1,student s2where s1.sage = s2.sage and s1.sno != s2.snoand s1.sdept = s2.sdept and s1.sdept = 'MA');/select sname,sage from student s1 where sdept = 'MA' ands1.sage in (sele

55、ct sage from student s2 where s2.sno != s1.sno);3-42select sname from student where ssex =' 女'and sno in(selectsno from score where cno in (select cno from teach where tname ='王成剛);3-43select tname from teach where cno in (select cno from scoregroup by cno having count(sno)>3);注意group

56、 by必須要有,不然having條件沒(méi)有意義,having條件檢索group by 后面接having固定搭配3-44select sname,ssex from student union select tname,tsex fromteach;/union 進(jìn)行結(jié)果歹U的連接, union , union , union3-45select sname from student where sno not in (select sno fromscore where cno = '004');/not in 或者 not exists3-46select sname fro

57、m student where sno in (select sno from score);3-47select distinct sno from score where sno != '96002' and cno in(select cno from score where sno = '96002');3-48select sno,cno,score from score s1 where exists (select * from錯(cuò)誤score s2 group by cno having sl.score > avg(score);/sele

58、ct sno, SCORE.cno, score from score ,(select cno ,avg(score)as av from score group by cno)avgs where score > av ando = o ;3-49select Cno from Course where exists (select * from studentwhere exists (select * from score where sno = student.sno andcno = o);3-50select sname,sdept from student where n

59、ot exists (select * fromcourse where exists (select * from score where sno=student.snoand cno = o);實(shí)驗(yàn)四:視圖、授權(quán)控制與事務(wù)處理實(shí)驗(yàn)日期年 月 日實(shí)驗(yàn)?zāi)康耐ㄟ^(guò)實(shí)驗(yàn)進(jìn)一步理解視圖的建立和更新、數(shù)據(jù)庫(kù)的權(quán)限管理和事務(wù)處理功能。實(shí)驗(yàn)內(nèi)容4. SQL視圖的定義與操縱:4-1 :建立視圖建立計(jì)算機(jī)系的學(xué)生的視圖STUDENT CS4-2 :建立視圖建立由學(xué)號(hào)和平均成績(jī)兩個(gè)字段的視圖STUDENT GR4-3 :視圖查詢利用視圖STUDENT_GS求年齡大于19歲的學(xué)生的全部信息。4-4 :視圖查詢利用視

60、圖STUDENT_GR求平均成績(jī)?yōu)?8分以上的學(xué)生的學(xué)號(hào)和平均成績(jī)。4-5 :(視圖更新)利用視圖STUDENT_GS增加學(xué)生(96006',張然',CS, 02',男',19 )。4-6 :(視圖更新)利用視圖STUDENT_GS將學(xué)生年齡增加1歲。觀察其運(yùn)行結(jié)果并分析原因。4-7 :(視圖更新)利用視圖STUDENT_GR將平均成績(jī)?cè)黾?2分。觀察其運(yùn)行結(jié)果并分析原因。4-8 :(視圖更新)刪除視圖STUDENT CS學(xué)號(hào)為 96006'的學(xué)生的全部數(shù)據(jù)。4-9 :(視圖更新)刪除視圖STUDENT G的全部數(shù)據(jù)。例4-10 :(刪除視圖)刪除視圖S

61、TUDENTC和 STUDENTGR5. SQL數(shù)據(jù)控制語(yǔ)句:例5-1 :(授權(quán)) 給左右鄰近同學(xué)(用戶)授予在表Student上的SELECT限,并使這兩個(gè)用戶具有給其他用戶授予相同權(quán)限的權(quán)限。例5-2 :(授權(quán))給鄰近同學(xué)(用戶)授予 Teach表上的所有權(quán)限。例5-3 :(授權(quán)) 給所有用戶授予 Score表上的SELECTOR例5-4 :(授權(quán)驗(yàn)證)觀察左右鄰近同學(xué)查詢你所授權(quán)的表中的內(nèi)容。例5-5 :(收回授權(quán))收回上面例子中的所有授予的權(quán)限。6. SQL事務(wù)處理:例6-1 :(事務(wù)回退)將課程名稱表中的程序設(shè)計(jì)課程學(xué)時(shí)數(shù)修改為 80、微機(jī)原理課程學(xué)時(shí)數(shù)修改為70學(xué)時(shí),查詢?nèi)空n程的總

溫馨提示

  • 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ù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論