人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

數(shù)據(jù)庫表的查詢操作實踐演練(實驗三)

發(fā)布日期:2021-12-24 05:29 | 文章來源:gibhub

繼前兩次的實驗,本次實驗以熟練掌握利用select語句進(jìn)行各種查詢操作:單表查詢、多表連接及查詢、嵌套查詢、集合查詢等,鞏固數(shù)據(jù)庫查詢操作。
下面就跟著小編一起練習(xí)吧!
在實驗一創(chuàng)建并插入數(shù)據(jù)的表(Student, Course,SC,Teacher,TC)的基礎(chǔ)上,完成以下操作。
(1)將教師‘羅莉'的名字改為‘羅莉莉'。

復(fù)制代碼 代碼如下:
update Teacher set tname='羅莉莉' where tname='羅莉'

(2)將兩個同學(xué)(數(shù)據(jù)自己臨時設(shè)置,用后即刪除)的兩門課程的成績以運行sql程序文件的形式插入score表中。該題用以驗證、理解和掌握關(guān)系模型的完整性規(guī)則;
插入:
復(fù)制代碼 代碼如下:
insert into Score(sno,cno,grade) values ('04261006','C003','64')
insert into Score(sno,cno,grade) values('04261007','C004','79')

查詢:
復(fù)制代碼 代碼如下:
select sno 學(xué)號,cno 課程號,grade 分?jǐn)?shù)from Score where sno=04261006 or sno=04261007;

刪除:
復(fù)制代碼 代碼如下:
delete from Score where sno=04261006 or sno=04261007;

(3)求每門課的平均成績,并把結(jié)果存入average表(自行設(shè)計并創(chuàng)建);
復(fù)制代碼 代碼如下:
CREATE TABLE average
(
cno CHAR(8),
avscore numeric(5,2),
constraint a1 primary key (cno),
constraint a2 foreign key (cno) references Course(cno),
)
insert into average(cno,avscore)
select distinct cno ,avg(grade) from Score group by cno

(4)將學(xué)生“馬麗”的年齡改為24;
復(fù)制代碼 代碼如下:
Update Student set 2014-year(Sbirth) 年齡 where Sname=' 馬麗'

(5)將所有學(xué)生的szipcode屬性列值填補上;
復(fù)制代碼 代碼如下:
update Student set szipcode='221000'

(6)將average表中的所有課程的平均成績置零;
復(fù)制代碼 代碼如下:
update average set avscore='0'

(7)刪除average表中的課程號為‘C007'的平均成績記錄;
復(fù)制代碼 代碼如下:
delete from average where cno='C007'

(8)刪除所有average表中平均成績記錄;
復(fù)制代碼 代碼如下:
delete from average;

(9)建立一個臨時學(xué)生信息表(tstudent),刪除該表中的學(xué)號含‘101'的所有學(xué)生記錄。
復(fù)制代碼 代碼如下:
create table tstudent ( Sno char(8) primary key, Sname varchar(8) unique );
Delete from tstudent where Sno like '001011%';

(10)查詢?nèi)w學(xué)生的學(xué)號與姓名;
復(fù)制代碼 代碼如下:
select sno 學(xué)號,sname 姓名from Student

(11)查詢?nèi)w學(xué)生的學(xué)號、姓名、所屬系;
復(fù)制代碼 代碼如下:
select sno 學(xué)號,sname 姓名,sdept 系from Student

(12)查詢?nèi)w學(xué)生的詳細(xì)記錄;
復(fù)制代碼 代碼如下:
select * from Student

(13)查詢?nèi)w學(xué)生的姓名及其年齡;
復(fù)制代碼 代碼如下:
select sname 姓名,2014-year(sbirth) 年齡from Student

(14)查詢?nèi)w學(xué)生的姓名、出生年份;
復(fù)制代碼 代碼如下:
select sname 姓名,year(sbirth) 出生年份from Student

(15)查詢所有修過課的學(xué)生的學(xué)號;
復(fù)制代碼 代碼如下:
select distinct sno from Score
select distinct student.sno from Student,Score where Student.sno=Score.sno and Score.grade>0 ;

