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

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、數(shù)據(jù)庫原理及應用崔巍_書后上機實驗程序源代碼:-1CREATEDATABASEtest1ON(NAME=test1_dat,FILENAME=f:DBdatatest1dat.mdf,SIZE= 10,MAXSIZE= 50,FILEGROWTH= 5 )LOGON(NAME=order_log,FILENAME=f:DBdatatest1log.ldf,SIZE= 5MB,MAXSIZE= 25MB,FILEGROWTH= 5MB)-2createdatabasetest2onprimary(name=test2_dat1,filename=f:DBdatatest2dat1.mdf),(n

2、ame=test2_dat2,filename=f:DBdatatest2dat2.ndf),(name=test2_dat3,filename=f:DBdatatest2dat3.ndf)logon(name=test2_log1,filename=f:DBdatatest2log1.ldf),(name=test2_log2,filename=f:DBdatatest2log2.ldf)-3createdatabasetest3onprimary(name=test3_dat1,filename=f:DBdatatest3dat1.mdf),(name=test3_dat2,filenam

3、e=f:DBdatatest3dat2.mdf),filegroupg2(name=test3_dat3,filename=d:DBdatatest3dat3.ndf),(name=test3_dat4,filename=d:DBdatatest3dat4.ndf),filegroupg3(name=test3_dat5,filename=e:DBdatatest3dat5.ndf),(name=test3_dat6,filename=e:DBdatatest3dat6.ndf)logon(name=test3_log,filename=f:DBdatatest3log.ldf)-4alter

4、databasetest1addfile(name=test1new_dat,filename=f:DBdatatest1newdat.ndf,size=5MB)-5alterdatabasetest1modifyfile(name=test1_dat,size=15MB)-6dropdatabasetest3四、實驗數(shù)據(jù)、結果分析:若沒有指定size,則默認為1MB,沒有指定Maxsize,文件可以增長到磁盤滿為止,沒有指定Filegrowth,則默認為10%。五、總結:CREATE DATABASE dataname創(chuàng)建數(shù)據(jù)庫ALTER DATABASE database修改數(shù)據(jù)庫DROP

