數(shù)據(jù)庫原理及應(yīng)用-崔巍書后上機實驗_第1頁
數(shù)據(jù)庫原理及應(yīng)用-崔巍書后上機實驗_第2頁
數(shù)據(jù)庫原理及應(yīng)用-崔巍書后上機實驗_第3頁
數(shù)據(jù)庫原理及應(yīng)用-崔巍書后上機實驗_第4頁
數(shù)據(jù)庫原理及應(yīng)用-崔巍書后上機實驗_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

數(shù) 據(jù) 庫 上 機 實 驗 報 告 1一、實驗?zāi)康模豪斫釹QLServer數(shù)據(jù)庫的存儲結(jié)構(gòu),掌握SQLServer數(shù)據(jù)庫的建立方法和維護方法。二、實驗內(nèi)容:在 SQLServer環(huán)境下建立數(shù)據(jù)庫和維護數(shù)據(jù)庫。三、程序源代碼:--1CREATEDATABASEtest1ON(NAME=test1_dat,FILENAME='f:\DB\data\test1dat.mdf',SIZE=10,MAXSIZE=50,FILEGROWTH=5)LOGON(NAME=order_log,FILENAME='f:\DB\data\test1log.ldf',SIZE=5MB,MAXSIZE=25MB,FILEGROWTH=5MB)--2createdatabasetest2onprimary(name=test2_dat1,filename='f:\DB\data\test2dat1.mdf'),(name=test2_dat2,filename='f:\DB\data\test2dat2.ndf'),(name=test2_dat3,filename='f:\DB\data\test2dat3.ndf')logon(name=test2_log1,filename='f:\DB\data\test2log1.ldf'),(name=test2_log2,filename='f:\DB\data\test2log2.ldf')--3createdatabasetest3onprimary(name=test3_dat1,filename='f:\DB\data\test3dat1.mdf'),(name=test3_dat2,filename='f:\DB\data\test3dat2.mdf'),filegroupg2(name=test3_dat3,filename='d:\DB\data\test3dat3.ndf'),(name=test3_dat4,filename='d:\DB\data\test3dat4.ndf'),filegroupg3(name=test3_dat5,filename='e:\DB\data\test3dat5.ndf'),(name=test3_dat6,filename='e:\DB\data\test3dat6.ndf')logon(name=test3_log,filename='f:\DB\data\test3log.ldf')--4alterdatabasetest1addfile(name=test1new_dat,filename='f:\DB\data\test1newdat.ndf',size=5MB)--5alterdatabasetest1modifyfile(name=test1_dat,size=15MB)--6dropdatabasetest3四、實驗數(shù)據(jù)、結(jié)果分析:若沒有指定size,則默認為1MB,沒有指定Maxsize,文件可以增長到磁盤滿為止,沒有指定Filegrowth,則默認為10%。五、總結(jié):CREATEDATABASEdataname創(chuàng)建數(shù)據(jù)庫ALTERDATABASEdatabase修改數(shù)據(jù)庫DROPDATABASEdataname刪除數(shù)據(jù)庫數(shù)據(jù)庫系統(tǒng)及應(yīng)用 上機實驗報告2一、實驗?zāi)康模赫莆毡砗退饕慕⒎椒氨斫Y(jié)構(gòu)的修改方法,并實踐DBMS提供的數(shù)據(jù)完整性功能,加深對數(shù)據(jù)完整性的理解。二、實驗內(nèi)容:建立表,修改表結(jié)構(gòu),建立索引,數(shù)據(jù)完整性描述。三.、程序源代碼:createdatabase 訂單管理createtable

