數(shù)據(jù)庫系統(tǒng)及應(yīng)用第二版課后上機答案_第1頁
數(shù)據(jù)庫系統(tǒng)及應(yīng)用第二版課后上機答案_第2頁
數(shù)據(jù)庫系統(tǒng)及應(yīng)用第二版課后上機答案_第3頁
數(shù)據(jù)庫系統(tǒng)及應(yīng)用第二版課后上機答案_第4頁
數(shù)據(jù)庫系統(tǒng)及應(yīng)用第二版課后上機答案_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上數(shù)據(jù)庫系統(tǒng)及應(yīng)用第二版課后上機答案實驗章節(jié):第一,二,三,四,五,七實驗一、實驗?zāi)康模菏煜?shù)據(jù)庫的基本操作,會運用sql處理問題二、實驗內(nèi)容:1.建立數(shù)據(jù)庫 ,2.建立表和數(shù)據(jù)完整性,3.SQL數(shù)據(jù)操作,4.SQL數(shù)據(jù)查詢,5視圖的定義和操作,7.存儲過程三.、程序源代碼:實驗一:1. create database test1on(name=test1_dat,filename='d:lydatatest1dat.mdf',size=5MB)log on(name=test1_log,filename='d:lydatatest1log.ld

2、f')2.create database test2onprimary(name=test2_dat1,filename='d:lydatatest2dat1.mdf'),(name=test2_dat2,filename='d:lydatatest2dat2.ndf'),(name=test2_dat3,filename='d:lydatatest2dat3.ndf')log on(name=test2_log1,filename='d:lydatatest2log1.ldf'),(name=test2_log2,fil

3、ename='d:lydatatest2log2.ldf')3. create database test3onprimary(name=test3_dat,filename='d:lydatatest3dat.mdf'),filegroup w1(name=test3_dat1,filename='d:lydatatest3dat1.ndf'),(name=test3_dat2,filename='d:lydatatest3dat2.ndf'),filegroup w2(name=test3_dat3,filename='

4、;e:lydatatest3dat3.ndf'),(name=test3_dat4,filename='e:lydatatest3dat4.ndf'),filegroup w3(name=test3_dat5,filename='f:lydatatest3dat5.ndf'),(name=test3_dat6,filename='f:lydatatest3dat6.ndf')log on(name=test3_log,filename='d:lydatatest3log.ldf')4. alter database tes

5、t1add file(name=new_dat,filename='d:lydatanewdat.ndf',size=5MB)5. alter database test1modify file(name=test1_dat,size=10 MB )6. Drop database test1Drop database test2Drop database test3實驗21建庫:CREATE DATABASE 訂單管理ON ( NAME = order_dat,FILENAME = 'd:lydataorderdat.mdf',SIZE = 10,MAXSIZ

6、E = 50,FILEGROWTH = 5 )LOG ON( NAME = order_log,FILENAME = 'd:lydataorderlog.ldf',SIZE = 5MB,MAXSIZE = 25MB,FILEGROWTH = 5MB ) 建表: 客戶號 char(8) primary key check(客戶號 like 'A-z%'), 客戶名稱 varchar(40) not null, 聯(lián)系人 char(8), 地址 varchar(40), 郵政編碼 char(6) check(郵政編碼 like '0-90-90-90-90-9

7、0-9'), 電話 char(12) check(電話 like '0-90-90-90-90-90-90-90-90-90-90-90-9') create table 產(chǎn)品(產(chǎn)品號 char(8) primary key check(產(chǎn)品號 like 'A-zA-z%'),產(chǎn)品名稱 varchar(40),規(guī)格說明 char(40) constraint uni unique,單價 smallmoney constraint dj check(單價>0)create table 訂購單(客戶號 char(8) not null foreign

8、key references 客戶,訂單號 char(8) primary key,訂購日期 datetime default getdate()create table 訂單名細(訂單號 char(8) foreign key references 訂購單,序號 tinyint,產(chǎn)品號 char(8) not null foreign key references 產(chǎn)品,數(shù)量 smallint constraint sl check(數(shù)量>0)primary key(訂單號,序號)2.1、先取消唯一性約束:alter table 產(chǎn)品 drop constraint unialter

9、table 產(chǎn)品 alter column 規(guī)格說明 varchar(40) 2.2 alter table 訂購單 add 完成日期 datetime null2.3 先取消約束 alter table 訂單名細 drop constraint num;ALTER TABLE 訂單名細 ADD CONSTRAINT num CHECK (數(shù)量 >= 0 AND 數(shù)量 <= 1000)alter table 訂單名細 drop constraint num3.1 create index sup_kh_idx on 客戶(客戶名稱)3.2 create unique index c

10、p_idx on 產(chǎn)品(產(chǎn)品名稱)3.3由于create table命令中 的primary key 約束將隱式創(chuàng)建聚集索引,且在創(chuàng)建表時已經(jīng)指定了關(guān)鍵字,則不可以再創(chuàng)建聚集索引3.4create index dd_mx_idx on 訂單名細(訂單號,序號,數(shù)量 desc)四、實驗數(shù)據(jù)、結(jié)果分析:實驗3客戶表:訂購單:訂單名細:產(chǎn)品:1、insert into 訂單名細 values( 'dd16','32','cp56','150')insert 客戶(客戶號,客戶名稱 )values ('E20','

11、廣西電子')訂購單備份:select* into 訂購單備份 from 訂購單select * from 訂購單備份2、delete from 客戶 where 客戶號='E10'delete from 客戶 where 客戶號='E10'3、update 訂單名細set 數(shù)量=225where 訂單號='dd13'4、update 訂購單set 訂購日期='2011-10-11'where 訂單號 in (select 訂單號 from 訂單名細 where 產(chǎn)品號 in (select 產(chǎn)品號 from 產(chǎn)品 wher

12、e 產(chǎn)品名稱='MP4')5delete from 訂購單where 客戶號 in( select 客戶號 from 客戶 where 客戶名稱='華中電子廠')由于語句與 REFERENCE 約束"FK_訂單名細_訂單號_145C0A3F"沖突。該沖突發(fā)生于數(shù)據(jù)庫"訂單管理",表"dbo.訂單名細", column '訂單號'。語句已終止。實驗41、select * from 客戶2 .select distinct 客戶號 from 訂購單 where 訂單號 is not null

13、3、Select * from 產(chǎn)品 where 單價>5004Select * from 產(chǎn)品 where 單價>200 and 產(chǎn)品名稱='MP4'5、select * from 產(chǎn)品 where (單價>500)and (產(chǎn)品名稱='MP7' or 產(chǎn)品名稱='HTC手機')6、select 客戶名稱 ,聯(lián)系人 ,電話 ,訂單號from 訂購單 ,客戶where 訂購單.客戶號=客戶.客戶號 and 訂購日期='2011-10-11 00:00:00.000'7、select 客戶名稱,聯(lián)系人,電話from

14、 客戶,訂購單,訂單名細,產(chǎn)品where (客戶.客戶號=訂購單.客戶號 and 訂購單.訂單號=訂單名細.訂單號 and 訂單名細.產(chǎn)品號=產(chǎn)品.產(chǎn)品號) and 產(chǎn)品名稱='MP4'8、select * from 訂單名細 where 產(chǎn)品號 in (select 產(chǎn)品號 from 產(chǎn)品 where 產(chǎn)品名稱='MP3')9、select * from 訂購單where 訂單號 in (select 訂單號 from 訂單名細 where 數(shù)量>200)10、select * from 產(chǎn)品 where 單價=(select 單價 from 產(chǎn)品 wh

15、ere 產(chǎn)品名稱='HTC手機')11、 select* from 產(chǎn)品 where 單價 between 200 and 350 12、 select * from 客戶 where 客戶名稱 like'%公司'13、 select * from 客戶 where 客戶名稱 not like'%電子廠'14、 select * from 產(chǎn)品 order by 單價15、 select * from 產(chǎn)品 order by 產(chǎn)品名稱,單價16、 select COUNT (distinct 產(chǎn)品名稱) from 產(chǎn)品17. select sum

16、 (數(shù)量) from 訂單名細 where 產(chǎn)品號 in(select 產(chǎn)品號 from 產(chǎn)品 where 產(chǎn)品名稱='MP4')18、 select SUM (數(shù)量*單價) from 訂單名細,產(chǎn)品 where 訂單名細.產(chǎn)品號 =產(chǎn)品.產(chǎn)品號 and 產(chǎn)品名稱='MP4'19、 select count(訂單號),avg(數(shù)量*單價)from 產(chǎn)品,訂單名細 where 訂單名細.產(chǎn)品號 =產(chǎn)品.產(chǎn)品號20. SELECT 訂單號,COUNT(訂單名細.產(chǎn)品號) AS 項目數(shù),SUM( 產(chǎn)品.單價 * 訂單名細.數(shù)量 ) AS 總金額 FROM 訂單名細,

17、產(chǎn)品 WHERe 訂單名細.產(chǎn)品號 = 產(chǎn)品.產(chǎn)品號 GROUP BY 訂單號21、select 客戶.客戶號,產(chǎn)品.產(chǎn)品號,數(shù)量*單價 as 總金額from 客戶,訂購單,訂單名細,產(chǎn)品where 客戶.客戶號=訂購單.客戶號 and 訂購單.訂單號=訂單名細.訂單號 and 訂單名細.產(chǎn)品號=產(chǎn)品.產(chǎn)品號 and 產(chǎn)品名稱='MP3'order by 客戶號 compute max(數(shù)量*單價),min (數(shù)量*單價) by 客戶號22、select 訂單號,avg(數(shù)量*單價)as 平均金額,count(產(chǎn)品.產(chǎn)品號)from 訂單名細,產(chǎn)品where 訂單名細.產(chǎn)品號=

18、產(chǎn)品.產(chǎn)品號group by 訂單號 having count(序號)>=223 select 客戶名稱,聯(lián)系人,電話,訂購單.訂單號 from 客戶, 訂單名細,訂購單where(客戶.客戶號= 訂購單.客戶號 ) and 訂購日期 is null24. select 客戶名稱,聯(lián)系人,電話,訂單號,訂購日期from 客戶,訂購單where 客戶.客戶號=訂購單.客戶號 and 訂購日期>'2011-10-1'25. select outa.產(chǎn)品號,outa.產(chǎn)品名稱, outa.規(guī)格說明, outa.單價from 產(chǎn)品 outawhere 單價= (SELECT

19、 max(單價) FROM 產(chǎn)品 innera WHERE outa.規(guī)格說明= innera.規(guī)格說明)26. select * from 客戶 where not exists ( select* from 訂購單 where 客戶號=訂購單.客戶號 ) 27. select * from 客戶 where exists ( select* from 訂購單 where 客戶號=訂購單.客戶號 ) 28.select * from 產(chǎn)品 where(單價>any(select 單價/2 from 產(chǎn)品 where 產(chǎn)品名稱='HTC手機')29.select * fr

20、om 產(chǎn)品 where(單價>all(select 單價 from 產(chǎn)品 where 產(chǎn)品名稱='HTC手機')30.1. select *from 客戶 cross join 訂購單 where 客戶.客戶號= 訂購單.客戶號30.2 select *from 客戶 inner join 訂購單 on 客戶.客戶號= 訂購單.客戶號30.3 select 客戶.客戶號,客戶名稱, 聯(lián)系人,地址,郵政編碼,電話 ,訂單號,訂購日期 from 客戶 left join 訂購單 on 客戶.客戶號= 訂購單.客戶號30.4 select 客戶.客戶號,客戶名稱, 聯(lián)系人,地址

21、,郵政編碼,電話 ,訂單號,訂購日期 from 客戶 right join 訂購單 on 客戶.客戶號= 訂購單.客戶號30.5 select 客戶.客戶號,客戶名稱, 聯(lián)系人,地址,郵政編碼,電話 ,訂單號,訂購日期 from 客戶 full join 訂購單 on 客戶.客戶號= 訂購單.客戶號實驗51.create view k_hu as select * from 客戶 where 客戶名稱='東方電子廠'查詢:select * from k_hu2.create view ke_h as select 客戶號,客戶名稱 from 客戶查詢: select * fro

22、m ke_h3.create view ke_hu as select 聯(lián)系人,電話 from 客戶 where 客戶名稱='東方電子廠'查詢: select * from ke_hu.4.給出地址在廣州有訂購產(chǎn)品MP4的客戶名稱create view k_kehu as select 客戶名稱 from 客戶,訂購單,訂單名細,產(chǎn)品 where 地址='廣州' and 客戶.客戶號=訂購單.客戶號 and 訂購單.訂單號=訂單名細.訂單號 and 訂單名細.產(chǎn)品號=產(chǎn)品.產(chǎn)品號 and 產(chǎn)品名稱='MP4'查詢 :select * from k

23、_kehu5 確定哪些客戶名稱目前沒有訂購單的視圖create view dd_gg as select * from 客戶 where not exists ( select* from 訂購單 where 客戶號=訂購單.客戶號 ) 查詢: select * from dd_gg6,定義視圖 包含訂單號,產(chǎn)品號,數(shù)量訂單總金額create view d_ming (訂單號,產(chǎn)品號,數(shù)量,總金額)as select 訂單號,訂單名細.產(chǎn)品號,數(shù)量,單價*數(shù)量 from 訂單名細,產(chǎn)品 where 訂單名細.產(chǎn)品號=產(chǎn)品.產(chǎn)品號 查詢:select *from d_ming2.1 刪除視圖 d

24、rop view <視圖名> 例如 drop view dd_gg插入:對第五個視圖進行插入insert  dd_gg values ('E33','南華電子','李然','南寧','','2') 第六個視圖插入 insert d_ming values ('dd23','cp33','160','30000')更新:更新第一個視圖:update k_hu set 客戶名稱='東方電子公司' where 客戶號='E12'實驗7 CREATE PROCedur

溫馨提示

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

最新文檔

評論

0/150

提交評論