



下載本文檔
版權(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ù)查看現(xiàn)有數(shù)據(jù)庫(kù)showdatabases;建立demo數(shù)據(jù)庫(kù)createdatabasedemo;查看現(xiàn)有數(shù)據(jù)庫(kù)showdatabases;刪除數(shù)據(jù)庫(kù)dropdatabasedemo;查看現(xiàn)有數(shù)據(jù)庫(kù)showdatabases;例2:建立基本表如果demo數(shù)據(jù)庫(kù)未建立,則先建立數(shù)據(jù)庫(kù)createdatabasedemo;不選擇缺省數(shù)據(jù)庫(kù)的情況下建立學(xué)生表createtableStudent(錯(cuò)誤的Snochar(9)notnull,錯(cuò)誤的Snamechar(8)notnull,Ssexchar(2)notnulldefault'男',Sagetinyint(2),Sdeptchar(2));createtabledemo.Student(Snochar(9)notnull,Snamechar(8)notnull,Ssexchar(2)notnulldefault'男',Sagetinyint(2),Sdeptchar(2));選擇demo為缺省的數(shù)據(jù)庫(kù)Usedemo;不用再demo.Course不用再demo.SC不用再demo.Course不用再demo.SCCnochar(1)notnull,Cnamechar(12)notnull,Cpnochar(1),Ccredittinyint(1));建立學(xué)生選課表createtableSC(Snochar(9),Cnochar(1),Gradetinyint(3));查看demo庫(kù)中基本表的數(shù)量showtables;showtablesfromdemo;(如果demo不是當(dāng)前數(shù)據(jù)庫(kù))例3:查看基本表的結(jié)構(gòu)showcolumnsfromstudent;descstudent;showcolumnsfromcourse;desccourse;showcolumnsfromsc;descsc;例4:修改基本表的結(jié)構(gòu)向student表的最后中插入一個(gè)字段altertablestudentaddaddressvarchar(64);查看student表結(jié)構(gòu)所發(fā)生的變化descstudent;向student表的第1列前插入一個(gè)字段altertablestudentaddIDNumchar(18)notnullfirst;查看student表結(jié)構(gòu)所發(fā)生的變化descstudent;向student表的sage列后插入一個(gè)字段altertablestudentaddbirthdaydateaftersage;查看student表結(jié)構(gòu)所發(fā)生的變化descstudent;刪除新增加的三個(gè)字段altertablestudentdropIDNum;altertablestudentdropaddress;altertablestudentdropbirthday;查看student表結(jié)構(gòu)所發(fā)生的變化descstudent;將Sdept由char(2)修改為char(8)altertablestudentchangeSdeptSdeptchar(8);查看student表結(jié)構(gòu)所發(fā)生的變化descstudent;將Sage由tinyint(2)修改為int(6),并不允許為空altertablestudentchangeSageSageint(6)notnull;查看student表結(jié)構(gòu)所發(fā)生的變化descstudent;將Ssex由char(2)修改為int(1),缺省為0altertablestudentchangeSsexSsexint(1)default0;查看student表結(jié)構(gòu)所發(fā)生的變化descstudent;將Sno改名為Snum,由char(9)修改為int(6),且為主鍵altertablestudentchangeSnoSnumint(6)primarykey;查看student表結(jié)構(gòu)所發(fā)生的變化descstudent;(也可以先刪除Sno,再增加Snum)例5:刪除基本表刪除Student表droptablestudent;查看demo庫(kù)中基本表的數(shù)量showtables;刪除Course表/SC表droptablecourse;droptableSC;查看demo庫(kù)中基本表的數(shù)量showtables;例6:創(chuàng)建數(shù)據(jù)庫(kù)的時(shí)候建立索引如果Student表已存在,則先刪除droptablesstudent;createtableStudent(Snochar(9)notnull,Snamechar(8)notnull,Ssexchar(2)notnulldefault'男',Sagetinyint(2),Sdeptchar(2),Primarykey(sno),indexsi(snameasc),uniques2(sagedesc));查看數(shù)據(jù)表的結(jié)構(gòu)descStudent;查看索引showindexfromStudent;例7:先建立數(shù)據(jù)表,再通過(guò)修改數(shù)據(jù)表的屬性來(lái)建立索引如果Student表已存在,則先刪除droptablesstudent;createtableStudent(Snochar(9)notnull,Snamechar(8)notnull,Ssexchar(2)notnulldefault'男',Sagetinyint(2),Sdeptchar(2));altertablestudentaddprimarykey(sno);altertablestudentaddindexsi(snameasc);altertablestudentadduniques2(sagedesc);查看數(shù)據(jù)表的結(jié)構(gòu)descStudent;查看索引showindexfromStudent;例8:先建立數(shù)據(jù)表,再直接創(chuàng)建索弓1(普通/唯一性索弓I)如果Student表已存在,則先刪除droptablesstudent;createtableStudent( primaykey建主索引Snochar(9)notnullprimarykey,Snamechar(8)notnull,Ssexchar(2)notnulldefault'男',Sagetinyint(2),Sdeptchar(2));createindexsionstudent(snameasc);createuniqueindexs2onstudent(sagedesc);查看數(shù)據(jù)表的結(jié)構(gòu)descStudent;查看索引showindexfromStudent;例9:通過(guò)修改屬性的方式刪除Student表的索引altertablestudentdropindexsi;altertablestudentdropindexs2;altertablestudentdropprimarykey;查看索引showindexfromStudent;例10:直接刪除索弓1(普通/唯一性索引)因?yàn)榍懊胬?已刪除,要先建立createindexsionstudent(snameasc);createuniqueindexs2onstudent(sagedesc);刪除索引dropindexsionstudent;dropindexs2onstudent;查看索引showindexfromStudent;例11:按表結(jié)構(gòu)對(duì)應(yīng)插入數(shù)據(jù)清除前面索引的影響,重新建立Student表droptablestudent;createtableStudent(Snochar(9)notnullprimarykey,Snamechar(8)notnull,Ssexchar(2)notnulldefault'男',Sagetinyint(2),Sdeptchar(2));在Student中插入數(shù)據(jù)insertintostudentvalues('200215121',‘李勇','男',20,'CS');正確查看數(shù)據(jù)select*fromStudent;insertintostudentvalues(‘張三','200215999'男',20,‘CS');不正確,200215999當(dāng)做姓名,超寬查看數(shù)據(jù)select*fromStudent;insertintostudentvalues('張三','200215','男',20,'CS');正確,張三當(dāng)做學(xué)號(hào),200215當(dāng)做姓名查看數(shù)據(jù)select*fromStudent;insertintostudentvalues('200215128,,NULL/女',19,'CS');錯(cuò)誤,sname不能為空查看數(shù)據(jù)select*fromStudent;insertintostudentvalues('200215128,,‘張三','女',NULL,NULL);正確查看數(shù)據(jù)select*fromStudent;insertintostudentvalues('200215129',‘李四','男’);錯(cuò)誤,給出的數(shù)據(jù)列(3)必須與數(shù)據(jù)表的列(5)相同查看數(shù)據(jù)select*fromStudent;例12:按字段名對(duì)應(yīng)插入數(shù)據(jù)insertintostudent(sname,sno)values(J李四','2002151295);正確,帶缺省值及空字段查看數(shù)據(jù)select*fromStudent;insertintostudent(ssex,sno)values女:200215130,);錯(cuò)誤,sname非空且無(wú)缺省值查看數(shù)據(jù)select*fromStudent;例13:刪除數(shù)據(jù)清空全部數(shù)據(jù)deletefromstudent;查看數(shù)據(jù)select*fromStudent;再增加4條記錄insertintostudentvalues('200215121',‘李勇','男',20,'CS');insertintostudentvalues('200215122','劉晨','女',19,'CS');insertintostudentvalues('200215123','王敏','女',18,'MA');insertintostudentvalues('200215125',‘張立','男',19,‘IS');查看數(shù)據(jù)select*fromStudent;刪除學(xué)號(hào)為200215122的記錄deletefromstudentwheresno=,200215122';查看數(shù)據(jù)select*fromStudent;刪除年齡大于等于19的學(xué)生deletefromstudentwheresage>=19;查看數(shù)據(jù)select*fromStudent;重新插入3條記錄,恢復(fù)為4條記錄insertintostudentvalues('200215121,,*李勇','男',20,'CS');insertintostudentvalues('200215122',‘劉晨','女‘,19,'CS');insertintostudentvalues('200215125',‘張立‘,'男',19,‘IS');刪除性別為男的學(xué)生deletefromstudentwheressex=,男’;查看數(shù)據(jù)select*fromStudent;重新插入2條記錄,恢復(fù)為4條記錄insertintostudentvalues('200215121',‘李勇','男',20,'CS');insertintostudentvalues('200215125',‘張立','男',19,'IS');刪除性別為男且年齡<=19的學(xué)生deletefromstudentwheressex='男'andsage<=19;查看數(shù)據(jù)select*fromStudent;重新插入1條記錄,恢復(fù)為4條記錄insertintostudentvalues('200215125','張立','男',19,'IS');刪除性別為男或者年齡<=18的學(xué)生deletefromstudentwheressex='男'orsage<=18;查看數(shù)據(jù)select*fromStudent;例14:修改記錄先將數(shù)據(jù)恢復(fù)為4條記錄deletefromstudent;insertintostudentvalues('200215121',‘李勇','男',20,'CS');insertintostudentvalues('200215122','劉晨','女',19,'CS');insertintostudentvalues('200215123','王敏','女',18,‘MA');insertintostudentvalues('200215125',‘張立','男',19,'IS');查看數(shù)據(jù)select*fromStudent;將李勇的年齡修改為21歲updatestudentsetsage=21wheresname='李勇';查看數(shù)據(jù)select*fromStudent;將所有男生的年齡加倍updatestudentsetsage=sage*2wheressex=,男';查看數(shù)據(jù)select*fromStudent;將所有男生的年齡增加15%updatestudentsetsage=sage*l.15wheressex=,男';查看數(shù)據(jù)(注意,結(jié)果仍為整數(shù))select*fromStudent;將學(xué)號(hào)為200215121的學(xué)生的年齡修改為21,系部為ISupdatestudentsetsage=21,sdept='IS'wheresno=,20021512T;查看數(shù)據(jù)select*fromStudent;將學(xué)號(hào)為200215121的學(xué)生的學(xué)號(hào)修改為200215122updatestudentsetsno=,200215122,wheresno=,20021512T;錯(cuò)誤,主鍵重復(fù)查看數(shù)據(jù)select*fromStudent;將學(xué)號(hào)為200215121的學(xué)生的學(xué)號(hào)修改為200215124updatestudentsetsno=,200215124,wheresno=,20021512T;正確查看數(shù)據(jù)select*fromStudent;將學(xué)號(hào)為200215122的學(xué)生的姓名清空updatestudentsetsname=NULLwheresno=,200215122,;錯(cuò)誤,sname不允許為空查看數(shù)據(jù)select*fromStudent;先修改屬性,允許為空altertablestudentchangesnamesnamechar(8);查看結(jié)構(gòu)descstudent;再將學(xué)號(hào)為200215122的學(xué)生的姓名清空updatestudentsetsname=NULLwheresno=,200215122,;正確查看數(shù)據(jù)select*fromStudent;將學(xué)號(hào)為200215123的學(xué)生的年齡改為zzupdatestudentsetsage=,zz'wheresno=,200215123,;錯(cuò)誤,sage為整型查看數(shù)據(jù)select*fromStudent;例15:帶外鍵的數(shù)據(jù)表的創(chuàng)建建立三張無(wú)外鍵關(guān)系的表,順序任意如果Student、course>SC表已存在,則先刪除droptablesstudent;droptablescourse;droptablessc;建立學(xué)生選課表createtableSC(Snochar(9),Cnochar(1),Gradetinyint(3),primarykey(Sno,Cno));建立學(xué)生表createtableStudent(Snochar(9)notnullprimarykey,Snamechar(8)notnull,Ssexchar(2)notnulldefault'男',Sagetinyint(2),Sdeptchar(2));建立課程表createtableCourse(Cnochar(1)notnullprimarykey,Cnamechar(12)notnull,Cpnochar(1),Ccredittinyint(1)建立Student與SC存在外鍵關(guān)系的表,則必須先student,后sc表如果Student、course、SC表已存在,則先刪除droptablesstudent;droptablescourse;droptablessc;建立學(xué)生選課表createtableSC(Snochar(9),Cnochar(1),r、+?? 創(chuàng)建不成功Gradetinyint(3),primarykey(Sno,Cno),foreignkey(Sno)referencesStudent(sno));先建立學(xué)生表(不帶主鍵)createtableStudent(Snochar(9)notnull,Snamechar(8)notnull,Ssexchar(2)notnulldefault'男',Sagetinyint(2),Sdeptchar(2));再建立學(xué)生選課表createtableSC(Snochar(9),Cnochar(1),Gradetinyint(3),primarykey(Sno,Cno),foreignkey(Sno)referencesStudent(sno));創(chuàng)建不成功,因?yàn)镾tudnet(sno)不是主鍵刪除學(xué)生表droptablestudent;建立學(xué)生表(帶主鍵)createtableStudent(Snochar(9)notnullprimarykey,Snamechar(8)notnull,Ssexchar(2)notnulldefault'男',Sagetinyint(2),Sdeptchar(2));再建立學(xué)生選課表createtableSC(Snochar(9),Cnochar(1),Gradetinyint(3),primarykey(Sno,Cno),foreignkey(Sno)referencesStudent(sno));創(chuàng)建成功創(chuàng)建有外鍵關(guān)系的三個(gè)表,則SC必須最后建立如果Student、course>SC表已存在,則先刪除droptablessc;droptablescourse;droptablesstudent;建立學(xué)生表createtableStudent(Snochar(9)notnullprimarykey,Snamechar(8)notnull,Ssexchar(2)notnulldefault'男',Sagetinyint(2),Sdeptchar(2));建立課程表createtableCourse(Cnochar(1)notnullprimarykey,Cnamechar(12)notnull,Cpnochar(1),Ccredittinyint(1),foreignkey(Cpno)referencesCourse(Cno));建立學(xué)生選課表createtableSC(Snochar(9),Cnochar(1),Gradetinyint(3),primarykey(Sno,Cno),foreignkey(Sno)referencesStudent(sno),foreignkey(Cno)referencesCourse(Cno));查看表結(jié)構(gòu)showtables;descStudent;descCourse;descSC;例16:帶外鍵數(shù)據(jù)表的刪除droptableCourse;刪除不成功,要?jiǎng)h除course,必須先刪除scdroptableSC;droptablecourse;droptablestudent;例17:帶外鍵數(shù)據(jù)表的記錄的插入先把三張表補(bǔ)齊建立課程表createtableCourse(Cnochar(1)notnullprimarykey,Cnamechar(12)notnull,Cpnochar(1),Ccredittinyint(1),foreignkey(Cpno)referencesCourse(Cno));建立學(xué)生表createtableStudent(Snochar(9)notnullprimarykey,Snamechar(8)notnull,Ssexchar(2)notnulldefault'男',Sagetinyint(2),Sdeptchar(2));建立學(xué)生選課表createtableSC(Snochar(9),Cnochar(1),Gradetinyint(3),primarykey(Sno,Cno),foreignkey(Sno)referencesStudent(sno),foreignkey(Cno)referencesCourse(Cno));查看數(shù)據(jù)表showtables;向student表插入記錄(正確)insertintostudentvalues('200215121',‘李勇','男',20,'CS');insertintostudentvalues('200215122','劉晨','女',19,'CS');insertintostudentvalues(1200215123','王敏','女',18,‘MA');insertintostudentvalues(*200215125',‘張立','男',19,'2S');查看student表的內(nèi)容select*fromstudent;向course表插入記錄(不正確)insertintocoursevalues(T','數(shù)據(jù)庫(kù)','5',4);錯(cuò)誤insertintocoursevalues('2','數(shù)學(xué)',NULL,2);正確insertintocoursevalues('3','信息系統(tǒng)',T',4);錯(cuò)誤insertintocoursevalues('4','操作系統(tǒng)','6',3);錯(cuò)誤insertintocoursevalues('5','數(shù)據(jù)結(jié)構(gòu)','7',4);錯(cuò)誤insertintocoursevalues('6','數(shù)據(jù)處理',NULL,2);正確insertintocoursevalues('7'PASCAL語(yǔ)言','6',4);正確查看Course表中已有數(shù)據(jù)select*fromcourse;向course表插入記錄1E確順序)首先刪除剛才已有的記錄deletefromcourse;錯(cuò)誤,原因先不管deletefromcoursewherecno=2;正確deletefromcoursewherecno=6;錯(cuò)誤,原因先不管deletefromcoursewherecno=7;deletefromcoursewherecno=6;查看Course表是否為空select*fromcourse;course表插入的正確順序insertintocoursevalues('2',‘?dāng)?shù)學(xué)',NULL,2)insertintocoursevalues('6','數(shù)據(jù)處理',NULL,2)insertintocoursevalues('7','PASCAL語(yǔ)言‘,'6',4)insertintocoursevalues('4','操作系統(tǒng)','6',3)insertintocoursevalues('5','數(shù)據(jù)結(jié)構(gòu)','7',4)insertintocoursevalues('1','數(shù)據(jù)庫(kù)', '5',4)insertintocoursevalues('3','信息系統(tǒng)',T',4)查看Course表中已有數(shù)據(jù)select*fromcourse;向sc表插入記錄insertintoscvalues(,200215121',T',92);insertintoscvaluesC200215121'2',85);insertintoscvalues('200215121'3',88);insertintoscvalues('200215122'2',90);insertintoscvalues(,200215122'3',80);正確查看SC表中已有數(shù)據(jù)select*fromSC;insertintoscvaluesC200215124'3',76);錯(cuò)誤,因?yàn)?00215124這個(gè)學(xué)號(hào)不存在insertintoscvalues(,200215125'8',56);錯(cuò)誤,因?yàn)?號(hào)課程不存在例18:帶外鍵的數(shù)據(jù)表的記錄的刪除刪除學(xué)號(hào)的200215123的學(xué)生deletefromstudentwheresno=,200215123,;正確查看Student表中已有數(shù)據(jù)select*fromStudent;刪除學(xué)號(hào)的200215122的學(xué)生deletefromstudentwheresno=,200215122,;不正確,因?yàn)?00215122在SC中有記錄查看Student表中已有數(shù)據(jù)select*fromStudent;刪除學(xué)號(hào)的200215122的學(xué)生先刪除SC中的記錄deletefromSCwheresno=,200215122';查看SC中的內(nèi)容select*fromSC;再刪除Student中的記錄deletefromstudentwheresno=,200215122,;正確查看Student表中已有數(shù)據(jù)select*fromStudent;刪除Course表中的所有內(nèi)容首先清除SC表的影響,將SC表刪空deletefromSC;全部刪除deletefromcourse;用與插入相反的順序進(jìn)行刪除插入順序deletefromcoursewherecno=,3,;deletefromcoursewherecno=,r;deletefromcoursewherecno=5;deletefromcoursewherecno=,4';deletefromcoursewherecno=7;deletefromcoursewherecno=,6,;deletefromcoursewherecno=,2,;例19:查詢-單表查詢-列的選擇首先清除前面操作的影響,重新建立3張表,輸入完整數(shù)據(jù)刪除3張表droptableSC;droptableStudent;droptableCourse;重新建立3張基本表createtableStudent(Snochar(9)notnullprimarykey,Snamechar(8)notnull,Ssexchar(2)notnulldefault'男',Sagetinyint(2),Sdeptchar(2)createtableCourse(Cnochar(1)notnullprimarykey,Cnamechar(12)notnull,Cpnochar(1),Ccredittinyint(1),foreignkey(Cpno)referencesCourse(Cno));createtableSC(Snochar(9),Cnochar(1),Gradetinyint(3),primarykey(Sno,Cno),foreignkey(Sno)referencesStudent(sno),foreignkey(Cno)referencesCourse(Cno));insertintostudentvalues('200215121',‘李勇','男',20,'CS');insertintostudentvalues('200215122','劉晨','女',19,'CS');insertintostudentvalues('200215123',‘王敏','女',18,‘MA');insertintostudentvalues(,200215125',‘張立‘,'男',19,'2S');insertintocoursevalues('2','數(shù)學(xué)', NULL,2);insertintocoursevalues('6','數(shù)據(jù)處理',NULL,2);insertintocoursevalues('7','PASCAL語(yǔ)言‘,'6',4)insertintocoursevalues('4','操作系統(tǒng)','6',3)insertintocoursevalues('5','數(shù)據(jù)結(jié)構(gòu)','7',4)insertintocoursevalues('1','數(shù)據(jù)庫(kù)', '5',4)insertintocoursevalues('3','信息系統(tǒng)',T',4)insertintoscvaluesC200215121'1',92);insertintoscvalues(,200215121'2',85);insertintoscvalues('200215121'3',88);insertintoscvalues(,200215122'2',90);insertintoscvaluesC200215122'3',80);查詢?nèi)龔埍淼膬?nèi)容select*fromStudent;select*fromCourse;select*fromSC;按指定順序顯示列selectsname,sno,sage,sdept,ssexfromstudent;按指定順序顯示部分列selectsname,sno,sagefromstudent;替換指定列的顯示selectsname'姓名',ssex'性別',sno'學(xué)號(hào)',sage'年齡',sdept'系部'fromstudent;例20:插入指定列的說(shuō)明及顯示計(jì)算后的內(nèi)容在sage前顯示“年齡”selectsname,sno,年齡',sagefromstudent;將sage替換為相應(yīng)的出生年份selectsname,sno,2010-sagefromstudent;selectsname,sno,'出生年份’,2010-sagefromstudent;selectsname,sno,2010-sage'出生年份'fromstudent;例21:消除取值重復(fù)的行selectssexfromstudent;selectdistinctssexfromstudent;例22:比較運(yùn)算單字段條件進(jìn)行比較運(yùn)算(使用6個(gè)比較運(yùn)算符)select*fromstudentwheresage>=20;select*fromstudentwheressex=,男';單字段條件比較(not與比較運(yùn)算符合用非必須)select*fromstudentwherenotsage>=20;select*fromstudentwherenotssex=,男';多個(gè)單字段比較進(jìn)行邏輯運(yùn)算(優(yōu)先級(jí)NOTANDOR)select* from student where sage>=19and ssex='男';select* from student where sage>=19orssex=,男';用between…and表示范圍(非必須)select* from student where sagebetween 18and19;select* from student where sagebetween 19and18;結(jié)果為空,因?yàn)閎etween有大小要求用in來(lái)確定集合范圍(非必須)select*fromstudentwheresdeptin('CS','MA');例23:通過(guò)字符串匹配來(lái)選擇行首先插入一條新的記錄insertintostudentvaluesC20021」21',‘李勇軍','男',21,'IS');
查看全部數(shù)據(jù)select*fromstudent;二與like在匹配全字符串時(shí)一樣select*fromstudentwheresname=,李勇';select*fromstudentwheresnamelike'李勇';在=中使用%與_(無(wú)效)select*fromstudentwheresname='李勇%';select*fromstudentwheresname='李勇—在like中使用%與_select*fromstudentwheresnamelike'李勇%';select*fromstudentwheresnamelike'李勇_本身有—或%時(shí)的轉(zhuǎn)義符匹配snolike,20021_121,snolike,20021_121,;snolike,20021\_121,;sno='20021」21';例24:涉及空值時(shí)的查詢先插入2條記錄insertintostudent(sno,sname)values2008001','張三’);
insertintostudent(sno,sname)values2008002','李四');查看全部數(shù)據(jù)select*fromstudent;查詢空值select*fromstudentwheresageisnull;select*fromstudentwheresdeptisnotnull;空值與的區(qū)別再插入2條記錄insertintostudent(sno,sname,sdept)valuesC2008003','王五','');insertintostudent(sno,sname,sdept)values('2008004','趙六','');兩個(gè)空格查看全部數(shù)據(jù)select*fromstudent;查詢空值、''和'selectselectselectselectfromfromfromfromstudentstudentstudentstudentwherewherewherewheresdeptisnull;sdept=,selectselectselectselectfromfromfromfromstudentstudentstudentstudentwherewherewherewheresdeptisnull;sdept=,,;sdept='';一個(gè)空格sdept=,';兩個(gè)空格例25:查詢結(jié)果排序首先刪除廢數(shù)據(jù)deletefromstudentwheresno=,20021_121';deletefromstudentwheresno='200800T;
deletefromstudentdeletefromstudentdeletefromstudentwheresno=2008002deletefromstudentdeletefromstudentdeletefromstudentwheresno=2008002wheresno=,2008003,wheresno=,2008004,查詢結(jié)果排序sage;select*fromstudentorderbysage;select*fromstudentorderbysagedesc;selectsdept;sdeptdesc;sdept;sdeptdesc;例26:集函數(shù)的使用先插入兩條記錄insertintostudentvalues('200215130',‘張三','男',19,‘IS');insertintostudentvalues200215131'李四','男',19,‘IS');查看全部數(shù)據(jù)select*fromstudent;count:求指定列的行的數(shù)量selectcount(*)fromstudent;selectcount(*)fromstudentwheressex='男';selectcount(sdept)fromstudent;selectcount(distinctsdept)fromstudent;selectcount(sdept)fromstudentwheressex='男';selectcount(distinctsdept)fromstudentwheressex=,男’;sum:求指定數(shù)值列的總和selectsum(sage)fromstudent;selectsum(sage)fromstudentwheressex=,男';selectsum(distinctsage)fromstudentwheressex=,男';avg:求指定列的平均值selectavg(sage)fromstudent;selectavg(sage)fromstudentwheressex='男';selectavg(distinctsage)fromstudentwheressex=,男';max:求指定列的最大值selectmax(sage)fromstudent;selectmax(sage)fromstudentwheressex='男';selectmax(distinctsage)fromstudentwheressex=,男’;min:求指定列的最小值selectmin(sage)fromstudent;selectmin(sage)fromstudentwheressex='男';selectmin(distinctsage)fromstudentwheressex-男';例27:用groupby進(jìn)行分組按sage對(duì)student進(jìn)行分組select*fromstudentgroupbysage;輸出表列中,除sage外其它字段的值無(wú)意義統(tǒng)計(jì)不同年齡的人數(shù)selectsage,count(*)fromstudentgroupbysage;統(tǒng)計(jì)不同系部的人數(shù)selectsdept,count(*)fromstudentgroupbysdept;統(tǒng)計(jì)不同年齡的男生人數(shù)selectsage,count(*)fromstudentwheressex=,男'groupbysage;按eno對(duì)SC進(jìn)行分組select*fromscgroupbyeno;輸出表列中,除eno外其它字段的值無(wú)意義統(tǒng)計(jì)各門課程的選課人數(shù)selecteno,count(*)fromscgroupbyeno;統(tǒng)計(jì)每個(gè)學(xué)生的選課數(shù)量selectsno,count(*)fromscgroupbysno;統(tǒng)計(jì)各門課程的平均成績(jī)selecteno,avg(grade)fromscgroupbyeno;統(tǒng)計(jì)每門課程的選課人數(shù)及平均成績(jī)selecteno,count(*),avg(grade)fromscgroupbyeno;統(tǒng)計(jì)每門課程的選課人數(shù)及平均成績(jī)(輸出格式變)selecteno'課號(hào)',count(*)'人數(shù)',avg(grade)'平均成績(jī)'fromscgroupbyeno;統(tǒng)計(jì)每門課程的最高/最低成績(jī)selecteno,max(grade),min(grade)fromscgroupbyeno;例28:用having子句在分組后進(jìn)行篩選
統(tǒng)計(jì)每門課程的選課人數(shù)selecteno,count(*)fromscgroupbyeno;統(tǒng)計(jì)每門課程的選課人數(shù)(按人數(shù)逆序)selecteno,count(*)fromscgroupbyenoorderbycount(*)desc;統(tǒng)計(jì)每門課程選課人數(shù)在2人以上的課程selecteno,count(*)fromscgroupbyenohavingcount(*)>=2;例29:等值與非等值連接查詢刪廢數(shù)據(jù)deletefromstudentwheresname=,張三';deletefromstudentwheresname=,李四';得到無(wú)意義的笛卡爾積select*select*fromfrom等值連接
select*select*fromfrom等值連接
select*
select*fromfromstudent,course,sc;student,scwherestudent.sno=sc.sno;student,course,scwherestudent.sno=o=sc.eno;自己組織輸出表列:selectsno,sname,ssex,eno,gradefromstudent,scwherestudent.sno=sc.sno;錯(cuò),sno無(wú)法區(qū)分屬于哪個(gè)數(shù)據(jù)表selectstudent.sno,sname,ssex,eno,gradefromstudent,scwherestudent.sno=sc.sno;正確selectstudent,sno,sname,ssex,sdept,course,eno,cname,gradefromstudent,course,scwherestudent.sno=o=sc.eno;正確例30:表的自連接查詢每一門課的間接先修課(先修的先修)selectfirst,eno,second,epnofromcoursefirst,coursesecondwherefirst.cpno=second.enoandsecond,epnoisnotnull;查詢每一門課的間接先修課,包括課程名selectfirst,eno,first,cname,second,epno,second,cnamefromcoursefirst,coursesecondwherefirst.cpno=second.eno;錯(cuò)誤,因?yàn)椴樵兂龅氖钦n程的課號(hào)、課程名、間接先修的課號(hào)、直接先修的課程名selectfirst,eno,first,cname,second,epno,third,cnamefromcoursefirst,coursesecond,coursethirdwherefirst.cpno=second.enoandsecond.cpno=third.eno;正確selectfirst,eno,first,cname,third,eno,third,cnamefromcoursefirst,coursesecond,coursethirdwherefirst.cpno=second.enoandsecond.cpno=third.eno;同樣正確例31:外連接Student表與SC表的內(nèi)、外連接內(nèi)連接形式1selectstudent,sno,sname,ssex,sdept,eno,gradefromstudent,scwherestudent.sno=sc.sno;內(nèi)連接形式2selectstudent,sno,sname,ssex,sdept,eno,gradefromstudentinnerjoinsconstudent.sno=sc.sno;左外連接selectstudent,sno,sname,ssex,sdept,eno,gradefromstudentleftjoinsconstudent.sno=sc.sno;右外連接selectstudent,sno,sname,ssex,sdept,eno,gradefromstudentrightjoinsconstudent.sno=sc.sno;銀行卡演示外連接新創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)并置為當(dāng)前createdatabasedemo2;usedemo2;建立工行數(shù)據(jù)表createtableicbc(xmchar(2),idchar(4)primarykey,Icardchar(4),Imoneyint(2));建立農(nóng)行數(shù)據(jù)表createtableabc(xmchar(2),idchar(4)primarykey,Acardchar(5),Amoneyint(2));插入工行數(shù)據(jù)insertintoicbcvalues('張','0001'1234',100)insertintoicbcvalues('李0002','5678,,200)insertintoicbcvalues('王','0003','5364,,300)插入農(nóng)行數(shù)據(jù)insertintoabcvalues('李','0002','75753,,10);insertintoabcvalues('王','0003','93877,,20);insertintoabcvalues('趙','0004','37748,,30);內(nèi)連接形式1:selecticbc.xm,icbc.id,icard,imoney,acard,amoneyfromicbc,abcwhereicbc.id=abc.id;內(nèi)連接形式2:selecticbc.xm,icbc.id,icard,imoney,acard,amoneyfromicbcinnerjoinabconicbc.id=abc.id;左外連接:(全部字段)selecticbc.xm,icbc.id,icard,imoney,abc.xm,abc.id,acard,amoneyfromicbcleftjoinabconicbc.id=abc.id;左外連接:(部分字段)selecticbc.xm,icbc.id,icard,imoney,acard,amoneyfromicbcleftjoinabconicbc.id=abc.id;右外連接:(全部字段)selecticbc.xm,icbc.id,icard,imoney,abc.xm,abc.id,acard,amoneyfromicbcrightjoinabconicbc.id=abc.id;右外連接:(部分字段)selecticbc.xm,icbc.id,icard,imoney,acard,amoneyfromicbcrightjoinabconicbc.id=abc.id;右外連接:(部分字段)selectabc.xm,abc.id,icard,imoney,acard,amoneyfromicbcrightjoinabconicbc.id=abc.id;全外連接:selecticbc.xm,icbc.id,icard,imoney,acard,amoneyfromicbcleftjoinabconicbc.id=abc.idunionselectabc.xm,abc.id,icard,imoney,acard,amoneyfromicbcrightjoinabconicbc.id=abc.id;例32:復(fù)合條件連接及多表連接3表等值連接selectstudent,sno,sname,ssex,sdept,course,eno,cname,gradefromstudent,course,scwherestudent.sno=o=sc.eno;3表等值連接+邏輯選擇條件selectstudent,sno,sname,ssex,sdept,course,eno,cname,gradefromstudent,course,scwherestudent.sno=o=sc.enoandssex=,男';例33:嵌套查詢示例★帶IN謂詞的子查詢查找和劉晨同一個(gè)系的人基本步驟1、查詢劉晨所在的系CS2、查詢CS系的學(xué)生3、查詢結(jié)果中去掉劉晨selectsno,sname,sdeptfromstudentwheresdeptin(selectsdeptfromstudentwheresname=,文!J晨');劉晨出現(xiàn)了重復(fù)selectsno,sname,sdeptfromstudentwheresdeptin(selectsdeptfromstudentwheresname=,文U晨')andsnameO,文ll晨';自連接方式查詢和劉晨同一個(gè)系的人selectsi.sno,si.sname,si.sdeptfromstudentsi,students2wheresi.sdept=s2.sdeptands2.sname=,劉|晨';劉晨出現(xiàn)了重復(fù)李勇CS李勇CS劉晨CS劉晨CS李勇CS劉晨CS劉晨CS李勇CS王敏MA王敏MA張立IS張立ISselectsi.sno,si.sname,si.sdeptfromstudentsi,students2wheresi.sdept=s2.sdeptands2.sname=,劉I晨'andsi.sname?'文[]晨';查找選修了“信息系統(tǒng)”課程的學(xué)生的學(xué)號(hào)和姓名基本步驟1、查詢Couse,得到“信息系統(tǒng)”的課號(hào)32、查詢SC,得到課號(hào)為3的學(xué)生的學(xué)號(hào)3、查詢Student,得到第2步學(xué)號(hào)所對(duì)應(yīng)的姓名查詢完整信息-通過(guò)多表連接方式selectstudent,sno,sname,course,eno,cname,gradefromstudent,sc,coursewherestudent.sno=o=ame=,信息系統(tǒng)';輸出結(jié)果只要學(xué)號(hào)和姓名-通過(guò)多表連接方式selectstudent,sno,snamefromstudent,sc,coursewherestudent.sno=o=ame=,信息系統(tǒng)';通過(guò)帶IN謂詞的子查詢selectsno,snamefromstudentwheresnoin(selectsnofromscwhereenoin(selectenofromcoursewherecname=,信息系統(tǒng)'));★帶比較運(yùn)算符的子查詢查找和劉晨同一個(gè)系的人selectsno,sname,sdeptfromstudentwheresdept=(selectsdeptfromstudentwheresname=,劉晨');劉晨出現(xiàn)了重復(fù)selectsno,sname,sdeptfromstudentwheresdept=(selectsdeptfromstudentwheresname=,劉晨')andsnameO?文lj晨';查詢和“劉晨”“張立”同一個(gè)系的人先插入兩條記錄insertintostudentvalues(*200215130),,張三','男',19,'IS');insertintostudentvaluesC200215131'李四','男',22,‘IS');查看全部數(shù)據(jù)select*fromstudent;查詢selectsno,sname,sdeptfromstudentwheresdeptin(selectsdeptfromstudentwheresname=,劉晨'orsname=,張立');有重復(fù)selectsno,sname,sdeptfromstudentwheresdeptin(selectsdeptfromstudentwheresname=,劉晨'orsname=,張立')and(sname<>,劉晨'orsname<>J張立');邏輯語(yǔ)法有錯(cuò)誤,無(wú)法達(dá)到預(yù)期要求selectsno,sname,sdeptfromstudentwheresdeptin(selectsdeptfromstudentwheresname=,劉晨'orsname=,張立')and(snameO)劉晨'andsnameO*張立');正確,但是在多關(guān)鍵字比較時(shí)比IN麻煩★帶ANY/ALL的子查詢查詢其它系中比“CS”系某一年齡的學(xué)生年齡還小的學(xué)生(小于任一年齡,即比CS中最大年齡小即可)帶ANY的子句selectsname,sagefromstudentwheresage<any(selectsagefromstudentwheresdept=,CS')andsdeptO,CS';集函數(shù)方式等價(jià)表示selectsname,sagefromstudentwheresage<(selectmax(sage)fromstudentwheresdept=,CS')andsdeptO*CS,;查詢其它系中比“CS”系年齡最小的學(xué)生還小的學(xué)生帶ALL子句selectsname,sagefromstudentwheresage<al](selectsagefromstudentwheresdept=,CS')andsdept<>'CS';集函數(shù)方式等價(jià)表示selectsname,sagefromstudentwheresage<(selectmin(sage)fromstudentwheresdept=,CS')andsdept<>'CS';★帶EXIST的子查詢查詢選修了2號(hào)課程的學(xué)生selectsnamefromstudentwhereexists(select*fromscwherecno=,2');查詢結(jié)果錯(cuò)誤EXIST查詢的執(zhí)行步驟:1、取主查詢中的一條記錄2、檢查子查詢是否有結(jié)果(無(wú)論一條還是多條記錄)3、若2有結(jié)果,輸出14、主查詢的所有記錄重復(fù)1-3selectsnamefromstudentwhereexists(select*fromscwheresno=student.snoandcno=,2');正確以下一樣selectstudent,snamefromstudent,scwherewheresc.sno=o=,2';查找和劉晨同一個(gè)系的人selectsno,sname,sdeptfromstudentsiwhereexists(select*fromstudents2wheres2.sname=,劉晨');不正確,全部結(jié)果selectsno,sname,sdeptfromstudentsiwhereexists(select*fromstudents2wheres2.sdept=sl.sdeptands2.sname=,文lj晨');劉晨有重復(fù)selectsno,sname,sdeptfromstudentsiwhereexists(select*fromstudents2wheres2.sdept=sl.sdeptands2.sname=,劉晨')andsi.snameO1劉晨';正確selectsno,sname,sdeptfromstudentsiwhereexists(select*fromstudents2wheres2.sdept=sl.sdeptandsi.snameO5劉晨'ands2.sname=,劉晨');正確查詢選修了全部課程的學(xué)生的姓名查詢步驟1、通過(guò)course表查詢?nèi)空n程的課號(hào)2、在SC中查詢是有同一學(xué)號(hào)選修了全部課號(hào)3、在student中查詢?cè)搶W(xué)號(hào)的姓名先增加4條記錄insertintoscvalues:200215121'4',79);insertintoscvalues('200215121'5',65);insertintoscvalues(,200215121'6',83);insertintoscvalues('200215121'7',93);selectsnamefromstudentwherenotexists(select*fromcoursewherenotexists(select*fromsc));不正確,全部結(jié)果selectsnamefromstudentwherenotexists(select*fromcoursewherenotexists(select*fromscwherecno=course.eno));不正確,全部結(jié)果selectsnamefromstudentwherenotexists(select*fromcoursewherenotexists(select*fromscwheresno=student.sno));不正確,還有李勇和劉晨(SC中有的學(xué)生)selectsnamefromstudentwherenotexists(select*fromcoursewherenotexists(select*fromscwheresno=student.snoandcno=course.eno));正確查詢至少選修了200215122選修的全部課程的學(xué)生的學(xué)號(hào)查詢步驟1、在course中查找200215122選修的幾門課的課號(hào)2、在course中查找選修課號(hào)包含200215122的學(xué)號(hào)3、在student中查找對(duì)應(yīng)的姓名selectdistinctsnofromscsexwherenotexists(select*fromscscywherenotexists(select*fromscsczwheresex.sno=,200215122,andscz.sno=o=scy.eno));空集合selectdistinctsnofromscsexwherenotexists(select*fromscscywherescy.sno=,200215122,andnotexists(select*fromscsczwherescz.sno=o=scy.eno));錯(cuò)誤,包含了200215122自己selectdistinctsnofromscsexwherenotexists(select*fromscscywherescy.sno=,200215122,andnotexists(select*fromscsczwherescz.sno=o=scy.eno))andscz.sno<>>200215122,;語(yǔ)法錯(cuò)誤(最外層查詢中沒(méi)有scz)selectdistinctsnofromscsexwherenotexists(select*fromscscywherescy.sno=,200215122,andnotexists(select*fromscsczwherescz.sno=o=scy.eno))andscy.snoO*200215122';語(yǔ)法錯(cuò)誤(最外層查詢中沒(méi)有scy)selectdistinctsnofromscwherenotexists(selectenofromscwherescy.sno=,200215122,andnotexists(select*fromscwherescz.sno=o=scy.eno))andsex.sno<>'200215122';selectdistinctsnofromscsexwherenotexists(select*fromscscywh
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年政策法規(guī)對(duì)評(píng)估行業(yè)的影響試題及答案
- 教育研究問(wèn)題研究
- 美容師水療與芳香療法知識(shí)試題及答案
- 2024小自考市場(chǎng)營(yíng)銷專業(yè)題及答案
- 營(yíng)養(yǎng)配比計(jì)算的重要性試題及答案
- 美容師考試復(fù)習(xí)中的跨學(xué)科知識(shí)整合試題及答案
- 學(xué)習(xí)2024年汽車維修工考試的有效方法與試題及答案
- 2024年汽車維修基礎(chǔ)知識(shí)試題及答案
- 汽車美容服務(wù)的創(chuàng)新思路試題及答案
- 智能工業(yè)機(jī)器人的未來(lái)發(fā)展趨勢(shì)
- 2023年上海嘉定區(qū)行政服務(wù)中心工作人員招聘筆試參考題庫(kù)附帶答案詳解
- 保健食品試卷答案
- #2鍋爐水冷壁安裝施工方案
- 光伏混凝土鉆孔灌樁基礎(chǔ)施工方案方案
- 2022年四川省特種設(shè)備作業(yè)安全管理人員考試題庫(kù)匯總(含真題和典型題)
- 公司發(fā)貨通知單
- 法語(yǔ)動(dòng)詞變位
- GB/T 247-2008鋼板和鋼帶包裝、標(biāo)志及質(zhì)量證明書的一般規(guī)定
- GB/T 24677.2-2009噴桿噴霧機(jī)試驗(yàn)方法
- GB/T 18915.1-2002鍍膜玻璃第1部分:陽(yáng)光控制鍍膜玻璃
- GB 3796-2006農(nóng)藥包裝通則
評(píng)論
0/150
提交評(píng)論