佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > 數(shù)據(jù)庫表的查詢操作實(shí)踐演練(實(shí)驗(yàn)三)

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

熱門標(biāo)簽:百度競價(jià)排名 地方門戶網(wǎng)站 AI電銷 Linux服務(wù)器 呼叫中心市場需求 鐵路電話系統(tǒng) 網(wǎng)站排名優(yōu)化 服務(wù)外包

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

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

(2)將兩個(gè)同學(xué)(數(shù)據(jù)自己臨時(shí)設(shè)置,用后即刪除)的兩門課程的成績以運(yùn)行sql程序文件的形式插入score表中。該題用以驗(yàn)證、理解和掌握關(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é)號(hào),cno 課程號(hào),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è)計(jì)并創(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屬性列值填補(bǔ)上;
復(fù)制代碼 代碼如下:
update Student set szipcode='221000'

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

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

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

(9)建立一個(gè)臨時(shí)學(xué)生信息表(tstudent),刪除該表中的學(xué)號(hào)含‘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é)號(hào)與姓名;
復(fù)制代碼 代碼如下:
select sno 學(xué)號(hào),sname 姓名from Student

(11)查詢?nèi)w學(xué)生的學(xué)號(hào)、姓名、所屬系;
復(fù)制代碼 代碼如下:
select sno 學(xué)號(hào),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é)號(hào);
復(fù)制代碼 代碼如下:
select distinct sno from Score
select distinct student.sno from Student,Score where Student.sno=Score.sno and Score.grade>0 ;

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

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

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

(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ì)算機(jī)系”和“電商系”的學(xué)生的姓名;
復(fù)制代碼 代碼如下:
select sname from Student where sdept='計(jì)算機(jī)系' or sclass='電商系'

(22)查詢既不是“計(jì)11”也不是“計(jì)61”班的學(xué)生的姓名和班級(jí)信息;
復(fù)制代碼 代碼如下:
select sname,sclass from Student where sclass not in('計(jì)','計(jì)');
(23)查詢學(xué)號(hào)為“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é)號(hào)以“04262”打頭的學(xué)生信息;
復(fù)制代碼 代碼如下:
select * from Student where sno like '04262%'

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

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

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

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

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

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

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

(32)查找選修了“C001”號(hào)課程的學(xué)生學(xué)號(hào)及其成績,查詢結(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é)號(hào)和成績;
復(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)計(jì)算各個(gè)課程號(hào)與相應(yīng)的選課人數(shù);
復(fù)制代碼 代碼如下:
select count(sno) 選課人數(shù)from Score group by cno;

(39)查詢“計(jì)算機(jī)系”選修了兩門課程以上的學(xué)生學(xué)號(hào)、姓名;
復(fù)制代碼 代碼如下:
select Student.sno,sname from Student where Student.sno in
(select Student.sno from Student,Score where
sdept='計(jì)算機(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”號(hào)課程且成績在90分以上的所有同學(xué);
復(fù)制代碼 代碼如下:
select sname,grade from student,Score where Student.sno=Score.sno and cno='C001' and grade>=90;

(43)使用復(fù)合條件連接查詢每個(gè)學(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號(hào)課程的學(xué)生學(xué)號(hào)、姓名;
復(fù)制代碼 代碼如下:
select student.sno,sname from student,Score where student.sno=Score.sno and cno='C001';
(46)查詢選修了課程C001或C007的學(xué)生學(xué)號(hào)、姓名;
[code]select student.sno,sname,cno from student,Score where student.sno=Score.sno and cno in ('C001','C007');
[/code]
(47)查詢“計(jì)算機(jī)系”的學(xué)生及年齡不大于23歲的學(xué)生;
復(fù)制代碼 代碼如下:
select sno ,sname,2014-year(sbirth) age ,sclass from student where sdept='計(jì)算機(jī)系' or 2014-year(sbirth)=23;

(48)查詢既選修了課程C001又選修了課程C007的所有學(xué)生學(xué)號(hào)、姓名;
復(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é)號(hào)、姓名、性別、年齡;
復(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ì)算機(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='計(jì)61')and sclass !='計(jì)61';

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

(52)建立“計(jì)算機(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ì)算機(jī)系”學(xué)生的視圖2,并要求進(jìn)行修改與插入時(shí),仍須保證該視圖只有“計(jì)算機(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ì)算機(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ì)算機(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)定義一個(gè)反映學(xué)生年齡的視圖,定義視圖名為“v_birth_student”;
復(fù)制代碼 代碼如下:
create view v_birth_student
as
select sno,sname,2014-year(sbirth) age from student

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

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

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

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

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

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

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

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

您可能感興趣的文章:
  • MySQL學(xué)習(xí)筆記3:表的基本操作介紹
  • 單個(gè)select語句實(shí)現(xiàn)MySQL查詢統(tǒng)計(jì)次數(shù)
  • sql查詢出各科成績最好的學(xué)生信息
  • mysql查詢昨天 一周前 一月前 一年前的數(shù)據(jù)
  • mysql查詢今天、昨天、近7天、近30天、本月、上一月的SQL語句
  • MySql查詢時(shí)間段的方法
  • MySQL查詢和修改auto_increment的方法
  • 一個(gè)優(yōu)化MySQL查詢操作的具體案例分析
  • MySQL查詢倒數(shù)第二條記錄實(shí)現(xiàn)方法
  • 50條SQL查詢技巧、查詢語句示例
  • SQL查詢出表、存儲(chǔ)過程、觸發(fā)器的創(chuàng)建時(shí)間和最后修改時(shí)間示例
  • 大幅優(yōu)化MySQL查詢性能的奇技淫巧
  • SQL大量數(shù)據(jù)查詢的優(yōu)化及非用like不可時(shí)的處理方案
  • 如何使用MySQL查詢某個(gè)列中相同值的數(shù)量統(tǒng)計(jì)
  • SQL如何實(shí)現(xiàn)MYSQL的遞歸查詢
  • 數(shù)據(jù)庫表的創(chuàng)建、管理和數(shù)據(jù)操作(實(shí)驗(yàn)一)
  • 數(shù)據(jù)庫表的查詢操作(實(shí)驗(yàn)二)

標(biāo)簽:銅川 黃山 仙桃 湖南 湘潭 崇左 蘭州 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《數(shù)據(jù)庫表的查詢操作實(shí)踐演練(實(shí)驗(yàn)三)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    天峨县| 安丘市| 通榆县| 澄江县| 紫阳县| 广南县| 且末县| 江津市| 定日县| 宁波市| 青阳县| 永新县| 甘南县| 西昌市| 周口市| 泽州县| 九寨沟县| 平罗县| 时尚| 盘山县| 崇州市| 都江堰市| 四川省| 罗定市| 荣成市| 上思县| 芮城县| 阜南县| 临泉县| 应用必备| 石楼县| 会理县| 常宁市| 闵行区| 荃湾区| 普陀区| 弥勒县| 库尔勒市| 方城县| 西畴县| 扶绥县|