試驗五-觸發器和存儲過程_第1頁
試驗五-觸發器和存儲過程_第2頁
試驗五-觸發器和存儲過程_第3頁
試驗五-觸發器和存儲過程_第4頁
試驗五-觸發器和存儲過程_第5頁
已閱讀5頁,還剩3頁未讀 繼續免費閱讀

下載本文檔

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

文檔簡介

1、實驗五:觸發器和存儲過程一.實驗目的:理解觸發器和存儲過程的含義,掌握用 SQL語句實 現觸發器和存儲過程的編寫,并初步掌握什么情況下使用事務。二.實驗內容:有一個小型的圖書管理數據庫,包含的表為:bookstore (bookid , bookname , bookauthor , purchasedate , state );-圖書庫存表borrowcard (cardid , ownername );-借書證表borrowlog (cardid , bookid , borrowdate , returndate );- 借書記錄表寫一個存儲過程,實現借書操作,要求有事務處理。(1)讀者

2、借書, 要先設置書籍不在庫標志state (借出),然后增加借書記錄,在同一事務中完成。(2)要求在事務執行過程中引入錯誤觸發事件,以此體 會事務的錯誤保護機制和事務編程的作用。(3)要求用觸發器實現表的完整性控制。三、操作與運行.創建圖書數據庫:create table bookstore(bookid int not null primary key,bookname char(20),bookauthor char(20),purchasedate datetime,state char(4)create table borrowcard(cardid int not null prim

3、ary key,ownername char(20)create table borrowlog(cardid int not null,bookid int not null,borrowdate datetime,returndate datetime,primary key(cardid,bookid),一foreign key(cardid)references borrowcard(cardid),一foreign key(bookid)references bookstore(bookid)通過以上語句,可以看到數據庫中的表建立成功。.創建存儲過程:create proc book

4、_borrowmycardid_in int,mybookid_in int,str_out char(30) outputasbeginif not exists(select * from borrowcard where cardid=mycardid_in) beginset str_out=該讀者不存在returnendif(select state from bookstorewherebookid=mybookid_in尸 借出beginset str_out= 該書以借出endbegin traninsert into borrowlog values(mycardid_in,

5、mybookid_in, getdate(),null)if error0beginrollback transet str_out=執行過程中遇到錯誤!returnendupdate bookstore set state=借出where bookid=myboo kid_inif error0beginrollback transet str_out=執行過程中遇到錯誤!,returnendif error=0begincommit transet str_out=借書成功!,return 1elsebeginrollback transet str_out=執行過程中遇到錯誤!,retu

6、rnendEnd查看執行結果:執行存儲過程:當違反參照完整性時:declare str_outchar(30)exec book borrow1114060119,106,str_outoutputprint執行情況+ str_out當圖書已借出時執行結果會是:declare str_out char(30)outputexec book_borrow 1114060114,102,str_outprint 執行情況+ str_out當正常執行時(即不違反完整性時):原先的借書記錄有:正常借書時:declare str_out char(30) exec book_borrow 1114060

7、116,105,str_out outputprint執行情況(斫題晌的行數為1行)(斫器響的行數為1行)(所題響的行粒為1行)執行情況借書成功!+ str_out查看借書記錄:ST1E阻串鼻I1MHBLM LDP UHlRIlIi Lbt lit ificani |-EP feuitiimu- LmfllECniDIL NHiqViMMl-DI-r 9V3GBI D&.W m:V-Q3-C5!2川 U5- IE: |j:a: n. M7T ltl_L查看圖書在庫狀態:由此可知借書成功。.創建觸發器:create trigger delete_borrowlogonbookstoreforde

8、leteasbeginfrodelete from borrowlog where bookid in(select bookid m deleted)End執行:deletefrom bookstorewhere bookid=102結果:(所影響的行數為1行)查看結束記錄情況:In“oan理國mx卜。1如|由力HL1i Halidalj99. W? MU可知刪除觸發器創建成功。create trigger update_borrowlogon bookstorefor updateas begindeclare old_bookid int,new_bookid intselect old

9、_bookid=bookid from deletedselect new_bookid=bookid from insertedbookiupdate borrowlog set bookid=new_bookid whered=old_bookidEnd執行:update bookstoreset bookid=119where bookid=105結果:11- 1,口,口a X mk i i_,w4三上,I| f 0 來檢查。創建觸發器時剛開始不能創建成,根據提示知道原來在建表時已經 設置了外鍵,所以棄掉就可以了。五.思考題:如何通過系統的設置實現類似的功能, 而不需觸發器? 答:可以使 用企業管理其中可視化的建表方法,也可以使用 sql語句來在表中增加 外鍵約束就可以了

溫馨提示

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

最新文檔

評論

0/150

提交評論