




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、實(shí)訓(xùn)的課程名稱*參加實(shí)訓(xùn)的年級及學(xué)期實(shí)訓(xùn)的周數(shù)或天數(shù)一、實(shí)訓(xùn)的目的*二、實(shí)訓(xùn)的理論基礎(chǔ)*三、實(shí)訓(xùn)內(nèi)容1.*2.*四、實(shí)訓(xùn)要求*五、實(shí)訓(xùn)安排 時間 任 務(wù) 地點(diǎn) 指導(dǎo)教師 * * * * * *六、實(shí)訓(xùn)紀(jì)律及注意事項(xiàng) 1.*2.*3.*4.*七、考核辦法及標(biāo)準(zhǔn)*數(shù)據(jù)庫原理與應(yīng)用實(shí)訓(xùn)報告班 級 二級學(xué)院 姓 名 實(shí)訓(xùn)地點(diǎn):指導(dǎo)教師: 年 月 日實(shí)訓(xùn)報告第一部分:實(shí)訓(xùn)目的通過本次課程設(shè)計讓學(xué)生能夠綜合運(yùn)用所學(xué)的關(guān)系數(shù)據(jù)庫原理知識解決并能設(shè)計一個實(shí)際問題,進(jìn)一步掌握數(shù)據(jù)庫原理的相關(guān)理論和數(shù)據(jù)庫的設(shè)計實(shí)現(xiàn)過程,進(jìn)一步提高學(xué)生的分析問題和解決問題的能力以及學(xué)生的動手能力,能夠獨(dú)立完成數(shù)據(jù)庫的功能的設(shè)計和開
2、發(fā)。第二部分:實(shí)訓(xùn)準(zhǔn)備 安裝有SQL SERVER 2005以上版本的軟件的計算機(jī)。第三部分:實(shí)訓(xùn)要求 1.畫出所給任務(wù)中表的E-R圖2.實(shí)訓(xùn)部分(全部使用T-SQL語句完成以下實(shí)驗(yàn)內(nèi)容)第四部分:實(shí)訓(xùn)內(nèi)容(步驟及程序)l E-R圖cs_tmcs_scdeptst_fromst_score slt_cousecousestudentdp_idscoresltdatecs_idcs_idcs_nmst_idst_dpidst_datest_idst_sexxst_nmst_birthst_mntdt_teldp_iddp_drtdp_nmm實(shí)驗(yàn)1 數(shù)據(jù)庫操作1創(chuàng)建數(shù)據(jù)庫:操作1.1:創(chuàng)建一個te
3、st數(shù)據(jù)庫,其主數(shù)據(jù)文件邏輯名test_data,物理文件名test_data.mdf,初始大小10MB,最大尺寸為無限大,增長速度1MB;數(shù)據(jù)庫日志文件邏輯名稱為test_log,物理文件名為test_log.ldf,初始大小為1MB,最大尺寸為5MB,增長速度為10%。create database test數(shù)據(jù)庫on(name='test_data',filename='D:課程設(shè)計數(shù)據(jù)庫test_data.mdf',size=10mb,filegrowth=1mb,maxsize=unlimited)log on(name='test_log
4、39;,filename='D:課程設(shè)計數(shù)據(jù)庫test_log.ldf',size=1mb,filegrowth=10%,maxsize=5mb)go2查看數(shù)據(jù)庫屬性:操作1.2:使用T-SQL語句查看數(shù)據(jù)庫test屬性exec sp_helpdb test數(shù)據(jù)庫go3刪除數(shù)據(jù)庫:操作1.3:使用T-SQL語句刪除數(shù)據(jù)庫test drop database test數(shù)據(jù)庫go實(shí)驗(yàn)2 表操作1創(chuàng)建表:操作2.1:創(chuàng)建學(xué)生表:表名:student 說明:學(xué)生基本信息表屬性列數(shù)據(jù)類型長度空值列約束說明st_idnVarChar9Not NullPK學(xué)生學(xué)號st_n
5、mnVarChar8Not Null 學(xué)生姓名st_sexnVarChar2Null 學(xué)生性別st_birthdatetime Null 出生日期st_scoreint Null 入學(xué)成績st_datedatetime Null 入學(xué)日期st_fromnChar20Null 學(xué)生來源st_dpidnVarChar2Null 所在系編號st_mnttinyint Null 學(xué)生職務(wù)use test數(shù)據(jù)庫gocreate table student(st_id nvarchar
6、(9) not null primary key,st_nm nvarchar (8) not null,st_sex nvarchar (2),st_birth datetime ,st_score int ,st_date datetime,st_from nchar (20),st_dpid nvarchar (2),st_mnt tinyint )go操作2.2:創(chuàng)建課程信息表:表名:couse 說明:課程信息表屬性列數(shù)據(jù)類型長度空值列約束說明cs_idnVarChar4Not NullPK課程編號cs_nmnVarChar20Not Null 課程名稱cs_tmi
7、nt Null 課程學(xué)時cs_scint Null 課程學(xué)分use test數(shù)據(jù)庫gocreate table couse(cs_id nvarchar (4) not null primary key,cs_nm nvarchar (20) not null,cs_tm int ,cs_sc int)go操作2.3:創(chuàng)建選課表:表名:slt_couse 說明:選課表屬性列數(shù)據(jù)類型長度空值列約束說明cs_idnVarChar4Not NullFK課程編號st_idnVarChar9Not NullFK學(xué)生編號scoreint Null
8、 課程成績sltdatedatetime Null 選課日期use test數(shù)據(jù)庫gocreate table slt_couse(cs_id nvarchar (4) not null,st_id nvarchar (9) not null,score int,sltdate datetime)goalter table slt_couseadd constraint fk_cs_idforeign key (cs_id)references couse(cs_id)goalter table slt_couseadd constraint fk_st_idfo
9、reign key (st_id)references student(st_id)go操作2.4:創(chuàng)建院系信息表:表名:dept 說明:院系信息表屬性列數(shù)據(jù)類型長度空值列約束說明dp_idnVarChar2Not Null 系編號dp_nmnVarChar20Not Null 院系名稱dp_drtnVarChar8Null 院系主任dt_telnVarChar12Null 聯(lián)系電話use test數(shù)據(jù)庫gocreate table dept(dp_id nvarchar(2) not null,dp_nm nvarchar(20) not n
10、ull,dp_drt nvarchar (8) ,dt_tel nvarchar (12)go2修改表結(jié)構(gòu):(1)向表中添加列:操作2.5:為“dept”表添加“dp_count”列(數(shù)據(jù)類型為nvarchar,長度為3,允許為空)use test數(shù)據(jù)庫goalter table deptadd dp_count nvarchar (3)go(2)修改列數(shù)據(jù)類型:操作2.6:修改“dept”表的“dp_count”列數(shù)據(jù)類型為intuse test數(shù)據(jù)庫goalter table deptalter column dp_count intgo(3)刪除表中指定列:操作2.7:刪除“dept”表
11、的“dp_count”列use test數(shù)據(jù)庫goalter table deptdrop column dp_countgo3刪除表操作2.8:刪除“dept”表use test數(shù)據(jù)庫godrop table deptgo4向表中輸入數(shù)據(jù)記錄操作2.9:分別向“student”表、“couse”表、“slt_couse”表、“dept”表中輸入數(shù)據(jù)記錄use test數(shù)據(jù)庫goinsert studentvalues ('000000001','小二','男','1991-01-01','89','201
12、1-09-01','廣東','10','1')insert studentvalues ('000000002','小三','男','1991-01-02','79','2011-09-01','廣東','12','1')insert studentvalues ('000000003','小四','女','1991-01-03',&
13、#39;69','2011-09-01','廣東','8','1')insert studentvalues ('000000004','小六','男','1991-04-01','59','2011-09-01','廣東','13','1')insert studentvalues ('000000005','小七','男',
14、9;1991-01-11','89','2011-09-01','廣東','11','1')Gouse test數(shù)據(jù)庫goinsert cousevalues ('0001','計算機(jī)網(wǎng)絡(luò)','72','2')insert cousevalues ('0002','c語言程序設(shè)計','72','2')insert cousevalues ('0003','S
15、QL數(shù)據(jù)庫','72','2')insert cousevalues ('0004','計算機(jī)導(dǎo)論','72','2')insert cousevalues ('0005','高級數(shù)學(xué)','72','2')Gouse test數(shù)據(jù)庫goinsert slt_cousevalues ('0001','000000001','72','2011-10-12')inser
16、t slt_cousevalues ('0002','000000002','92','2011-10-12')insert slt_cousevalues ('0003','000000003','62','2011-10-12')insert slt_cousevalues ('0004','000000004','82','2011-10-12')insert slt_cousevalues (
17、39;0005','000000005','52','2011-10-12')use test數(shù)據(jù)庫goinsert deptvalues ('8','藝術(shù)系','王大仁','12345768')insert deptvalues ('9','建筑系','李小仁','12645678')insert deptvalues ('10','信息系','李大仁','
18、;12349878')insert deptvalues ('13','管理系','王小仁','16345678')insert deptvalues ('12','外語系','王小明','14512678')go實(shí)驗(yàn)3 數(shù)據(jù)完整性1空值約束( NULL )操作3.1:將student表中的st_sex列屬性更改為NOT NULLuse test數(shù)據(jù)庫goalter table studentalter column st_sex nvarchar(2) no
19、t nullgo2默認(rèn)值約束( DEFAULT )操作3.2:將student表中的st_from列默認(rèn)值設(shè)置為“陜西省”use test數(shù)據(jù)庫goalter table studentadd constraint df_fromdefault '陜西省' for st_fromgo3默認(rèn)值對象操作3.3:創(chuàng)建默認(rèn)值對象df_today為當(dāng)前日期,并將其綁定到slt_couse表中的sltdate列,然后取消綁定,最后刪除默認(rèn)值對象df_today。use test數(shù)據(jù)庫gocreate default df_today as '2014-12-30' goex
20、ec sp_bindefault 'df_today','slt_couse.sltdate'goexec sp_unbindefault 'slt_couse.sltdate'godrop default df_today go4檢查約束( CHECK )操作3.4:將slt_couse表中的score列的檢查約束設(shè)置為>=0且<=100use test數(shù)據(jù)庫goalter table slt_couseadd constraint ck_scorecheck (score>=0 and score<=100)go5規(guī)則
21、約束對象操作3.5:創(chuàng)建規(guī)則約束對象rl_sex,用于檢查性別的取值僅限于“男”和“女”,并將其綁定到student表中的st_sex列,然后取消綁定,最后刪除規(guī)則約束對象rl_sex。use test數(shù)據(jù)庫gocreate rule rl_sex as sex ='男' or sex='女'goexec sp_bindrule 'rl_sex' , 'student.st_sex'goexec sp_unbindrule 'student.st_sex'godrop rule rl_sex go6主鍵操作3.6:
22、將dept表中的dp_id列設(shè)置為主鍵use test數(shù)據(jù)庫goalter table deptadd constraint pk_dp_idprimary key (dp_id)go7唯一性約束( UNIQUE )操作3.7:將dept表中的dp_nm列設(shè)置為唯一性約束use test數(shù)據(jù)庫goalter table deptadd constraint ix_nmunique (dp_nm)go8標(biāo)識列操作3.8:向slt_couse表中添加標(biāo)識列id,第1行默認(rèn)值為1,相鄰兩個標(biāo)識列間的增量為1use test數(shù)據(jù)庫goalter table slt_couseadd id int ID
23、ENTITY (1,1) not nullgo9外鍵( FOREIGN KEY )操作3.9:被參照表為dept,參照表為studentuse test數(shù)據(jù)庫goalter table studentadd constraint fk_dpidforeign key (st_dpid)references dept(dp_id)go實(shí)驗(yàn)4 數(shù)據(jù)更新1表中插入數(shù)據(jù)操作4.1:向dept表插入一條記錄,系號11,系名自動控制系,系主任為李其余,電話81234567use test數(shù)據(jù)庫goinsert deptvalues ('11','自動控制系','李其
24、余','81234567')go操作4.2:向student表插入一條記錄,學(xué)號070201001,姓名為王小五,性別為男,出生日期1990年9月9日,系號為11,其余字段為NULL或默認(rèn)值use test數(shù)據(jù)庫goinsert student(st_id,st_nm,st_sex,st_birth, st_dpid)values ('070201001','王小五','男','1990-9-9','11')go操作4.3:向couse表插入一條記錄,課程號1234,課程名為操作系統(tǒng),其余字
25、段為NULL或默認(rèn)值use test數(shù)據(jù)庫goinsert couse(cs_id,cs_nm)values ('1234','操作系統(tǒng)')go操作4.4:向slt_couse表插入一條記錄,課程號1234,學(xué)名070201001,其余字段為NULL或默認(rèn)值use test數(shù)據(jù)庫goinsert slt_couse (cs_id,st_id)values ('1234','070201001')go2修改表中數(shù)據(jù)操作4.5:修改student表記錄,將王小五的入學(xué)成績改為88use test數(shù)據(jù)庫goupdate students
26、et st_score ='88'where st_nm='王小五'go操作4.6:修改couse表記錄,將所有記錄的學(xué)分改為4,學(xué)時改為64use test數(shù)據(jù)庫goupdate couseset cs_sc='4' ,cs_tm='64'go操作4.7:修改slt_couse表記錄,將課程號為1234,學(xué)名為070201001的記錄的成績改為77use test數(shù)據(jù)庫goupdate slt_couseset score= 77where cs_id='1234'and st_id='070201001
27、'go3刪除表中數(shù)據(jù)操作4.8:刪除slt_couse表記錄,將課程號為1234,學(xué)名為070201001的記錄刪除use test數(shù)據(jù)庫godelete slt_cousewhere cs_id='1234'and st_id='070201001'go操作4.9:刪除couse表記錄,將課程號為1234的記錄刪除use test數(shù)據(jù)庫godelete cousewhere cs_id='1234'go實(shí)驗(yàn)5 數(shù)據(jù)查詢(1)簡單查詢(1)查詢表中所有的列操作5.1:查詢所有系的信息use test數(shù)據(jù)庫goselect *from de
28、ptgo(2)查詢表中指定列的信息操作5.2:查詢所有的課程號與課程名稱use test數(shù)據(jù)庫goselect cs_id ,cs_nmfrom couse(3)在查詢列表中使用列表達(dá)式操作5.3:在查詢student表時使用列表達(dá)式:入學(xué)成績+400use test數(shù)據(jù)庫goselect st_score+400from studentgo(4)重新命名查詢結(jié)果操作5.4:使用AS關(guān)鍵字為dept表中屬性指定列名:系號、系名、系主任、聯(lián)系電話use test數(shù)據(jù)庫goselect dp_id as '系號',dp_nm as '系名',dp_drt as
29、39;系主任', dt_tel as '聯(lián)系電話'from deptgo操作5.5:使用"="號為couse表中屬性指定列名:課程號、課程名、學(xué)時(=cs_sc*16)、學(xué)分use test數(shù)據(jù)庫goselect '課程號'=cs_id,'課程名'=cs_nm ,'學(xué)時=cs_sc*16'=cs_tm, '學(xué)分'=cs_scfrom cousego(5)增加說明列操作5.6:查詢dept表的系號、系名和系主任,向查詢結(jié)果中插入說明列:系號、系名和系主任use test數(shù)據(jù)庫goselec
30、t dp_id '系號' ,dp_nm '系名',dp_drt '系主任'from deptgo(6)查詢列表中使用系統(tǒng)函數(shù)操作5.7:顯示所有學(xué)生的學(xué)號、姓名、性別和入學(xué)年份use test數(shù)據(jù)庫goselect st_id ,st_nm ,st_sex , datepart(yy,st_date) '入學(xué)年份'from studentgo操作5.8:顯示所有學(xué)生學(xué)號、姓名、性別和班級(學(xué)號前6位)use test數(shù)據(jù)庫goselect left(st_id ,6) '班級',st_nm ,st_sex , st
31、_dpidfrom studentgo(7)消除查詢結(jié)果中的重復(fù)項(xiàng)操作5.9:顯示所有學(xué)生班級use test數(shù)據(jù)庫goselect distinct left(st_id,6) '班級'from studentgo(8)取得查詢結(jié)果的部分行集操作5.10:顯示前5條學(xué)生記錄信息use test數(shù)據(jù)庫goselect top 5 *from studentgo操作5.11:顯示前25%條學(xué)生記錄信息use test數(shù)據(jù)庫goselect top 25 percent *from studentgo操作5.12:顯示前n條學(xué)生記錄信息,n為局部變量use test數(shù)據(jù)庫godec
32、lare n intset n = 5select top n *from studentgo實(shí)驗(yàn)6 數(shù)據(jù)查詢(2)條件查詢1使用關(guān)系表達(dá)式表示查詢條件操作6.1:查詢dept表中系號為11的院系信息use test數(shù)據(jù)庫goselect *from deptwhere dp_id='11'go操作6.2:查詢student表中11系的學(xué)生學(xué)號、姓名、性別和所在系編號use test數(shù)據(jù)庫goselect st_id ,st_nm ,st_sex , st_dpidfrom studentwhere st_dpid ='11'go操作6.3:查詢student表
33、中2008年及以后入學(xué)的學(xué)生信息use test數(shù)據(jù)庫goselect *from studentwhere datepart(yy,st_date)>='2008'go操作6.4:在查詢student表080808班學(xué)生的學(xué)號、姓名、性別和入學(xué)成績use test數(shù)據(jù)庫goselect left(st_id,6) ,st_nm ,st_sex ,st_scorefrom studentwhere left(st_id,6)='080808'go2使用邏輯表達(dá)式表示查詢條件操作6.5:查詢student表中非11系的學(xué)生信息use test數(shù)據(jù)庫gosel
34、ect *from studentwhere not (st_dpid ='11')go操作6.6:查詢選修了1002號課程且成績在60以下的學(xué)生學(xué)號use test數(shù)據(jù)庫goselect st_idfrom slt_cousewhere cs_id='1002'and score<60go操作6.7:查詢2007年入學(xué)的11系所有男生信息use test數(shù)據(jù)庫goselect *from studentwhere st_dpid='11'and st_sex='男'and datepart(yy,st_date)='
35、;2007'go操作6.8:查詢11系和12系的學(xué)生信息use test數(shù)據(jù)庫goselect *from studentwhere st_dpid='11'or st_dpid='12'go操作6.9:查詢11系和12系所有2007年入學(xué)的學(xué)生信息use test數(shù)據(jù)庫goselect *from studentwhere (st_dpid='11'or st_dpid='12')and datepart(yy,st_date)='2007'go3使用LIKE關(guān)鍵字進(jìn)行模糊查詢操作6.10:查詢所有“計算
36、機(jī)”開頭的課程信息use test數(shù)據(jù)庫goselect *from cousewhere cs_nm like '計算機(jī)%'go操作6.11:查詢所有由三個字組成的“王”姓學(xué)生信息use test數(shù)據(jù)庫goselect *from studentwhere st_nm like '王_'go操作6.12:查詢所有課程名中包含“信息”的課程信息use test數(shù)據(jù)庫goselect *from cousewhere cs_id like '%信息%'go操作6.13:查詢學(xué)生姓名介于王姓到張姓的信息use test數(shù)據(jù)庫goselect *fro
37、m studentwhere st_nm like '王-張%'go4使用BetweenAnd關(guān)鍵字進(jìn)行查詢操作6.14:查詢在1989.7.1到1990.6.30之間出生的學(xué)生信息use test數(shù)據(jù)庫goselect *from studentwhere st_birth between '1989-7-1' and '1990-6-30'go操作6.15:查詢選修了1001號課程且成績在60到80之間的學(xué)生選課信息use test數(shù)據(jù)庫goselect *from slt_cousewhere cs_id='1001'and
38、 score between 60 and 80go5使用IN關(guān)鍵字進(jìn)行查詢操作6.16:查詢11系、12系、13系的學(xué)生信息use test數(shù)據(jù)庫goselect *from studentwhere st_dpid in (11,12,13)go操作6.17:查詢所有張,王,李,趙姓的學(xué)生的學(xué)號、姓名、性別use test數(shù)據(jù)庫goselect st_id ,st_nm ,st_sexfrom studentwhere left(st_nm,1) in ('張','王','李','趙')go6使用NOT NULL關(guān)鍵字進(jìn)行查詢
39、操作6.18:查詢所有生源為非空的學(xué)生信息use test數(shù)據(jù)庫goselect *from studentwhere st_from is not nullgo操作6.19:查詢選修了1001號課程且成績?yōu)榭盏膶W(xué)生選課信息use test數(shù)據(jù)庫goselect *from slt_cousewhere cs_id='1001'and score is nullgo實(shí)驗(yàn)7 數(shù)據(jù)查詢(3)查詢排序與查詢結(jié)果存儲操作7.1:查詢課程信息,按課程名稱降序排序use test數(shù)據(jù)庫goselect *from couseorder by cs_nm descgo操作7.2:查詢選修了1
40、001號課程成績非空的學(xué)生學(xué)號和成績,并按成績降序排序use test數(shù)據(jù)庫goselect st_id ,scorefrom slt_cousewhere cs_id='1001'and score is not nullorder by score descgo操作7.3:查詢11系學(xué)生學(xué)號、姓名和年齡,按年齡升序排序use test數(shù)據(jù)庫goselect st_id ,st_nm ,datepart(yy,getdate()-datepart(yy,st_birth) '年齡'from studentwhere st_dpid='11'or
41、der by 年齡 ascgo操作7.4:查詢學(xué)生信息,按姓名升序排序,再按系號降序排序use test數(shù)據(jù)庫goselect *from studentorder by st_nm asc , st_dpid descgo操作7.5:創(chuàng)建學(xué)生表副本student01,僅保留學(xué)生學(xué)號、姓名和性別use test數(shù)據(jù)庫goselect st_id ,st_nm ,st_sexinto student01from studentgo操作7.6:查詢陜西籍學(xué)生,將結(jié)果保存在新表st_shanxiuse test數(shù)據(jù)庫goselect *into st_shanxifrom studentwhere
42、st_from='陜西省'go操作7.7:查詢選修了1001號課程學(xué)生的選課信息,按學(xué)號升序排序,將結(jié)果保存在新表use test數(shù)據(jù)庫goselect *into slt_couse01from slt_cousewhere cs_id='1001'order by st_id ascgo操作7.8:用局部變量stage保存學(xué)生張三的年齡use test數(shù)據(jù)庫godeclare stage intselect stage = datepart(yy,getdate()-datepart(yy,st_birth)from studentwhere st_nm=&
43、#39;張三'go操作7.9:用局部變量name和stscore保存070101班按學(xué)號排序后最后一個學(xué)生的姓名和入學(xué)成績use test數(shù)據(jù)庫godeclare name nvarchar (8) ,stscore intselect name=st_nm ,stscore=st_scorefrom studentwhere left(st_id,6)='070101' order by st_id descgo 實(shí)驗(yàn)8 數(shù)據(jù)查詢(4)查詢統(tǒng)計與匯總操作8.1:查詢課程總數(shù)use test數(shù)據(jù)庫goselect count(*)from cousego操作8.2:查詢
44、選修1001號課程的學(xué)生人數(shù)use test數(shù)據(jù)庫goselect count(st_id)from slt_cousewhere cs_id='1001'go操作8.3:查詢被選修課程的數(shù)量use test數(shù)據(jù)庫goselect count(distinct cs_id)from slt_cousego操作8.4:查詢選修070101班學(xué)生的平均入學(xué)成績use test數(shù)據(jù)庫goselect avg(st_score)from studentwhere left(st_id ,6) ='070101'go操作8.5:查詢070101001號學(xué)生選修課程的數(shù)量、
45、總分以及平均分use test數(shù)據(jù)庫goselect count(cs_id) '總量' ,sum(score) '總分' ,avg(score) '平均分'from slt_cousego操作8.6:查詢選修1001號課程的學(xué)生人數(shù)、最高分、最低分和平均分use test數(shù)據(jù)庫goselect count(*) '學(xué)生總數(shù)' ,max(score) '最高分' ,min(score) '最低分'from slt_cousewhere cs_id='1001' go操作8.7:求各個
46、課程號和相應(yīng)的選課人數(shù)use test數(shù)據(jù)庫goselect cs_id ,count(st_id)from slt_cousegroup by cs_idgo操作8.8:統(tǒng)計各班人數(shù)use test數(shù)據(jù)庫goselect left(st_id ,6) '班級' ,count(st_id) '人數(shù)'from studentgroup by left(st_id ,6)go操作8.9:依次按班級、系號對學(xué)生進(jìn)行分類統(tǒng)計人數(shù)、入學(xué)平均分use test數(shù)據(jù)庫goselect (left(st_id,6) '班級' ,(st_dpid) '系級
47、' ,avg(st_score) '入學(xué)平均分' ,count(st_id) '人數(shù)'from studentgroup by left(st_id ,6),st_dpidgo操作8.10:查詢選修了均分在75以上的課程號及均分use test數(shù)據(jù)庫goselect cs_id '課程號' ,avg(score) '平均分'from slt_cousegroup by cs_idhaving avg(score)>75go操作8.11:查詢選修了2門以上課程的學(xué)生學(xué)號use test數(shù)據(jù)庫goselect st_id
48、from slt_cousegroup by st_idhaving count(*)>2go操作8.12:明細(xì)匯總年齡<20的學(xué)生,并匯總學(xué)生數(shù)量、平均年齡use test數(shù)據(jù)庫goselect st_nm,datepart(yy,getdate()-datepart(yy,st_birth) '平均年齡'from studentwhere datepart(yy,getdate()-datepart(yy,st_birth)<20compute count(st_nm), avg(datepart(yy,getdate()-datepart(yy,st_b
49、irth)go操作8.13:按班級明細(xì)匯總成績<85分的學(xué)生,匯總學(xué)生數(shù)、均分use test數(shù)據(jù)庫goselect st_nm ,left(st_id,6) '班級' ,st_score from studentwhere st_score<85order by left(st_id,6)compute count(st_nm),avg(st_score) by left(st_id,6)go 實(shí)驗(yàn)9 數(shù)據(jù)查詢(5)連接查詢操作9.1:用SQL Server形式連接查詢學(xué)生學(xué)號、姓名、性別及其所選課程編號use test數(shù)據(jù)庫goselect stud
50、ent.st_id, st_nm, st_sex, cs_idfrom student , slt_couse where student.st_id =slt_couse.st_idgo操作9.2:用ANSI形式連接查詢學(xué)生學(xué)號、姓名、性別及其所選課程編號use test數(shù)據(jù)庫goselect student.st_id, st_nm, st_sex, cs_idfrom student inner join slt_couse on student.st_id = slt_couse.st_idgo操作9.3:用SQL Server形式連接查詢學(xué)生學(xué)號、姓名及其所選課程名稱及成績use t
51、est數(shù)據(jù)庫goselect student.st_id ,st_nm ,cs_nm ,scorefrom student, couse,slt_cousewhere student.st_id=slt_couse.st_idand couse.cs_id=slt_couse.cs_idgo操作9.4:用ANSI形式連接查詢學(xué)生學(xué)號、姓名及其所選課程名稱及成績use test數(shù)據(jù)庫goselect student.st_id ,st_nm ,cs_nm ,scorefrom slt_couse inner join student on student.st_id=slt_couse.st_i
52、dinner join couse on slt_couse.cs_id=couse.cs_idgo操作9.5:查詢選修了1002課程的學(xué)生學(xué)號、姓名及1001課程成績use test數(shù)據(jù)庫goselect student.st_id ,st_nm ,scorefrom student ,slt_cousewhere student.st_id=slt_couse.st_idand cs_id='1002'and cs_id='1001'go操作9.6:查詢選修了“數(shù)據(jù)結(jié)構(gòu)”課程的學(xué)生學(xué)號、姓名及課程成績use test數(shù)據(jù)庫goselect student.s
53、t_id,st_nm,scorefrom student,slt_couse,cousewhere student.st_id=slt_couse.st_idand couse.cs_id=slt_couse.cs_idand cs_nm='數(shù)據(jù)結(jié)構(gòu)'go操作9.7:用左外連接查詢沒有選修任何課程的學(xué)生學(xué)號、姓名use test數(shù)據(jù)庫goselect student.st_id ,st_nm ,scorefrom student left join slt_couseon student.st_id=slt_couse.st_idwhere score is null go操作
54、9.8:用右外連接查詢選修各個課程的學(xué)生學(xué)號use test數(shù)據(jù)庫goselect slt_couse.st_id ,couse.cs_idfrom slt_couse right join couseon couse.cs_id=slt_couse.cs_idgo實(shí)驗(yàn)10 數(shù)據(jù)查詢(6)子查詢操作10.1:用子查詢對各班人數(shù)進(jìn)行查詢(新增列)use test數(shù)據(jù)庫goselect distinct left(st_id ,6) '班級','人數(shù)'=(select count(st_id) from student bwhere left(student.st_
55、id,6)= left(b.st_id,6)from studentgo操作10.2:用子查詢對各課程的選課人數(shù)進(jìn)行查詢(新增列)use test數(shù)據(jù)庫goselect distinct slt_couse.cs_id ,'人數(shù)'=(select count(cs_id) from slt_couse bwhere slt_couse.cs_id=b.cs_id)from slt_cousego操作10.3:查詢選修了1002課程成績不及格的學(xué)生的學(xué)號、姓名和性別,并按姓名升序排序通過子查詢實(shí)現(xiàn):使用IN關(guān)鍵字use test數(shù)據(jù)庫goselect st_id,st_nm,st
56、_sexfrom studentwhere st_id in (select st_id from slt_couse where cs_id='1002' and score<60)Order by st_nmgo通過子查詢實(shí)現(xiàn):使用比較運(yùn)算符use test數(shù)據(jù)庫goselect st_id ,st_nm ,st_sexfrom studentwhere (select score from slt_couse where student.st_id=slt_couse.st_id and cs_id='1002')<60order by st_nmgo操作10.4:查詢“東方紅”同學(xué)所在班的學(xué)生信息,并按姓名降序排序通過子查詢實(shí)現(xiàn):IN運(yùn)算符use test數(shù)據(jù)庫goselect *from studentwhere left(st_id,6) in (select left(st_id,6) from stud
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 光伏融資租賃協(xié)議合同范本
- 會員推廣合同范本
- 單位廚房用人合同范例
- 加盟合同范本在
- 產(chǎn)銷合作協(xié)議合同范本
- 水泥買賣的合同范本
- 包工簡易合同范本
- 個人店員合同范本
- 高級包間服務(wù)合同范本
- 中標(biāo)檢測儀器合同范本
- 鋼筋工工藝與實(shí)習(xí)(第二版)課件匯總?cè)珪娮咏贪竿暾嬲n件最全幻燈片(最新)課件電子教案幻燈片
- 煤礦從業(yè)人員考試題庫全答案(word版)
- 洞頂回填技術(shù)交底
- 最簡易的帕累托圖制作方法簡介PPT通用課件
- 城市軌道交通應(yīng)急處理課程標(biāo)準(zhǔn)
- 第18課 罐和壺(一)
- 初二下分式混合計算練習(xí)1(附答案)
- (完整版)振幅調(diào)制與解調(diào)習(xí)題及其解答
- 抗震支架施工安裝合同
- JJG 657-2019 呼出氣體酒精含量檢測儀 檢定規(guī)程(高清版)
- 政法書記在全縣公安工作會議上的講話
評論
0/150
提交評論