客戶(客戶號char(8)primarykeycheck(客戶名稱varchar(40)notnull,聯(lián)系人char(8),地址varchar(40),郵政編碼char(6)check( 郵政編碼

客戶號like'[A-z]%'),like'[0-9][0-9][0-9][0-9][0-9][0-9]'),電話char(12)check(電話like'1[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'))createtable產(chǎn)品(產(chǎn)品號char(8)primarykeycheck(產(chǎn)品號like'[A-Z][A-Z]%'),產(chǎn)品名稱varchar(40),規(guī)格說明char(40)constraintuniunique,單價smallmoneyconstraintdjcheck(單價>0))createtable訂購單(客戶號char(8)notnullforeignkeyreferences客戶,訂單號char(8)primarykey,訂購日期datetimedefaultgetdate())createtable訂單名細(訂單號char(8)foreignkeyreferences訂購單,序號tinyint,產(chǎn)品號char(8)notnullforeignkeyreferences產(chǎn)品,數(shù)量smallintconstraintslcheck(數(shù)量>0),primarykey(訂單號,序號))修改產(chǎn)品表--1altertable

產(chǎn)品

dropconstraintunialtertablealtertable

產(chǎn)品產(chǎn)品

altercolumn 規(guī)格說明varchar(40)addconstraintuniunique( 規(guī)格說明)--2altertable

訂購單

add

完成日期

datetimedefaultnull--3altertable

訂單名細

dropconstraintslaltertable

訂單名細