(16)查詢“計算機(jī)系”班全體學(xué)生名單;
復(fù)制代碼 代碼如下:
select sno,sname from Student where sdept='計算機(jī)系'

(17)查詢查詢所有年齡在23歲以下的學(xué)生姓名及其年齡;
復(fù)制代碼 代碼如下:
select sname 姓名,2014-year(sbirth) 年齡from Student where 2014-year(sbirth)<23;

(18)查詢考試成績有不及格的學(xué)生的學(xué)號;
復(fù)制代碼 代碼如下:
select distinct sno from Score where grade<60;

(19)查詢年齡在20至22歲之間的學(xué)生姓名、系和年齡;
復(fù)制代碼 代碼如下:
select sname 姓名,sdept 系,2014-year(sbirth) 年齡from student where 2014-year(sbirth) between 20 and 22;

(20)查詢年齡不在20至22歲之間的學(xué)生姓名、系和年齡;
復(fù)制代碼 代碼如下:
select sname 姓名,sdept 系,2014-year(sbirth) 年齡from student where 2014-year(sbirth) not between 20 and 22;

(21)查詢“計算機(jī)系”和“電商系”的學(xué)生的姓名;
復(fù)制代碼 代碼如下:
select sname from Student where sdept='計算機(jī)系' or sclass='電商系'

(22)查詢既不是“計11”也不是“計61”班的學(xué)生的姓名和班級信息;
復(fù)制代碼 代碼如下:
select sname,sclass from Student where sclass not in('計','計');
(23)查詢學(xué)號為“04262002”的學(xué)生的詳細(xì)情況;
[code]select student.sno,sname,ssex,2014-year(sbirth),sclass,grade from Student,Score where Student.sno=Score.sno and Student.sno='04262002';

(24)查詢學(xué)號以“04262”打頭的學(xué)生信息;
復(fù)制代碼 代碼如下:
select * from Student where sno like '04262%'

(25)查詢所有姓“張”學(xué)生的學(xué)號、姓名、性別、年齡;
復(fù)制代碼 代碼如下:
select sno 學(xué)號,sname 姓名,ssex 性別,2011-year(sbirth) 年齡from Student where sname like'王%'

(26)查詢名字中第二個字有“?!弊值膶W(xué)生的學(xué)號、姓名、性別、年齡;
復(fù)制代碼 代碼如下:
select sno 學(xué)號,sname 姓名,ssex 性別,2011-year(sbirth) 年齡from Student where sname like '_田%'

(27)查詢所有不姓“劉”學(xué)生的姓名;
復(fù)制代碼 代碼如下:
select sname 姓名from Student where sname not like '劉%'

(28)查詢課程號以“C”開頭的最后兩個字母為“05”的課程號和課程名;
復(fù)制代碼 代碼如下:
select cno,cname from Course where cno like 'C%05'

(29)某些學(xué)生選修某門課程后沒有參加考試,所以有選修課記錄,但沒有考試成績,試查找缺少考試成績的學(xué)生和相應(yīng)的課程號;
復(fù)制代碼 代碼如下:
select Student.sno,sname,cno from Student,Score where Student.sno=Score.sno and grade is NULL;

(30)查找全部有成績記錄的學(xué)生學(xué)號、課程號;
復(fù)制代碼 代碼如下:
select sno, cno from Score where grade is not NULL;

(31)查找“計算機(jī)系”年齡在22歲以下的學(xué)生學(xué)號、姓名;
復(fù)制代碼 代碼如下:
select sno ,sname from Student where sdept='計算機(jī)系' and 2014-year(sbirth)<22

(32)查找選修了“C001”號課程的學(xué)生學(xué)號及其成績,查詢結(jié)果按分?jǐn)?shù)降序排序;
復(fù)制代碼 代碼如下:
select student.sno,grade from student,Score where Student.sno=Score.sno and cno='C001' order by grade desc;

(33)查詢?nèi)w學(xué)生情況,查詢結(jié)果按所在系升序排列,對同一系中的學(xué)生按年齡降序排列;
復(fù)制代碼 代碼如下:
select * from student order by sdept asc,2014-year(sbirth) desc;

