2023年西北工業(yè)大學數(shù)據(jù)庫實驗報告_第1頁
2023年西北工業(yè)大學數(shù)據(jù)庫實驗報告_第2頁
2023年西北工業(yè)大學數(shù)據(jù)庫實驗報告_第3頁
2023年西北工業(yè)大學數(shù)據(jù)庫實驗報告_第4頁
2023年西北工業(yè)大學數(shù)據(jù)庫實驗報告_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

《數(shù)據(jù)庫原理》實驗報告題目:實驗一數(shù)據(jù)庫和表的創(chuàng)建與管理學號姓名班級日期孟玉軍1001140216.11.10實驗內(nèi)容、環(huán)節(jié)以及結(jié)果假設學校允許學生將銀行卡和校園卡進行綁定,在student數(shù)據(jù)庫中有如下的基本表,其中校園卡編號cardid即為學生的學號:?icbc_card(studcardid,icbcid,balance)//校園卡ID,工行卡ID,銀行卡余額campus_card(studcardid,balance)//校園卡ID,校園卡余額創(chuàng)建數(shù)據(jù)庫代碼如下:usestudentcreatetablecampus_card(?studcardidChar(8),balanceDecimal(10,2))createtableicbc_card(?studcardidChar(8),icbcidChar(10),? lanceDecimal(10,2),)示例數(shù)據(jù)如下:insertintocampus_cardvalues('20230031',30)insertintocampus_cardvalues('20230032',50)insertintocampus_cardvalues('20230033',70)insertintoicbc_cardvalues('20230031','',1000)insertintoicbc_cardvalues('20230032','',1000)insertintoicbc_cardvalues('20230033','',1000)針對以上數(shù)據(jù)庫按照規(guī)定完畢下列實驗:編寫一個事務解決(begintran)實現(xiàn)如下的操作:某學號為20230032的學生要從銀行卡中轉(zhuǎn)賬200元到校園卡中,若中間出現(xiàn)故障則進行rollback。(15分)代碼:usestudentbegintransactionzhuanzhanggodeclare@xdecimal(10,2)select@x=balancefromicbc_cardwherestudcardid='20230032'set@x=@x-200if(@x>=0)beginupdateicbc_cardsetbalance=@xwherestudcardid='20230032'updatecampus_cardsetbalance=balance+200wherestudcardid='20230032'committranendelsebeginprint'余額局限性,不能轉(zhuǎn)賬'rollbacktranend結(jié)果顯示:針對本題的數(shù)據(jù)庫和表,分別用品體的例子展現(xiàn)四種數(shù)據(jù)不一致問題:丟失修改、讀臟數(shù)據(jù)、不可反復讀和幻讀(刪除和插入)。(40分,每種數(shù)據(jù)不一致10分)1).丟失修改:執(zhí)行兩段代碼begintransactiondeclare@adecimal(10,2)select@a=balancefromicbc_cardwherestudcardid='20230032'waitfordelay'00:00:05'updateicbc_cardsetbalance=@a+1wherestudcardid='20230032'commitselect*fromicbc_card-------------------------------------begintransactiondeclare@bdecimal(10,2)select@b=balancefromicbc_cardwherestudcardid='20230032'waitfordelay'00:00:05'updat(yī)eicbc_cardsetbalance=@b+2wherestudcardid='20230032'commitselect*fromicbc_card兩段代碼執(zhí)行后的結(jié)果:出現(xiàn)了丟失修改2).讀臟數(shù)據(jù):begintransactiondeclare@bdecimal(10,2)select@b=balancefromicbc_cardwherestudcardid='20230032'updat(yī)eicbc_cardsetbalance=@b*2wherestudcardid='20230032'begintransactionselect*fromicbc_cardcommitrollbackselect*fromicbc_card結(jié)果顯示:3).不可反復讀:begintransactionselect*fromicbc_cardbegintransactiondeclare@bdecimal(10,2)select@b=balancefromicbc_cardwherestudcardid='20230032'updateicbc_cardsetbalance=@b*2wherestudcardid='20230032'commitselect*fromicbc_cardcommit結(jié)果顯示:4).幻讀刪除:begintransactionselect*fromicbc_cardbegintransactiondeletefromicbc_cardwherestudcardid='20230032'commitselect*fromicbc_cardcommit結(jié)果顯示:插入:begintransactionselect*fromicbc_cardbegintransactioninsertintoicbc_cardvalues('20230034','',1000)commitselect*fromicbc_cardcommit結(jié)果顯示:運用鎖機制、數(shù)據(jù)庫的隔離級別等,設計方案分別解決上述丟失修改、讀臟數(shù)據(jù)和不可反復讀(或者幻讀)的數(shù)據(jù)不一致問題。(30分,每種數(shù)據(jù)不一致10分,提醒可以用sp_lock系統(tǒng)存儲過程查看當前鎖狀況)1).丟失修改:begintransactiondeclare@bdecimal(10,2)select@b=balancefromicbc_cardwith(xlock)wherestudcardid='20230032'waitfordelay'00:00:05'updateicbc_cardsetbalance=@b+2wherestudcardid='20230032'commitselect*fromicbc_cardbegintransactiondeclare@adecimal(10,2)select@a=balancefromicbc_cardwith(xlock)wherestudcardid='20230032'waitfordelay'00:00:05'updateicbc_cardsetbalance=@a+1wherestudcardid='20230032'commitselect*fromicbc_card成功加3,解決了丟失修改2).讀臟數(shù)據(jù)begintranupdatecampus_cardwith(xlock)setbalance=balance-20wherestudcardid='20230032'waitfordelay'00:00:05'rollbackbegintransettranisolationlevelreaduncommittedselect*fromcampus_cardwith(holdlock)wherestudcardid='20230032'waitfordelay'00:00:05'select*fromcampus_cardwith(holdlock)wherestudcardid='20230032'結(jié)果顯示:沒有出現(xiàn)臟數(shù)據(jù)3).不可反復讀begintransettranisolationlevelreaduncommitteddeclare@b1Decimal(10,2)declare@b2Decimal(10,2)select@b1=balancefromcampus_cardwith(holdlock)wherestudcardid='20230032'print('第一次數(shù)據(jù):'+convert(varchar,@b1))waitfordelay'00:00:05'select@b2=balancefromcampus_cardwith(holdlock)wherestudcardid='20230032'print('第一次數(shù)據(jù):'+convert(varchar,@b2))begintranupdatecampus_cardwith(xlock)setbalance=balance+100wherestudcardid='20230032'waitfordelay'00:00:03'commit結(jié)果顯示:消除了反復讀錯誤構(gòu)造一個出現(xiàn)死鎖的情形。(10分)事務一:begintransactionselect*fromcampus_cardwith(tablockx)waitfordelay'00:00:05'select*fromicbc_cardwith(tablockx)commit事務二:begintransactionselect*fromicbc_cardwith(tablockx)waitfordelay'00:00:05'select*fromcampus_cardwith

溫馨提示

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

評論

0/150

提交評論