addconstraintslcheck(

數(shù)量>0and

數(shù)量<1000)建立索引--1createindexcus_idxon 客戶(客戶名稱)--2createuniqueindexgdn_nuion 產(chǎn)品(產(chǎn)品名稱)--3創(chuàng)建表時,primarykey 隱式地創(chuàng)建了聚集索引,而一個表中只能有一個聚集索引。createclusteredindexoid_cluson 訂購單(訂單號)-- 失敗--4createindexitem_idxon 訂單名細(訂單號,序號,數(shù)量desc)數(shù)據(jù)庫系統(tǒng)及應(yīng)用 上機實驗報告3一、實驗?zāi)康模簽閷嶒?建立的表設(shè)計一組數(shù)據(jù)進行插入、刪除、修改等操作,并體會數(shù)據(jù)完整性約束的作用,加深對數(shù)據(jù)完整性及其約束的理解。二、實驗內(nèi)容:數(shù)據(jù)的插入、更新和刪除。.、程序源代碼:--1、部分記錄insertinto 客戶(客戶號,客戶名稱)values('C009','Jinsertinto 產(chǎn)品values('GD001','iphone','4s','5000')insertinto 產(chǎn)品values('GD002','ipad','2g','5500')

商場')insertinto產(chǎn)品(產(chǎn)品號,產(chǎn)品名稱)values('GD010','MacPro')insertinto訂購單(客戶號,訂單號)values('C001','or01')insertinto訂購單(客戶號,訂單號)values('C001','or02')insertinto訂購單(訂單號)values('or19')insertinto訂單名細values('or01','1','GD001','4')insertinto訂單名細values('or01','2','GD001','3')insertinto訂單名細values('or01','3','GD002','6')insertinto訂單名細(訂單號,序號,產(chǎn)品號)values('or14','28','GD006')--2deletefrom客戶where客戶號='C001'--受參照完整性約束deletefrom訂購單where訂單號='or01'--受參照完整性約束--3update訂購單set訂單號=nullwhere客戶號='C001'-- 受實體完整性約束update訂購單set客戶號='C011'where訂單號='or04'-- 受參照完整性約束update訂購單set客戶號='C009'where訂單號='or07'-- 更新成功update訂單名細set數(shù)量=0where序號='9'-- 受用戶定義完整性約束--4update訂單名細set數(shù)量=數(shù)量+10from訂購單where訂購單.訂單號=訂單名細.訂單號and客戶號='C002'--5deletefrom 訂單名細from訂購單where訂購單.訂單號=訂單名細.訂單號and客戶號='C002'四、實驗數(shù)據(jù)、結(jié)果分析:--2在刪除客戶號為“C002”的記錄時無法刪除,因為客戶表被訂購單表參照。刪除訂購單號為“or01”的記錄時無法刪除,因為訂單名細表參照訂購單表。--3第一個更新根據(jù)實體完整性約束,訂單號是主關(guān)鍵字,不能為空值。第二個更新根據(jù)參照完整性約束,訂購單表參照客戶表,而客戶表中沒有客戶號為“C011”的客戶。第四個更新根據(jù)用戶定義完整性約束,數(shù)量必須為正整數(shù)。--4使客戶號為C002的訂購單的訂購數(shù)量增加 10.--5刪掉客戶號為C002的訂單名細記錄。五、總結(jié):插入:INSERTINTO<表名>[(<列名>[,<列名>?])]values(<表達式>[,<表達式>?])更新:update<表名>set<列名>=<表達式>[,<列名>=<表達式>?][[from<表名>]where<邏輯表達式>]刪除:DELETEFROM表<名>[[FROM<表名>]WHERE邏<輯表達式>]完整性約束影響插入、更新和刪除等操作數(shù)據(jù)庫系統(tǒng)及應(yīng)用 上機實驗報告4一、實驗?zāi)康模菏炀氄莆?SQLSELECT語句,能夠運用該語句完成各種查詢。二、實驗內(nèi)容:用 SQLSELECT語句完成各種數(shù)據(jù)查詢。三.、程序源代碼:--1select*from客戶--2select客戶號from訂購單--3select*from產(chǎn)品where單價>=5000--4select*from產(chǎn)品where單價>5000and產(chǎn)品名稱='Macbook'--5select*from產(chǎn)品where單價>6000and產(chǎn)品名稱in('Macbook','ipad')--6selectc.客戶名稱,c.聯(lián)系人,c.電話,o.訂單號from客戶c,訂購單owhereo.訂購日期between'2011-10-30'and'2011-12-1'andc.客戶號=o.客戶號--7selectdistinct客戶名稱,聯(lián)系人,電話from客戶c,產(chǎn)品g,訂購單o,訂單名細dwhere產(chǎn)品名稱='iphone'andg.產(chǎn)品號=d.產(chǎn)品號andd.訂單號=o.訂單號ando.客戶號=c.客戶號--8select*from訂單名細where產(chǎn)品號in(select產(chǎn)品號from產(chǎn)品where產(chǎn)品名稱='Macbook')--9select*from訂購單where訂單號in(select訂單號from訂單名細where數(shù)量>10)--10select*from產(chǎn)品where單價=(select單價from產(chǎn)品where規(guī)格說明='4s')--11select*from產(chǎn)品where單價between1000and5000--12select*from客戶where客戶名稱like'%集團'--13select*from 客戶where 客戶名稱--14select*from 產(chǎn)品orderby 單價--15select*from產(chǎn)品

notlike'%

商場'orderby 產(chǎn)品名稱,單價--16selectCOUNT(產(chǎn)品號)from產(chǎn)品--17selectSUM(數(shù)量)from訂單名細where產(chǎn)品號=(select 產(chǎn)品號from產(chǎn)品where產(chǎn)品名稱

='ipad')--18selectSUM(數(shù)量*單價)總金額from產(chǎn)品,訂單名細where產(chǎn)品.產(chǎn)品號=訂單名細.產(chǎn)品號and產(chǎn)品名稱='ipodnano'--19selectCOUNT(distinct訂單號)as訂購單個數(shù),AVG(數(shù)量*單價)as平均金額from產(chǎn)品,訂單名細where產(chǎn)品.產(chǎn)品號=訂單名細.產(chǎn)品號--20select訂單號,COUNT(訂單號)項目數(shù),SUM(g.單價*o.數(shù)量)總金額from產(chǎn)品g,訂單名細owhereg.產(chǎn)品號=o.產(chǎn)品號groupby 訂單號--21selecti. 訂單號,MAX(數(shù)量*單價)最高金額,MIN(數(shù)量*單價) 最低金額from訂購單o,產(chǎn)品g,訂單名細iwhereo.訂單號=i.訂單號andg.產(chǎn)品號=i.產(chǎn)品號and產(chǎn)品名稱='iphone'groupbyi. 訂單號--22select 訂單號,COUNT(*)項目數(shù),AVG(數(shù)量*單價)平均金額from產(chǎn)品g,訂單名細iwhereg.產(chǎn)品號=i.產(chǎn)品號groupby 訂單號havingCOUNT(*)>=2--23select 客戶名稱,聯(lián)系人,電話,訂單號from客戶c,訂購單owherec.客戶號=o.客戶號and訂購日期isnull--24select客戶名稱,聯(lián)系人,電話,訂單號,訂購日期from客戶,訂購單where客戶.客戶號=訂購單.客戶號and訂購日期>'2011-10-10'--25select*from產(chǎn)品outawhere單價=(selectMAX(單價)from產(chǎn)品innerawhereouta.產(chǎn)品名稱=innera.產(chǎn)品名稱)--26select客戶號from客戶wherenotexists(select*from訂購單where客戶.客戶號=訂購單.客戶號)--27select*from客戶whereexists(select*from訂購單where客戶.客戶號=訂購單.客戶號)--28select產(chǎn)品名稱from產(chǎn)品where單價=any(select單價/2from產(chǎn)品)--29select產(chǎn)品名稱from產(chǎn)品where單價>all(selectmax(單價)from產(chǎn)品)--30--crossselect*from客戶crossjoin訂購單where客戶.客戶號=訂購單.客戶號--innerselect*from客戶innerjoin訂購單on客戶.客戶號=訂購單.客戶號--leftselect客戶.*,訂單號,訂購日期from客戶leftjoin訂購單on客戶.客戶號=訂購單.客戶號--rightselect 客戶.*,訂單號,訂購日期from客戶rightjoin 訂購單on客戶.客戶號=訂購單.客戶號--fullselect 客戶.*,訂單號,訂購日期from客戶fulljoin 訂購單on客戶.客戶號=訂購單.客戶號數(shù)據(jù)庫系統(tǒng)及應(yīng)用 上機實驗報告5一、實驗?zāi)康模豪斫庖晥D的概念,掌握視圖的使用方法。二、實驗內(nèi)容:定義視圖,并在視圖上完成查詢、插入、更新和刪除操作。.、程序源代碼:--1--1)、基于單個表按投影操作定義視圖createviewv_cusasselect客戶號,客戶名稱from客戶--使用select*fromv_cus--2)、基于單個表按選擇操作定義視圖createviewv_orderasselect*from訂購單where客戶號='C001'使用select*fromv_order--3)、基于單個表按選擇和投影操作定義視圖createviewv_cussasselect 客戶名稱,聯(lián)系人,電話from客戶where客戶號='C003'使用select*fromv_cuss--4)、基于多個表根據(jù)連接操作定義視圖createviewv_joinasselect 客戶.*,訂單號,訂購日期from客戶join 訂購單on客戶.客戶號=訂購單.客戶號使用select*fromv_join--5)、基于多個表根據(jù)嵌套查詢定義視圖createviewv_itemasselect*from 訂單名細where產(chǎn)品號in(select 產(chǎn)品號from產(chǎn)品where產(chǎn)品名稱='Macbook')--使用select*fromv_item--6)、查定義含有虛字段的視圖createviewv_items( 訂單號,序號,產(chǎn)品號,數(shù)量,總金額)asselecti.*,i. 數(shù)量*g.單價from訂單名細i, 產(chǎn)品gwherei. 產(chǎn)品號=g.產(chǎn)品號--使用select*fromv_items--2--在視圖上查詢select*fromv_items--在視圖上插入insertintov_cusvalues('C012','Google')--在視圖上更新updatev_orderset 訂購日期='2011-11-29'--在視圖上刪除deletefromv_cusw

溫馨提示

  • 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論