5、 DATABASE dataname刪除數(shù)據(jù)庫數(shù)據(jù)庫系統(tǒng)及應用 上機實驗報告2一、實驗目的:掌握表和索引的建立方法及表結構的修改方法,并實踐DBMS提供的數(shù)據(jù)完整性功能,加深對數(shù)據(jù)完整性的理解。二、實驗內容:建立表,修改表結構,建立索引,數(shù)據(jù)完整性描述。三.、程序源代碼:createdatabase訂單管理createtable 客戶(客戶號 char(8) primary key check(客戶號 like A-z%),客戶名稱 varchar(40) not null,聯(lián)系人 char(8),地址 varchar(40),郵政編碼 char(6) check(郵政編碼 like 0-90

6、-90-90-90-90-9), char(12) check( like 10-90-90-90-90-90-90-90-90-90-90-9)create table 產品(產品號 char(8) primary key check(產品號 like A-ZA-Z%),產品名稱 varchar(40),規(guī)格說明 char(40) constraint uni unique,單價 smallmoney constraint dj check(單價0)create table訂購單(客戶號 char(8) not null foreign key references客戶,訂單號 char(8

7、) primary key,訂購日期 datetime default getdate()create table訂單名細(訂單號 char(8) foreign key references訂購單,序號 tinyint,產品號 char(8) not null foreign key references產品,數(shù)量 smallitn constraint sl check(數(shù)量0),primarykey(訂單號,序號)-修改產品表-1alter table產品dropconstraint unialter table產品altercolumn規(guī)格說明 varchar(40)alter tab

8、le產品add constraint uni unique(規(guī)格說明)-2alter table訂購單 add 完成日期 datetime default null-3alter table訂單名細 drop constraint slalter table訂單名細 add constraint sl check(數(shù)量0 and數(shù)量1000)-建立索引-1create index cus_idx on客戶(客戶名稱)-2createunique index gdn_nui on產品(產品名稱)-3創(chuàng)建表時,primary key隱式地創(chuàng)建了聚集索引,而一個表中只能有一個聚集索引。create

9、clustered index oid_clus on訂購單(訂單號) -失敗-4create index item_idx on訂單名細(訂單號,序號,數(shù)量 desc)數(shù)據(jù)庫系統(tǒng)及應用 上機實驗報告3一、實驗目的:為實驗2建立的表設計一組數(shù)據(jù)進行插入、刪除、修改等操作,并體會數(shù)據(jù)完整性約束的作用,加深對數(shù)據(jù)完整性及其約束的理解。二、實驗內容:數(shù)據(jù)的插入、更新和刪除。三.、程序源代碼:-1、部分記錄insertinto客戶values(C001,A公司,小明,北京,123456,143567568457)insertinto客戶values(C002,B公司,小李,上海,234567,1463

10、89045634)insertinto客戶(客戶號,客戶名稱)values(C009,J商場)insertinto產品values(GD001,iphone,4s,5000)insertinto產品values(GD002,ipad,2g,5500)insertinto產品(產品號,產品名稱)values(GD010,Mac Pro)insertinto訂購單(客戶號,訂單號)values(C001,or01)insertinto訂購單(客戶號,訂單號)values(C001,or02)insertinto訂購單(訂單號)values(or19)insertinto訂單名細 values(or

11、01,1,GD001,4)insertinto訂單名細 values(or01,2,GD001,3)insertinto訂單名細 values(or01,3,GD002,6)insertinto訂單名細(訂單號,序號,產品號)values(or14,28,GD006)-2deletefrom客戶where客戶號=C001-受參照完整性約束deletefrom訂購單where訂單號=or01-受參照完整性約束-3update訂購單set訂單號=nullwhere客戶號=C001-受實體完整性約束update訂購單set客戶號=C011where訂單號=or04-受參照完整性約束update訂購單

12、set客戶號=C009where訂單號=or07-更新成功update訂單名細set數(shù)量=0 where序號=9-受用戶定義完整性約束-4update訂單名細set數(shù)量=數(shù)量+10from訂購單where訂購單.訂單號=訂單名細.訂單號and客戶號=C002-5deletefrom訂單名細from訂購單where訂購單.訂單號=訂單名細.訂單號and客戶號=C002四、實驗數(shù)據(jù)、結果分析:-2在刪除客戶號為“C002”的記錄時無法刪除,因為客戶表被訂購單表參照。刪除訂購單號為“or01”的記錄時無法刪除,因為訂單名細表參照訂購單表。-3第一個更新根據(jù)實體完整性約束,訂單號是主關鍵字,不能為空值

13、。第二個更新根據(jù)參照完整性約束,訂購單表參照客戶表,而客戶表中沒有客戶號為“C011”的客戶。第四個更新根據(jù)用戶定義完整性約束,數(shù)量必須為正整數(shù)。-4使客戶號為C002的訂購單的訂購數(shù)量增加10.-5刪掉客戶號為C002的訂單名細記錄。五、總結:插入:INSERT INTO (,)values(,)更新:update set =,=from where刪除:DELETE FROM FROM WHERE 完整性約束影響插入、更新和刪除等操作數(shù)據(jù)庫系統(tǒng)及應用 上機實驗報告4一、實驗目的:熟練掌握SQL SELECT語句,能夠運用該語句完成各種查詢。二、實驗內容:用SQL SELECT語句完成各種數(shù)

14、據(jù)查詢。三.、程序源代碼:-1select * from 客戶-2select 客戶號 from 訂購單-3select * from 產品 where 單價=5000-4select * from 產品 where 單價5000 and 產品名稱=Macbook-5select * from 產品 where 單價6000 and 產品名稱 in(Macbook,ipad)-6select c.客戶名稱,c.聯(lián)系人,c. ,o.訂單號from 客戶 c,訂購單 owhere o.訂購日期 between 2011-10-30 and 2011-12-1and c.客戶號=o.客戶號-7sel

15、ect distinct 客戶名稱,聯(lián)系人, from 客戶 c,產品 g,訂購單 o,訂單名細 dwhere 產品名稱=iphoneand g.產品號=d.產品號and d.訂單號=o.訂單號and o.客戶號=c.客戶號-8select * from 訂單名細where 產品號 in(select 產品號from 產品where 產品名稱=Macbook)-9select * from 訂購單where 訂單號 in(select 訂單號from 訂單名細where 數(shù)量10)-10select * from 產品 where 單價 =(select 單價 from 產品 where 規(guī)格

16、說明=4s)-11select * from 產品 where 單價 between 1000 and 5000-12select * from 客戶 where 客戶名稱 like %集團-13select * from 客戶 where 客戶名稱 not like %商場-14select * from 產品 order by 單價-15select *from 產品order by 產品名稱,單價-16select COUNT(產品號)from 產品-17select SUM(數(shù)量)from 訂單名細where 產品號=(select 產品號from 產品where 產品名稱=ipad)

17、-18select SUM(數(shù)量*單價) 總金額from 產品,訂單名細where 產品.產品號=訂單名細.產品號and 產品名稱=ipod nano-19select COUNT(distinct 訂單號)as 訂購單個數(shù),AVG(數(shù)量*單價)as 平均金額from 產品,訂單名細where 產品.產品號=訂單名細.產品號-20select 訂單號,COUNT(訂單號) 項目數(shù),SUM(g.單價*o.數(shù)量) 總金額from 產品 g,訂單名細 owhere g.產品號=o.產品號group by 訂單號-21select i.訂單號,MAX(數(shù)量*單價) 最高金額,MIN(數(shù)量*單價) 最低

18、金額from 訂購單 o,產品 g,訂單名細 iwhere o.訂單號=i.訂單號and g.產品號=i.產品號and 產品名稱=iphonegroup by i.訂單號-22select 訂單號,COUNT(*) 項目數(shù),AVG(數(shù)量*單價) 平均金額from 產品 g,訂單名細 iwhere g.產品號=i.產品號group by 訂單號having COUNT(*)=2-23select 客戶名稱,聯(lián)系人, ,訂單號from 客戶 c,訂購單 owhere c.客戶號=o.客戶號and 訂購日期 is null-24select 客戶名稱,聯(lián)系人, ,訂單號,訂購日期from 客戶,訂購

19、單where 客戶.客戶號=訂購單.客戶號and 訂購日期2011-10-10-25select *from 產品 outawhere 單價=(select MAX(單價)from 產品 innerawhere outa.產品名稱=innera.產品名稱)-26select 客戶號from 客戶where not exists(select *from 訂購單where 客戶.客戶號=訂購單.客戶號)-27select *from 客戶where exists(select *from 訂購單where 客戶.客戶號=訂購單.客戶號)-28select 產品名稱from 產品where 單價=

20、 any(select 單價/2from 產品)-29select 產品名稱from 產品where 單價 all(select max(單價) from 產品)-30-crossselect * from 客戶 cross join 訂購單where 客戶.客戶號=訂購單.客戶號-innerselect * from 客戶 inner join 訂購單on 客戶.客戶號=訂購單.客戶號-leftselect 客戶.*,訂單號,訂購日期from 客戶 left join 訂購單on 客戶.客戶號=訂購單.客戶號-rightselect 客戶.*,訂單號,訂購日期from 客戶 right jo

21、in 訂購單on 客戶.客戶號=訂購單.客戶號-fullselect 客戶.*,訂單號,訂購日期from 客戶 full join 訂購單on 客戶.客戶號=訂購單.客戶號數(shù)據(jù)庫系統(tǒng)及應用 上機實驗報告5一、實驗目的:理解視圖的概念,掌握視圖的使用方法。二、實驗內容:定義視圖,并在視圖上完成查詢、插入、更新和刪除操作。三.、程序源代碼: -1-1)、基于單個表按投影操作定義視圖create view v_cus asselect 客戶號,客戶名稱from 客戶-使用select * from v_cus-2)、基于單個表按選擇操作定義視圖create view v_order asselect

22、 *from 訂購單where 客戶號=C001-使用select * from v_order-3)、基于單個表按選擇和投影操作定義視圖create view v_cuss asselect 客戶名稱,聯(lián)系人, from 客戶where 客戶號=C003-使用select * from v_cuss-4)、基于多個表根據(jù)連接操作定義視圖create view v_join asselect 客戶.*,訂單號,訂購日期from 客戶 join 訂購單on 客戶.客戶號=訂購單.客戶號-使用select * from v_join-5)、基于多個表根據(jù)嵌套查詢定義視圖create view v_item asselect * from 訂單名細where 產品號 in(select 產品號from 產品where 產品名稱=Macbook)-使用select * from v_item-6)、查定義含有虛字段的視圖create view v_items(訂單號,序號,產品號,數(shù)量,總金額) asselect i.*,i.數(shù)量*g.單價from 訂單名細 i,產品 gwhere i.產品號=g.產品號-使用select * from v

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論