(34)查詢學(xué)生總?cè)藬?shù);
復(fù)制代碼 代碼如下:
select count(*) 人數(shù)from Student;

(35)查詢選修了課程的學(xué)生人數(shù);
復(fù)制代碼 代碼如下:
select count(distinct sno)人數(shù)from Score;

(36)在所有課程中查詢最高分的學(xué)生學(xué)號和成績;
復(fù)制代碼 代碼如下:
select sno,grade from Score where grade =(select max(grade)from Score )

復(fù)制代碼 代碼如下:
select distinct a.* from Score a where a.sno IN (select top 1 Score.sno from Score where Score.cno = a.cno order by grade desc)

(37)查詢學(xué)習(xí)“C001”課程的學(xué)生最高分?jǐn)?shù);
復(fù)制代碼 代碼如下:
select max(grade)最高分?jǐn)?shù)from Score where cno='C001'

(38)計算各個課程號與相應(yīng)的選課人數(shù);
復(fù)制代碼 代碼如下:
select count(sno) 選課人數(shù)from Score group by cno;

(39)查詢“計算機(jī)系”選修了兩門課程以上的學(xué)生學(xué)號、姓名;
復(fù)制代碼 代碼如下:
select Student.sno,sname from Student where Student.sno in
(select Student.sno from Student,Score where
sdept='計算機(jī)系'and Student.sno=Score.sno group by Student.sno having count(cno)>=2);

(40)自然連接student和score表;
復(fù)制代碼 代碼如下:
select student.*,Score.grade from student ,Score where student.sno=Score.sno;

(41)使用自身連接查詢每一門課程的間接先行課(即先行課的先行課)
復(fù)制代碼 代碼如下:
select a.cno,b.cpno from Course a,Course b where a.cpno=b.cno;

(42)使用復(fù)合條件連接查詢選修“c001”號課程且成績在90分以上的所有同學(xué);
復(fù)制代碼 代碼如下:
select sname,grade from student,Score where Student.sno=Score.sno and cno='C001' and grade>=90;

(43)使用復(fù)合條件連接查詢每個學(xué)生選修的課程名及其成績;
復(fù)制代碼 代碼如下:
select Student.sno,sname,cname,grade from Course,Score,Student where Course.cno=Score.cno and student.sno=Score.sno;

(44)查詢選修了全部課程的學(xué)生;
復(fù)制代碼 代碼如下:
select Sname from Student where not exists (select * from Course where not exists(select * from Score where Sno=Student.Sno and Cno=Course.Cno))

(45)查詢所有選修了C001號課程的學(xué)生學(xué)號、姓名;
復(fù)制代碼 代碼如下:
select student.sno,sname from student,Score where student.sno=Score.sno and cno='C001';
(46)查詢選修了課程C001或C007的學(xué)生學(xué)號、姓名;
[code]select student.sno,sname,cno from student,Score where student.sno=Score.sno and cno in ('C001','C007');
[/code]
(47)查詢“計算機(jī)系”的學(xué)生及年齡不大于23歲的學(xué)生;
復(fù)制代碼 代碼如下:
select sno ,sname,2014-year(sbirth) age ,sclass from student where sdept='計算機(jī)系' or 2014-year(sbirth)<=23;

(48)查詢既選修了課程C001又選修了課程C007的所有學(xué)生學(xué)號、姓名;
復(fù)制代碼 代碼如下:
select student.sno,sname from student,Score where student.sno=Score.sno and cno='C001' and student.sno in (select student.sno from student,Score where student.sno=Score.sno and cno='C007')

(49)查詢選修了課程名為“數(shù)據(jù)庫原理”的學(xué)生的學(xué)號、姓名、性別、年齡;
復(fù)制代碼 代碼如下:
select student.sno ,sname,ssex,cname,2011-year(sbirth) age from student,Score,Course where student.sno=Score.sno and Score.cno=Course.cno and cname='數(shù)據(jù)庫原理';

