第一篇:SQL實驗報告
實驗四觸發(fā)器實驗
(一)
after
觸發(fā)器
(1 1)
在l l i neitem 表上定義一個 after 觸發(fā)器, , 當(dāng)修改列項目e e x tendedprice d i scount
x tax 時, , 要把 s orders 表得to o tal pri ce e 一起修改, , 以保證數(shù)據(jù)一致性
C RE ATE T RIGGER
trig _line ite m_ pr ice_ update on line it em fo r upda te
a as
begin i f(UPDATE(ex tend edprice)
o r UPDATE(tax)
or UPD AT E(di scou nt))begin
-— 聲明游標(biāo)變量指向 inserted 表
d eclare
cursor_inserted c urs or
rea d_only
o for select order key,linenu mber,exte nd edpr ice, dis coun t, tax
from
in ser ted
—-息信找查取獲量變明聲? 聲明變量獲取查找信息
de clare order key in t, @linenumb er
int,exte nd edprice
real,dis scount real,tax real
—-打開游標(biāo) epo? en cursor_i ns ert ed
—-標(biāo)游取讀? 讀取游標(biāo)
fe tch
next
from cur sor _i ns erte d int o @o rderkey, @lin enumber,e ext ende dprice, @di scount,t ax
w whi le FETC H_S TATUS =0 nigeb? n
? —-聲明一個變量保存重新計算得新價格 ced?? ecl are @n ew_tota lpri ce
r eal
? select @n ew_t otal price= @ext en dedpr ice *(1 -@di scou nt)*(1 +@tax)
—-用新得總價格變量更新 orders 表得 t ot alprice pu?? update orde rs
se t tot alpri ce= new_totalprice where or derkey=orde rkey
en hctef? ext f ro m cur sor_i nser ted int o @order ke y, @li nenum ber, @ex tende dp rice,discoun t, @tax
dne?llaed? locate c ur sor_i nser te d e end end ?(2)在 在 linei tem表上定義一個 aftr er 觸發(fā)器, , 當(dāng)增加一項訂單明細(xì)時, ,自動修改 s orders 表得 total p rice, 以保證數(shù)據(jù)一致性
CREATE TR IGGER tri g_ lineit em_price_ in sert on l ine item f for inse rt
a s begin ——向指量變標(biāo)游明聲? 聲明游標(biāo)變量指向ins erted 表
de clare
c ursor_inse rted
cursor
read_ onl y
? for s ele ct orderkey,linenumbe r,ex te ndedp ric e,di scou nt,t ax
from ins serte d
--聲明變量獲取查找信息 ed? eclare @orde rk ey
int, @lin enumber int,e xten dedp rice
real, @discount
real, @ta x real -—? - 打開游標(biāo)
open cursor_i ns erte d --? - 讀取游標(biāo)
fe tc h
nex t
f rom
cu rsor_ins erted
into
@o rd erkey,li nen umbe er r, ex ten ded pric e,dis count,tax ihw? ile @@FE TCH_ STATU S=0 eb? egin
-—格價新得算計新重存保量變個一明聲?? 聲明一個變量保存重新計算得新價格
ced? clare @n ew_tot alprice real
celes? ct new _to ta lprice= @extend edp rice *(1-d is count)*(1+tax)—-新更量變格價總得新用?? 用新得總價格變量更新orders 表得totalpri ce
? u pda te
or ders s et
t ota lpric e=total pric e+ @new_ tota lpr ice w wh he re o rderke y=orderkey
tef? etch next from cursor_ ins erte d into o rder key, @l inen umber,e xtended dp ric e, @disc ou nt, @t ax
e end aed? deall oca te cu rsor_in serted en d
(3)
在l in e ite m表上定義一個a ft er觸發(fā)器, , 當(dāng)刪除一項訂單明細(xì)記錄時, ,自動修改 orders 表得 tot a lprice, 以保證數(shù)據(jù)一致性
CREATE TRI GG ER trig _line item_price_de let e
on line item fo r de let e
A AS begin
--聲明游標(biāo)變量指向delet ed 表
de clar e curso r_d eleted cursor
re ad _on ly
es
rof?? ele ct ord erk ey,line numbe r,extende dp rice,discoun t, tax
from
del eted -? -- 聲明變量獲取查找信息
declare ord erkey int,linenum be r
int,extendedp ri ce r eal,discou nt real,ta x real -—? -
打開游標(biāo) epo? en c urso r_ deleted
——標(biāo)游取讀? 讀取游標(biāo) ef? etch next fr om cur sor_delete d in to
order ke y,l inenumb er,ext end dedprice, @di scoun t,tax
wh ile @FETCH_STATUS=0
begi n
-— 聲明一個變量保存重新計算得新價格
? declare @ne w_ to talpric e real
? s selec t @new_t otalpr ice= @exte nde dpri ce*(1-@disco unt)*(1+ tax)
-? -— 用新得總價格變量更新orders 表得tot alp rice u?? upd at(yī)e
orders set t otalpri ce=to talpr ice-@new _totalp rice wh er e order key= @ord erkey
fetch
n ext f rom cursor_inse rt ed in to orderkey, @line numbe r,extende ed dprice, @d iscou nt,t ax dne? nd
d deal lo cat e cur sor_inse rted e end((4 4))驗證 up d at e觸發(fā)器
— -查瞧 號訂單得 to ta a l pr i ce
selec t
*
fro o m
o o r ders where
orde r key=1 8 30;
— -查瞧明細(xì)表得相關(guān)信息
se l ect *
f ro m
lin ei i te e m
w here or de e r key=183 0
and l ine num m be e r =1;
—— 驗證 e update 觸發(fā)器
updat e
lineitem set t ax=tax+0、05
whe re orderkey=1830;
(二)
i i n stead
of
觸發(fā)器
((1))
在 在 lineit em 表上定義一個ins tead o f upda te觸 發(fā)器, 當(dāng)修改明細(xì)表中得數(shù)量 量quan ti ty 時, 應(yīng)先檢查供應(yīng)表par tsupp 得av ai lqty 就是否足夠, 不足夠則拒絕執(zhí)行, 否則執(zhí)行并修改相應(yīng)數(shù)值以保證數(shù)據(jù)一致性 于 由于 in stead
of 觸發(fā)器更新某個表會使得該表上其她不滿足更新列不能更新,因 因用 此逆向思維使用 a fter 觸發(fā)器實現(xiàn)相同效果 即先更新 qu antity, 再比較av ailq qt ty, 如果滿足更新數(shù)量, 就修改partsupp 得 表得 a vailqty, 如果不滿足, 則把lineitem得quantity 更新之后得數(shù)據(jù)重新修改回來 c create trigge r trig_lin eit em_quanti ty_ upda te
on
li neit em f or upda te
as begin if UPDATE(qu ant ity)b begin ——向指別分量變標(biāo)游明聲? 聲明游標(biāo)變量分別指向 i nserted 表與 d el eted 表 ed? declare c urso r_inser ted
cur sor
r ead_on ly
for
tceles?? t
orde rkey, partk ey,s uppkey,lin enum ber, quantit y fr om i nserted
decl are
cursor_de leted c urs or
rea d_ onl y
f or select quantity
fr om deleted
-—息信找查取獲量變明聲? 聲明變量獲取查找信息
decl are
@qu an tity _dif f_lineitem i nt,q uanti ty_pa rtsup p i nt ced? clare suppkey int, @par tke y in t,o rd erkey i nt,unenil?? um ber int ,qty _inserted in t , @qty_delete d int
-- 打開游標(biāo) ruc nepo? rsor_in sert ed po? open cur sor_d eleted
-—量變給賦值數(shù)標(biāo)游取讀? 讀取游標(biāo)數(shù)值賦給變量 f? fet ch next from
cu rsor_ insert ed
? i into or de rk ey,pa rtkey, @suppk ey, @line nu mber, @qty _ins erte d
f et ch
next from
cursor _d eleted
int o q ty _de lete d
whi le fe tch_st at(yī)us=0 geb? egin
--計算訂單明細(xì)修改時, 訂購數(shù)量得變化值 inserte d表項-d elet ed表項
s sel ect quantity_d if f_ li neit em= @q ty _in se rte d—@ @q ty_delete ed
? --從parts upp 表獲?。醰a ilq ty值, 注意partsupp 表得主鍵為(partk ey,suppke ey)tceles?? t @quanti ty_p ar tsupp =av ailq ty fro m pa rtsu pp
w?? wh er e suppkey= suppke y
and part key= @par tk ey
-—斷判始開?? 開始判斷
geb? begi n
fi? f quant ity _d iff_ lin eite m=0
?
? p rin t “ 更新得數(shù)量與原表中得值相同, 不需要更新”
e ls e if @quantit y_d iff_lin eitem 〈=q uantity_partsupp
?
be gin
? pu?? pd at(yī)e partsupp
s et avail qty= availqty-@qua ntit y_d iff_li ne item
? pus erehw? ppkey=suppkey
and
p artkey= @par tke y
?
? p rint “ 兩個表都更新成功’ ?
??ne? nd
els e
igeb?? in
? u?? update li nei tem
set
quantit y=quantity+ @quanti ty_diff_linei tem
whe re o rd erke y=@orde rke y and li nenu mber= @li ine number
?
p ri nt '更新失敗”
?? end
? e nd ef?? etch ne xt
fr om c urso r_i nserted
i nto @ord erke y, @partkey,s upp key, @linenumb er, @qt y_ ins ert ed
f etch ne xt from curso r_de leted into @qt y_d elet ed dne? nd
d eallocat e cur sor _i nserte d
dealloc at(yī) e cursor_de le ted e end e end(2)在 在 l ineite m表上定義一個 instea d of in sert 觸發(fā)器, 當(dāng)插入明細(xì)表中一條記錄 時 時, 應(yīng)先檢查供應(yīng)表par tsupp 得 得 ava il qt y就是否足夠 qu anti ty 得數(shù)量 c rea te t rig ger tri g_lineitem_q ua nti ty_ insert
on
line item i instead of inser t as b begin
-— 聲明游標(biāo)變量指向 inserte d表
d eclar e cur sor_inserte d cur sor
rea d_ only f or
s?? select or derk ey,pa rtkey,sup pk ey, lin en umber,q uantity
f rom ins er ted
-—
聲明變量獲取查找信息
dec lare quantity int, @av ailq ty i nt, @suppkey
in t, @partkey
in nt t, @o rderkey int, @linenu mber int
-—標(biāo)游開打? 打開游標(biāo) c nepo? curs or_ins erted -? -— 讀取游標(biāo)
f etc h next fro m cursor_insert ed int o @orde rkey,partkey,@ @s suppkey, @linenumber,qu antity
wh ile @@FETCH_S TATUS= 0 igeb? in
--為變量賦值
a tceles? availqty y
= =av ai lqt y fr om
partsupp
w? whe re suppkey =@su ppk ey and part ke y= partke y
? if @quant ity 〈= @avail qt y
-— 如果可以更新
b? begin /??? /*將 將 insert ed 表中得記錄插入到明細(xì)表*/
? sni? sert
i nto l ineite m select *
from i nserted
ro=yekredro
erehw?? rderk ey and
linenumb er = @linenumber */??新更時同? *同時更新 part supp 表得數(shù)量*/
?? u upd ate pa rtsup p set a vailqty=availqty-@quanti ty
?
erehw? e sup pke y= @sup pkey and partkey=part key
p pr int ’pa arts upp 表有足夠得貨物可以滿足 lin eitem 得quan tity y, 插入成功’
end
else
begin
?? p rint t
' 'pa rt sup p表沒有足夠得貨物可以滿足 l ineitem 得 得 q uantity,插入失敗’
dne?f?? fetc h next from curso r_ins ert ed in to @ord erkey, partkey, suppkey, @li inenumbe r, qu antity e? en d
deall ocat e cursor_ inserted end(3)在 在 line ite m表上定義一個 inste ad of del ete 觸發(fā)器,當(dāng) 當(dāng) 刪除 明細(xì)表中記錄 時 時, 同時改變表 供應(yīng)表 partsupp 得ava il qty y 數(shù)值 c re at(yī)e trigge r tri g_ lin eit em_quanti ty_ del ete o n line item inste ad of del ete as be gin
—-聲明游標(biāo)變量指向deleted 表
de cla re curs or_ del eted c ursor
read _only for
les?? elect or derke y, par tkey,sup pkey, linenumbe r,quan tity fr om deleted -? --聲明變量
decl are s upp key i nt, par tke y
int, @orde rkey int,linen umb er
int, @qua ant it y int
—-標(biāo)游開打? 打開游標(biāo)
open
cursor_deleted
-—標(biāo)游取讀? 讀取游標(biāo)
f fetch next fr om
curs or _del et ed
in to @ord erkey , @partkey,s uppke ey, @lin en umber,q ua ntity
whi le
F FET CH_ STATUS=0 igeb? in
*/*除刪? *刪除*/
? de let e from
lineite m where linenu mber= line number and o rde rkey =ord der key
*/新更時同? 同時更新 pa rt supp 表得數(shù)量*/
u pdate
parts upp se t
availqt y=a vai lq ty+quant ity
? where s uppke y= @s uppkey a nd par tk ey= @p ar tkey
? p rin t
’ 刪除成功, 并且已經(jīng)把貨物數(shù)量歸還到 p ar tsup p里“ f?? fetc h ne xt fr om
curso r_del eted
into @ord er ke y,p ar tkey,@ @suppkey, @lin enu mber,quanti ty ne? nd ed? ea lloc at(yī)e cursor_ delete d end(4)驗證 update 觸發(fā)器--查瞧li neit em 得quan tit y select *
fr om
lin eit em whe re or derk ey =1830
and li nenum ber=1;
—— 查詢partsupp 表得ava ilqt y se lect * from part supp w here suppk ey =(select
supp key fr om
lin e
item w here ord erkey=18 30)
and partk ey
=(s elec t part key from lin eite m wh er e
order key =18 30 a nd linenu mber=1)
---更新數(shù)量過大
—— 更新得值與原值相同
---更新到+ + 2 00 數(shù)量, , 成功
update
li neitem set
quant ity
=q uanti ty+ 200
where order key=1830
and lin en umber = 1;
--更新 +2 00 成功后l ineite m得 quanti ty y 變化
—— 更新+200 成功后par ts upp 表得a va ilqty 變化
實驗到此。所有創(chuàng)建得觸發(fā)器結(jié)果
刪除觸發(fā)器
dr op trigger tri g_li nei tem_de lete;實驗六存儲過程實驗
(1 1)
定義無參數(shù) 得存儲過程并執(zhí)行
更新所有訂單總價
go?
CREATE
PROCED URE Pro c_Ca lTotalP ri ce
AS BEGIN
up date
o rders set to talprice =
(sele ct sum(exte ndedprice*(1-discount)*(1+ tax))
? fr om li nei tem
o
erehw? orders、orderkey= li neite m。o rderke y)END
go 執(zhí)行此存儲過程
exec P ro c_CalTotal Pric c e;;
(2 2)
定義有參數(shù)得存儲 過程并執(zhí)行
更新給定訂單號得總價
g go create pro cedu re P ro_Cal TotalP rice 4Ord er @id_ order
intege r a as be gin
u up date e
o orders set total price=(les? lect SU M(ext en ded price*(1 -di sc ount)*(1 +tax))
? fr om li neit em
? whe re
orde rs。or derke y=lin eite m。orderkey)end g o 執(zhí)行此存儲過程 exe c P ro_ CalTota lPr ice 4Order @id_ order=2;((3))
定義有局部變量得存儲過程 更新某一個顧客所有訂單得總價 g o cr eate
procedure Proc_Ca lTot alPr ice4Custo me r n ame _cus to mer varc har(50)as
dec lare cus tk ey _c usto mer i nt b egin tceles? t
cus tke y_cu stomer=cu st key
from
cu sto mer
where name _customer =RTRI M(n ame)
u pdate order s set to talp ri ce=(tceles?? t S UM(exte nde dp rice *(1 -dis count)*(1+tax))rf?? rom li neite m
w? wher e orders、order key=lineitem。or derke y
? and
o rde rs、cust key= @c ustkey _cust omer)?e nd go 執(zhí)行此存儲過程
e xec Pro c_CalT ot alPr ic e4C ustome r name_custom er=” 艾錦亮’;查瞧結(jié)果 u pdate
linei te m set
ex tende dpric e=1 00 , ta x=0.25 where orderkey=“1 830’;s elect * from
l ineite m
w here order key ='183 0’;
s elect * fr om or ders wh ere custke y=(sele ct cus tk ey from custo mer whe re name=’ 艾錦亮’);
(4)
定義一個帶輸出參數(shù)得存儲過程 更新某個顧客得所有得訂單總價, 并輸出總價 go crea te proc edure Proc_C alT otalP ri ce4Custo me r2
na me_ cu st ome r va rch ar(50), totalpr ice_ord er real out put
as —— 聲明一個變量存儲 name 對應(yīng)得顧客編號 d eclar e @c us tkey _cus tomer integer
—— 為該變量賦值
se lec t c ustke y_cu sto mer =cu stkey
from cus tome r w here n ame_ customer=R TRIM(n ame)begi n -? -- 更改訂單總價
up date
o rd ers
set t otalp rice=(se lec t SUM(ext endedprice*(1-dis count)*(1+tax))
f? fr om l in eitem rehw?? ere
orders。o rderk ey=li neit em.ord erk ey、sredro dna?、c ustkey =@custkey_ custome r)
-— 為返回變量賦值
select
totalprice_order=total pri ce fro m orders
? w here custk ey= @c ustkey_c ustom er
?
e nd go —-執(zhí)行帶有輸出參數(shù)得存儲過程要聲明輸出參數(shù)變量 declare t otalp rice re al;exe c Proc_ Cal Tota lPrice4C ustomer2
” 艾錦亮 ', @to talpri ce
output;--在屏幕上輸出返回值結(jié)果
selec t t otalpri ce;
實驗到此, , 所有結(jié)果顯示
(5 5)
修改存儲過程名
exec sp_renam e ’Pr o_Cal TotalPrice4Ord er",’C alTotalPr ice4 Or der';
(6)編譯存儲過程
exe c sp_ re compile ’CalTotalPric ce 4Order’;
(7)刪除存儲過程
d drop
proc edure CalT otalPric e4Orde r;e exe c sp_h elptext CalT ot alPrice4 Order;
第二篇:sql上機實驗報告
實驗一(2.20)
實驗內(nèi)容:熟悉sql server 2005
實驗?zāi)康模赫莆誷ql服務(wù)器的啟動和停止方法、熟悉management studio 的操作界面和新建查詢。
重點:使用數(shù)據(jù)庫引擎啟動數(shù)據(jù)庫服務(wù)器、停止數(shù)據(jù)庫服務(wù);服務(wù)器的認(rèn)識以及如何啟動查詢分析器
難點:無
實驗操作:
1.使用數(shù)據(jù)庫引擎啟動數(shù)據(jù)庫服務(wù)器:
開始->程序->microsoft sql server 2005->
sql server management studio->數(shù)據(jù)庫引擎->服務(wù)器名稱(選擇)->連接。
2.分別打開數(shù)據(jù)庫,安全性和管理文件夾查看文件內(nèi)容。
3.點擊新建查詢建立一個查詢窗口。
4.打開model數(shù)據(jù)庫,查看其中的文件。
實驗二(2.27)
實驗內(nèi)容:使用manegement studio和查詢分析器創(chuàng)建數(shù)據(jù)表
實驗?zāi)康模赫莆談?chuàng)建數(shù)據(jù)表的方法
重點:學(xué)會使用查詢分析器創(chuàng)建數(shù)據(jù)表
難點:字段數(shù)據(jù)類型的選擇以及數(shù)據(jù)長度的定義
實驗操作:
在basetest數(shù)據(jù)庫中創(chuàng)建學(xué)生表,學(xué)生(sno,sname,ssex,sage,sdept)。其中sno,sname,ssex,sdept字
段數(shù)據(jù)類型設(shè)為char,長度為10;sage字段數(shù)據(jù)類型設(shè)為int!create table 學(xué)生
實驗三(3.5)
實驗內(nèi)容:添加表的約束
實驗?zāi)康模赫莆帐褂胊lter table 語句修改表的時候添加表的約束
重點:為表添加主鍵外鍵約束
難點:為列設(shè)置列級完整性約束;使用sql語句為表設(shè)置外鍵。
實驗操作:
1:為學(xué)生表添加主鍵約束pk約束,設(shè)置sno為主鍵
alter table 學(xué)生
add constraint pk約束
primary key(sno)
實驗四(3.12)
查詢
(一)實驗?zāi)康模赫莆誗elect語句的使用方法
實驗重點:Select語句的語法結(jié)構(gòu)
實驗難點:連接查詢
實驗步驟:1.練習(xí)查找指定列的查詢
2.練習(xí)查找經(jīng)過計算的列
3.練習(xí)為查找字段設(shè)置字段別名
4.Where子句的使用
5.多表連接查詢
實驗五(3.19)
查詢
(二)實驗?zāi)康模赫莆誗QL語句的使用方法
實驗重點:統(tǒng)計查詢和嵌套查詢和聯(lián)合查詢
實驗難點:子查詢的方法
實驗步驟:1.1.Compute與Compute By的區(qū)別
2.子查詢 In與Exsits的區(qū)別
3.比較運算的子查詢
4.聯(lián)合查詢
5.OrderBy子句的使用
6.統(tǒng)計函數(shù)的使用
7.Group By 與Having的使用
實驗六(3.36)
查詢
(三)實驗?zāi)康模赫莆誗QL語句的使用方法
實驗重點:簡單查詢和統(tǒng)計查詢和嵌套查詢
實驗難點:嵌套查詢的方法
實驗步驟:1.在student表中查詢CS系學(xué)生信息,并產(chǎn)生一個總?cè)藬?shù)行
2.查詢學(xué)生信息,統(tǒng)計總?cè)藬?shù)并對每個系產(chǎn)生一個學(xué)生總?cè)藬?shù)行
3.按系分組,并在每組下顯示本系中年齡最大學(xué)生年齡
4.按系和性別分組并統(tǒng)計最大年齡和最小年齡
實驗七(4.2)
實驗內(nèi)容:創(chuàng)建索引
實驗?zāi)康模赫莆談?chuàng)建索引的方法
重點:掌握索引的基本功能
難點:使用sql語句創(chuàng)建索引
實驗操作:
1:使用manegement studio 創(chuàng)建索引
2:創(chuàng)建一表myfriend,所包含的屬性列為(名字char(10),性別(10),地址(100)),并在該表上基于名字創(chuàng)建一個唯一聚集索引wj索引,索引的填充為100。
實驗八(4.9)
實驗內(nèi)容:創(chuàng)建視圖
實驗?zāi)康模赫莆談?chuàng)建視圖的方法
重點:掌握視圖的基本功能
難點:使用sql語句創(chuàng)建視圖
1、在basetest數(shù)據(jù)庫中創(chuàng)建一個MA系的學(xué)生信息視圖
2、創(chuàng)建IS系女生選課信息,指定字段中文名
3、使用WITH CHECK OPTION子句創(chuàng)建男生信息視圖
4、在CS系學(xué)生信息視圖中查詢女生的信息
5、修改cs系學(xué)生05008的年齡為21歲
實驗九(4.16)
實驗內(nèi)容:流程控制語句與用戶自定義函數(shù)的使用
實驗?zāi)康模赫莆樟鞒炭刂普Z句與用戶自定義函數(shù)的使用
重點:掌握自定義函數(shù)的創(chuàng)建
難點:表值用戶自定義函數(shù)的創(chuàng)建
實驗操作:
1:計算1+2+3+...+100的和
2:計算n!(n=10)
3: 查詢是否有選修成績高于90分的學(xué)生,有則輸出該生信息,沒有就輸出“不存在選修成績高于90分的學(xué)生。”
實驗十(4.23)
實驗內(nèi)容:存儲過程、事務(wù)及游標(biāo)
實驗?zāi)康模赫莆沾鎯^程、事務(wù)及游標(biāo)的使用
重點:掌握存儲過程的創(chuàng)建、事務(wù)創(chuàng)建及游標(biāo)的使用方法
難點:帶參數(shù)的存儲過程的創(chuàng)建、游標(biāo)的使用
實驗操作:
1:創(chuàng)建存儲過程ppa,能根據(jù)給定的‘系別’返回該系對應(yīng)的學(xué)生的sno,sname,ssex 2:創(chuàng)建存儲過程ppb,能根據(jù)給定的‘系別’統(tǒng)計該系的人數(shù),并將人數(shù)輸出顯示!
第三篇:SQL實驗報告總結(jié)
《數(shù)據(jù)庫系統(tǒng)概論(第四版)》
體
會
學(xué)號: 姓名: 班級: 教師:
學(xué)
期實 驗 總 結(jié) 與 心 得
【實驗名稱】 數(shù)據(jù)庫的創(chuàng)建 【實驗內(nèi)容】
1、新建sql注冊表。
2、新建數(shù)據(jù)庫。主數(shù)據(jù)文件:邏輯文件名為student_data,物理文件名為student.mdf,初始大小為10mb,最大尺寸為無限大,增長速度為10%;
數(shù)據(jù)庫的日志文件:邏輯名稱為student_log,物理文件名為student.ldf,初始大小為1mb,最大尺寸為5mb,增長速度為 1mb
3、修改已注冊的sql server屬性為使用sql server身份驗證。
【實驗名稱】 數(shù)據(jù)庫的附加、分離、導(dǎo)入導(dǎo)出及分離 【實驗內(nèi)容】 1.數(shù)據(jù)庫文件的附加與分離(轉(zhuǎn)載于:sql實驗報告總結(jié))2.數(shù)據(jù)庫文件的導(dǎo)入和導(dǎo)出 3..數(shù)據(jù)庫的刪除 4.修改數(shù)據(jù)庫
【實驗名稱】 數(shù)據(jù)庫的創(chuàng)建(書中作業(yè))【實驗內(nèi)容】 1.在數(shù)據(jù)庫student中創(chuàng)建一個學(xué)生基本信息表 1.用企業(yè)管理其創(chuàng)建表 2.用查詢分析器創(chuàng)建表 2.sql server 2005的系統(tǒng)數(shù)據(jù)類型分為哪幾類?常用的數(shù)據(jù)類型有哪些?
答:字符串類型、整型、長整型、短整型、浮點數(shù)類型、定點數(shù)類型、日期、時間。常用的數(shù)據(jù)類型有:字符串類型、整型、長整型、短整型、浮點數(shù)類型、定點數(shù)類型、日期、時間
3.在數(shù)據(jù)庫student中創(chuàng)建一個名為t_couse(課程信息表)1.用企業(yè)管理其創(chuàng)建t_course表 2.用查詢分析器創(chuàng)建t_course 4.在數(shù)據(jù)庫student中創(chuàng)建一個名為t_score(學(xué)生成績)的表 5.sql server 2005 中有多少種約束?其作用分別是什么
答:非空約束,作用指定某一列不允許空值有助于維護數(shù)據(jù)的完整性,因為這樣可以確保行中的列永遠(yuǎn)保護數(shù)據(jù)。
主鍵約束,作用可以保證實體的完整性,是最重要的一種約束。唯一約束,作用指定一個或多個列的組合值具有唯一性,以防止在列中輸入重復(fù)的值。
檢查約束,作用對輸入列或者整個表中的值設(shè)置檢查條件,以限制輸入值,保證數(shù)據(jù)庫數(shù)據(jù)的完整性。
默認(rèn)約束,作用指定在插入操作中如果沒有提供輸入值。怎系統(tǒng)自動指定值。外鍵約束,作用主要用來維護兩個表之間數(shù)據(jù)的一致性。6.分別為t_course表和t_score表創(chuàng)建唯一性約束、檢查約束、默認(rèn)約束。篇二:sql server綜合實驗報告
華北科技學(xué)院計算機系綜合性實驗
實 驗 報 告 課程名稱 數(shù)據(jù)庫系統(tǒng) 實驗學(xué)期 2010 至 2011 學(xué)年 第 二 學(xué)期
學(xué)生所在系部 計算機系 年級 二年級 專業(yè)班級
網(wǎng)絡(luò)工程b093班
學(xué)生姓名
沈亞強
學(xué)號
200907024313
任課教師
郭紅 實驗成績
計算機系制
實驗報告須知
1、學(xué)生上交實驗報告時,必須為打印稿(a4紙)。頁面空間不夠,可以順延。
2、學(xué)生應(yīng)該填寫的內(nèi)容包括:封面相關(guān)欄目、實驗地點、時間、目的、設(shè)備環(huán)境、內(nèi)容、結(jié)果及分析等。
3、教師應(yīng)該填寫的內(nèi)容包括:實驗成績、教師評價等。
4、教師根據(jù)本課程的《綜合性實驗指導(dǎo)單》中實驗內(nèi)容的要求,評定學(xué)生的綜合性實驗成績;要求在該課程期末考試前將實驗報告交給任課教師。綜合性實驗
中,所涉及的程序,文檔等在交實驗報告前,拷貝給任課教師。任課教師統(tǒng)一
刻錄成光盤,與該課程的期末考試成績一同上交到系里存檔。
5、未盡事宜,請參考該課程的實驗大綱和教學(xué)大綱?!?/p>
數(shù)據(jù)庫系統(tǒng)
》課程綜合性實驗報告 篇三:數(shù)據(jù)庫上機實驗報告+總結(jié)
數(shù)據(jù)庫集中上機報告
學(xué) 生: 馬志鵬 學(xué) 號: 0221090118 班 級: 02210901 專
業(yè):計算機應(yīng)用技術(shù) 重慶郵電大學(xué)移通學(xué)院 2011年6月
第一天:access數(shù)據(jù)庫基本操作 1 實驗?zāi)康?/p>
1、熟悉的掌握access數(shù)據(jù)庫結(jié)構(gòu)與創(chuàng)建
2、了解創(chuàng)建、修改、刪除、查詢、保存等操作
3、輸入數(shù)據(jù)創(chuàng)建、設(shè)計器創(chuàng)建、向?qū)?chuàng)建。2 實驗內(nèi)容 3 實驗結(jié)果
1.2.2 2 access數(shù)據(jù)表的編輯
第二天 數(shù)據(jù)表基本操作 1 表關(guān)系與編輯數(shù)據(jù) 1 實驗?zāi)康模?/p>
1、實現(xiàn)一對一,一對多,多對多的實體關(guān)系
2、對“學(xué)生基本信息”表中的記錄進行排序,按出生日期降序排列
3、從“學(xué)生基本信息”表中篩選出所有計算機系男生的記錄
4、從“學(xué)生基本信息”表中篩選出回族和蒙古族的所有學(xué)生記錄 3 2 實驗內(nèi)容
1.select 學(xué)生基本信息表.學(xué)生姓名, 成績檔案表.* from 成績檔案表 inner join 學(xué)生基本信息表 on 成績檔案表.學(xué)生學(xué)號 = 學(xué)生基本信息表.學(xué)生學(xué)號
where(((學(xué)生基本信息表.學(xué)生姓名)=張冰冰));2 select 學(xué)生基本信息表.* from 學(xué)生基本信息表 where(((學(xué)生基本信息表.性別)=男)and((學(xué)生基本信息表.班級名稱)=計算機系));3 select 成績檔案表.c語言, 課程表.* from 成績檔案表, 課程表;4 select 學(xué)生基本信息表.*, 學(xué)生基本信息表.性別, 學(xué)生基本信息表.班級名稱 from 學(xué)生基本信息表 where(((學(xué)生基本信息表.性別)<>男)and((學(xué)生基本信息表.班級名稱)<>計算機系));5 select 學(xué)生基本信息表.*, 學(xué)生基本信息表.出生日期 from 學(xué)生基本信息表 where(((month([出生日期]))=9)and((day([出生日
期]))=1));6 select 學(xué)生基本信息表.* from 學(xué)生基本信息表 where(((學(xué)生基本信息表.學(xué)生姓名)like 李*));3 實驗結(jié)果 4 第3天 sql查詢設(shè)計 1 sql語言查詢 1 實驗?zāi)康模?/p>
熟悉了解sql語句 5 篇四:sqlserver實驗報告—數(shù)據(jù)庫和表的創(chuàng)建
數(shù)據(jù)庫實驗報告
課程名稱信息系統(tǒng)數(shù)據(jù)庫技術(shù) 實驗名稱 數(shù)據(jù)表的管理 專業(yè)班級
姓 名
學(xué) 號 實驗日期
實驗地點 2012—2013學(xué) 第 一 學(xué)期 篇五:數(shù)據(jù)庫上機實驗報告sql server 2000 課程代碼:1010000450 數(shù)據(jù)庫 database 學(xué)分:3 總學(xué)時:48 實驗學(xué)時:16 面向?qū)I(yè):信息與計算科學(xué),數(shù)學(xué)與應(yīng)用數(shù)學(xué)
一、實驗教學(xué)目標(biāo)
數(shù)據(jù)庫是計算機科學(xué)與技術(shù)專業(yè)的專業(yè)必修課程。課程內(nèi)容主要包括:數(shù)據(jù)模型、關(guān)系代數(shù)、關(guān)系數(shù)據(jù)庫標(biāo)準(zhǔn)語言sql、關(guān)系系統(tǒng)、關(guān)系數(shù)據(jù)理論、數(shù)據(jù)庫設(shè)計方法。學(xué)習(xí)本課程的過程中,只有通過上機實驗,才能使學(xué)生真正深層次了解數(shù)據(jù)庫系統(tǒng)的體系結(jié)構(gòu),掌握數(shù)據(jù)庫系統(tǒng)的基礎(chǔ)理論、技術(shù)和方法,掌握主流數(shù)據(jù)庫管理系統(tǒng)sql server 2000的應(yīng)用技術(shù)及數(shù)據(jù)庫應(yīng)用系統(tǒng)的設(shè)計、開發(fā)能力。
二、實驗教學(xué)基本要求
認(rèn)真閱讀教材中與實驗相關(guān)的章節(jié)內(nèi)容,提前做好實驗準(zhǔn)備,做到在每個實驗前明確實驗?zāi)康摹⒄莆諏嶒灥幕緝?nèi)容及操作方法;在實驗中正確使用實驗設(shè)備,認(rèn)真觀察、分析實驗結(jié)果;實驗后要根據(jù)要求做好總結(jié),寫出實驗報告。
三、實驗教材或?qū)嶒炛笇?dǎo)書
《數(shù)據(jù)庫系統(tǒng)概論》,薩師煊 王珊主編,高等教育出版社
四、考核方式與評分辦法 實驗成績評定分兩部分:上機實驗操作占50%,實驗報告占50%。
五、實驗項目設(shè)置
六、實驗內(nèi)容與實驗方式
實驗一
安裝sql server 2000
(一)實驗內(nèi)容 1.安裝microsoft的數(shù)據(jù)庫服務(wù)軟件sql server 2000。以sql server2000企業(yè)版為例安裝教程
第一步:打開安裝文件中的“autorun”文件,安裝程序會自動運行出現(xiàn)版本選擇界面,我們以選擇企業(yè)版為例。
第二步:在接下來的界面中選擇“安裝數(shù)據(jù)庫服務(wù)器”選項。
第三步:這時出現(xiàn)安裝向?qū)В霈F(xiàn)“計算機名”窗口?!氨镜赜嬎銠C”是默認(rèn)選項,其名稱就顯示在上面,我們按其默認(rèn)點“下一步”。
第四步:接下來,在“安裝選擇”對話窗口中,同樣按其默認(rèn)項“創(chuàng)建新的sql server實例,或安裝客戶端工具”點下一步。
第五步:在 用戶信息 窗口,輸入用戶信息,并接受軟件許可證協(xié)議。
第六步:在 安裝定義窗口,選擇 服務(wù)器和客戶端工具 選項進行安裝。我們需要將服務(wù)器和客戶端同時安裝,這樣在同一臺機器上,我們可以完成相關(guān)的所有操作,對于我們學(xué)習(xí)sql server很有用處。如果你已經(jīng)在其它機器上安裝了sql server,則可以只安裝客戶端工具,用于對其它機器上sql server的存取。
第七步:在 實例名 窗口,選擇 默認(rèn) 的實例名稱。這時本sql server的名稱將和windows 2000服務(wù)器的名稱相同。第八步:在 安裝類型 窗口,選擇 典型 安裝選項,并指定 目的文件夾。
第九步:在 服務(wù)賬號 窗口,請選擇 對每個服務(wù)使用統(tǒng)一賬戶...的選項。在 服務(wù)設(shè)置 處,選擇 使用本地系統(tǒng)賬戶。如果需要 使用域用戶賬戶 的話,請將該用戶添加至windows server的本機管理員組中。
第十步:在 身份驗證模式 窗口,請選擇 混合模式...選項,并設(shè)置管理員sa賬號的密碼。如果需要更高的安全性,則可以選擇 windows身份驗證模式,這時就只有windows server的本地用戶和域用戶才能使用sql server了。
第十一步:在 選擇許可模式 窗口,根據(jù)您購買的類型和數(shù)量輸入(0表示沒有數(shù)量限制)。每客戶表示同一時間最多允許的連接數(shù),處理器許可證表示該服務(wù)器最多能安裝多少個cpu。筆者這里選擇了 每客戶 并輸入了100作為示例。
第十二步:約10多分鐘的安裝時間,然后打開開始菜單下的sql子目錄下的企業(yè)管理器。2.熟悉軟件的使用以及各個組成部分。
第一:打開企業(yè)管理器,可以對本地的數(shù)據(jù)進行管理,也可以登錄服務(wù)器端,即可以遠(yuǎn)程操作自己的數(shù)據(jù)庫 ;
第二:登錄查詢分析器,可以通過sql管理語句,對數(shù)據(jù)庫進行管理。
(二)實驗方式與分組要求
老師先講解十分鐘,學(xué)生上機操作數(shù)據(jù)庫sql server2000,最后老師檢查操作結(jié)果。每次86組,每組1人。
實驗二
啟動sql server2000和建庫、表
(一)實驗內(nèi)容 1.啟動數(shù)據(jù)庫服務(wù)軟件sql server 2000。第一步:打開企業(yè)管理器,可以登錄服務(wù)器端,即可以遠(yuǎn)程操作自己的數(shù)據(jù)庫,登錄步驟如下; 第二步:左側(cè)方框中控制臺根目錄的子目錄的sql server組上,右擊,并新建sql server注冊?; 第三步:在“服務(wù)器”一欄里輸入想要登錄的服務(wù)器ip地址,選擇使用sql server身份驗證,寫入登錄名和密碼,就可以登錄了。
第四步:登錄查詢分析器,可以通過sql管理語句,對數(shù)據(jù)庫進行管理。2.在sql server 2000中建立數(shù)據(jù)庫。
第一步:通過企業(yè)管理器進入服務(wù)器端以后,可以在數(shù)據(jù)庫文件里“新建數(shù)據(jù)庫”。第二步:輸入數(shù)據(jù)庫名稱。
第三步:通過相關(guān)需求對數(shù)據(jù)庫的相關(guān)屬性進行設(shè)置,然后完成。3.在數(shù)據(jù)庫上建立表。第一步:登錄企業(yè)管理器
第二步:進入自己的database,并打開“表”選項;
第三步:在表選項右側(cè)空白處,點擊右鍵,選擇“新建表?”;
第四步:在彈出的窗口中,分別填寫列名,數(shù)據(jù)類型,長度和是否允許空值,然后關(guān)閉窗口。第五步:關(guān)閉窗口會彈出是否保存表,選擇是,接下來彈出的窗口中填寫表名即可。
(二)實驗方式與分組要求
老師先講解十分鐘,學(xué)生上機操作數(shù)據(jù)庫sql server2000,最后老師檢查操作結(jié)果。每次86組,每組1人。
實驗三
sql server2000查詢分析器
(一)實驗內(nèi)容 1.啟動數(shù)據(jù)庫服務(wù)軟件sql server 2000的查詢分析器。第一步:在登錄企業(yè)管理器的前提下,登錄查詢分析器。
第二步:登錄查詢分析器與登錄企業(yè)管理器類似,需要輸入ip地址或者如果是本地數(shù)據(jù)庫,直接用windows身份驗證即可。2.在查詢分析器中建立表。
第一步:在查詢分析器命令欄里輸入以下語句: 定義一個學(xué)生-課程模式s-t create schema “s-t” authorization wang /*為用戶wang定義了一個模式s-t*/ 然后點擊“分析查詢”和“執(zhí)行查詢”。第二步:在查詢分析器命令欄里輸入以下語句:
建立“學(xué)生”表student,學(xué)號是主碼,姓名取值唯一 create table student(sno
char(9)primary key, /* 列級完整性約束條件*/ sname char(20)unique, /* sname取唯一值*/ ssex char(2), sage smallint, sdept char(20));然后點擊“分析查詢”和“執(zhí)行查詢”。
(二)實驗方式與分組要求
老師先講解十分鐘,學(xué)生上機操作數(shù)據(jù)庫sql server2000,最后老師檢查操作結(jié)果。每次86組,每組1人。
實驗四
sql語言的ddl
(一)實驗內(nèi)容 1.用如下語句對表進行操作:create table 建表;drop table 刪除表;alter table 更改表。
create table student(sno char(4)primary key)drop table student /*當(dāng)student表與其他數(shù)據(jù)有級聯(lián)關(guān)系或者其他關(guān)系時,不能刪除。*/ alter table student add s_entrance date null/*向student表增加“入學(xué)時間”列,其數(shù)據(jù)類型為日期型*/ alter table student drop column sno /*刪除student表中的sno這一列,含有以下關(guān)系的列不能被刪除: ? 被復(fù)制列。
? 用在索引中的列。
? 用在 check、foreign key、unique 或 primary key 約束中的列。? 有相關(guān)聯(lián)的默認(rèn)值(由 default 關(guān)鍵字定義)的列,或綁定到默認(rèn)對象的列。? 綁定到規(guī)則的列。*/ 2.用如下語句對視圖進行操作:create view 建視圖;drop view 刪除視圖。/*創(chuàng)建一個is_student視圖,視圖中返回來自于表student中sdept=’is’的所有學(xué)生的sno,sname,sage列*/ create view is_student
as
select sno,sname,sage
from student where sdept=is /*刪除is_student視圖*/ drop view is_student 3.如下語句對索引進行操作:create index 建立索引;drop index 刪除索引。/*創(chuàng)建一個stusname索引,在student 表中以sname列升序排列*/ create clustered index stusname on student(sname)/*刪除student表中的stusname索引*/ drop index student.stusname
(二)實驗方式與分組要求
老師先講解十分鐘,學(xué)生上機操作數(shù)據(jù)庫sql server2000,最后老師檢查操作結(jié)果。每次86組,每組1人。
實驗五 sql語言的dml
(一)實驗內(nèi)容 1.啟動數(shù)據(jù)庫服務(wù)軟件sql server 2000的查詢分析器,用insert語句對表進行插入操作。/*將一個新學(xué)生元組(學(xué)號:200215128;姓名:陳冬;性別:男;所在系:is;年齡:18歲)插入到student表中*/ insert into student(sno,sname,ssex,sdept,sage)values(200215128,陳冬,男,is,18)/*學(xué)生張成民的信息插入到student表中*/ insert into student values(200215126,張成民,18,cs,男)/*對表student中每一個系,求學(xué)生的平均年齡,并把結(jié)果存入表dept_age表中。*/ 第一步:create table dept_age(sdept char(15), /* 系名*/ avg_age smallint)/*學(xué)生平均年齡*/ 第二步:insert into dept_age(sdept,avg_age)
select sdept,avg(sage)from student group by sdept 2.用update語句對表中已有的記錄進行修改。/*將學(xué)生200215121的年齡改為22歲*/ update student set sage=22 where sno=200215121 /*將所有學(xué)生的年齡增加1歲*/ update student set sage= sage+1 /*將cs系全體學(xué)生的成績置零*/ update sc set grade=0 where cs=(select sdept from student where student.sno = sc.sno)3.用delete語句對表中已有的記錄進行刪除。
第四篇:SQL數(shù)據(jù)庫系統(tǒng)_實驗報告4
實驗四
實驗時間:2014年4月29日
實驗名稱:T-SQL、存儲過程、觸發(fā)器
實驗?zāi)康模?/p>
1、掌握T-SQL常量、變量、運算符、表達式、函數(shù)的使用方法
2、掌握T-SQL流程控制語句使用方法
3、掌握存儲過程創(chuàng)建及使用方法
4、理解觸發(fā)器的創(chuàng)建及使用方法
實驗原理:
T-SQL,即Transact SQL,是微軟對SQL標(biāo)準(zhǔn)的一種實現(xiàn)。存儲過程可以預(yù)編譯T-SQL并保存在數(shù)據(jù)庫系統(tǒng)中,與直接發(fā)送SQL相比,加快了數(shù)據(jù)處理速度。觸發(fā)器可以在數(shù)據(jù)修改時自動運行,保證更有效地實施數(shù)據(jù)完整性。
儀器與材料:
裝有Windows操作系統(tǒng)、SQL Server數(shù)據(jù)庫系統(tǒng)的PC機一臺
實驗步驟:
1、加載示例數(shù)據(jù)庫
2、定義一個字符串變量和一個整型變量,賦值后再輸出查看運行結(jié)果
3、使用全局變量查看SQL Server版本信息
4、定義字符串變量保存身份證號,使用函數(shù)取出其表示的年月日信息并逐個輸出
5、創(chuàng)建函數(shù)計算全體學(xué)生某門課程平均成績
6、求1+2+??+100的和
7、創(chuàng)建存儲過程使用“學(xué)生基本信息表”查詢學(xué)生數(shù)量
8、創(chuàng)建觸發(fā)器,實現(xiàn)功能:當(dāng)“學(xué)生基本信息表”中數(shù)據(jù)被刪除時提示“不允許修改學(xué)生基本信息”
實驗記錄:
??
討論及問題回答:
查找資料,討論索引優(yōu)缺點。
第五篇:SQL數(shù)據(jù)庫系統(tǒng)_實驗報告2
實驗時間:2014年4月1日
實驗名稱:數(shù)據(jù)庫表的管理與查詢語句
實驗?zāi)康模?/p>
1、熟練掌握數(shù)據(jù)表的創(chuàng)建、修改和刪除方法
2、熟練掌握表中數(shù)據(jù)的插入、修改、刪除和查詢方法
3、熟練掌握SQL語句的使用方法
4、理解數(shù)據(jù)表的約束及其使用
實驗原理:
數(shù)據(jù)庫是保存數(shù)據(jù)的集合。表是數(shù)據(jù)的集合,由行列構(gòu)成,行又被稱為記錄,列被稱為字段。SQL全稱是Structured Query Language,其主要有功能有:創(chuàng)建數(shù)據(jù)庫并定義表的結(jié)構(gòu)、查詢需要的數(shù)據(jù)、更新或者刪除指定的數(shù)據(jù)、管理數(shù)據(jù)庫。
儀器與材料:
裝有Windows操作系統(tǒng)的PC機一臺,SQL Server安裝包ISO鏡像
實驗步驟:
1、加載示例數(shù)據(jù)庫,查看示例數(shù)據(jù)庫及內(nèi)部的數(shù)據(jù)庫表結(jié)構(gòu)
2、向“學(xué)生基本信息表”中添加“出生日期”字段
3、刪除“學(xué)生基本信息表”中“年齡”字段
4、刪除表“教師基本信息表”
5、向“學(xué)生基本信息表”中添加5條記錄
6、修改“學(xué)生基本信息表”中學(xué)號為3的學(xué)生的姓名為“張三”
7、刪除“學(xué)生基本信息表”中姓名為“李四”的學(xué)生信息
8、查詢“學(xué)生基本信息表”中所有學(xué)生的所有信息
9、查詢“學(xué)生基本信息表”中所有男生信息
10、查詢“學(xué)生基本信息表”中所有19歲以上學(xué)生信息
11、查詢“學(xué)生基本信息表”中所有姓“張”的學(xué)生信息
12、查詢“學(xué)生基本信息表”中所有選修103號課程的學(xué)生信息
實驗記錄:
1、加載示例數(shù)據(jù)庫,查看示例數(shù)據(jù)庫及內(nèi)部的數(shù)據(jù)庫表結(jié)構(gòu)
2、向“學(xué)生基本信息表”中添加“出生日期”字段
??
3、刪除“學(xué)生基本信息表”中“年齡”字段
??
4、刪除表“教師基本信息表”
??
(以下略)
討論及問題回答:
嘗試查詢所有選修了“SQL數(shù)據(jù)庫系統(tǒng)”并且成績優(yōu)秀的學(xué)生信息。