(50)查詢其他班中比“計算機(jī)系”所有學(xué)生年齡都小的學(xué)生名單;
復(fù)制代碼 代碼如下:
select sno,sname ,2014-year(sbirth) age from student where 2014-year(sbirth)<(select min(2014-year(sbirth)) from student where sclass='計61')and sclass !='計61';

(51)查詢與“夏天”在同一個系學(xué)習(xí)的學(xué)生學(xué)號、姓名、性別、年齡;
復(fù)制代碼 代碼如下:
select sno,sname,ssex,2014-year(sbirth) age from student where sdept=(select sdept from student where sname='夏天') and sname!='夏天'

(52)建立“計算機(jī)系”學(xué)生的視圖1;
復(fù)制代碼 代碼如下:
create view view_student
as select sno,sname,ssex,sbirth,sclass from student where sclass='13z網(wǎng)絡(luò)'

(53)建立“計算機(jī)系”學(xué)生的視圖2,并要求進(jìn)行修改與插入時,仍須保證該視圖只有“計算機(jī)系”班學(xué)生;
復(fù)制代碼 代碼如下:
create view view_student2
as select sno,sname,ssex,sbirth,sclass from student where sclass='13z網(wǎng)絡(luò)' with check option;

(54)建立“計算機(jī)系”選修了“C001”課程的學(xué)生的視圖,定義視圖名為“v_cs_C001_student1”;
復(fù)制代碼 代碼如下:
create view v_cs_C001_student1
as select student.sno,sname,ssex,sbirth,sclass from Student ,Score where
student.sno=Score.sno and sclass='13z網(wǎng)絡(luò)' and cno='C001';

(55)建立“計算機(jī)系”班選修了“C001”課程且成績在90分以上的學(xué)生的視圖,定義視圖名為“cs_c001_student2”;
復(fù)制代碼 代碼如下:
create view cs_c001_student2
as
select student.sno,sname ,ssex,sbirth,sclass,cno from student,Score where
student.sno=Score.sno and cno='C001' and sclass='13z網(wǎng)絡(luò)'and student.sno in (select student.sno from student,Score where student.sno=Score.sno and grade>90)

(56)定義一個反映學(xué)生年齡的視圖,定義視圖名為“v_birth_student”;
復(fù)制代碼 代碼如下:
create view v_birth_student
as
select sno,sname,2014-year(sbirth) age from student

(57)將學(xué)生表中所有女生記錄定義為一個視圖,視圖名為“v_female_student”;
復(fù)制代碼 代碼如下:
create view v_female_student
as
select * from student where ssex='女';

(58)將學(xué)生的學(xué)號及其平均成績定義為一個視圖,視圖名為“v_average_student”;
復(fù)制代碼 代碼如下:
create view v_average_student
as
select sno,avg(grade) avscore from Score group by sno;

(59)在“計算機(jī)系”學(xué)生視圖中找出年齡小于22歲的學(xué)生;
復(fù)制代碼 代碼如下:
select * from view_student where 2014-year(sbirth)<=22;

(60)利用視圖查詢“計算機(jī)系”選修了“C001”課程的學(xué)生;
復(fù)制代碼 代碼如下:
select * from v_cs_C001_student1;

(61)通過(52)中的“計算機(jī)系”視圖修改某個學(xué)生的名字;
復(fù)制代碼 代碼如下:
update view_student set sname='王某某'where sno=04261001;

(62)通過(53)中的“計算機(jī)系”視圖,插入一個新學(xué)生記錄。
復(fù)制代碼 代碼如下:
insert into view_student2(sno,sname,ssex,sbirth,sclass) values ('04262004','張某某','男','1987/11/09','計');

(63)通過(53)中的“計算機(jī)系”視圖,刪除一個學(xué)生記錄。
復(fù)制代碼 代碼如下:
delete from view_student2 where sno='04262004'and sname='張某某';

實驗課結(jié)束了,相信通過本節(jié)課的實踐操作,小伙伴們都對數(shù)據(jù)庫表的操作有了更進(jìn)一步的了解。
以上就是查詢數(shù)據(jù)庫表的基本操作,幾乎涵蓋了各種查詢操作所遇到的情況,值得大家親自操作一下,相信對大家的學(xué)習(xí)有所幫助。

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部