《2023年西北工业大学数据库实验报告.docx》由会员分享,可在线阅读,更多相关《2023年西北工业大学数据库实验报告.docx(13页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、数据库原理实验报告题目:实验一数据库和表的创建与管理学号姓名班级日期孟玉军1 0 0114 0216.11.10一.实验内容、环节以及结果假设学校允许学生将银行卡和校园卡进行绑定,在Stud e nt数据库中有如下的基本表,其中校园卡编号cardid即为学生的学号:。icbcca rd(s tud card i d, i c b c id, ba lan c e) / / 校园卡 ID,工行卡I D,银行卡余额cam p us_card( s tudca r d id, b a 1 a nee)/ /校园卡 I D,校园卡余额创建数据库代码如下:use siude n(c rea t e t
2、a b le campus_car d( s t udcar d id Cha r ( 8 ),balanc e Dec imal( 1 0 .2)cr e a t e t a bl e i c bc_ c ard(。s t ud c ardid Char,c b cid Char (10 ), 1 a ncc Deci mal(10.2),)示例数据如下:in s ert i nto camp u s _c ard v a lues(2O 2 3 0 0317 , 3 0 )i nsert into c a mp u s_ c a rd v a lu e s (z 20 2 3 0 03 2
3、 5 0)se t tr a n i sola t i on level read un c omm i ttedselec t * f r om c ampus_card w ith (hoi d lock)w h ere stud c arc! i d=, 20 2 3 0 032wait f or d e lay* 0 0:0 0 : 05selec t * from c a mp u s _c a r d with (holdlock)wh e re studcardi d = 2023 0 0 3 2结果显示:studcanSd balance 150032 50.00studcar
4、dd balance120150032! 50.00;没有出现脏数据3) .不可反复读b e gin t r a nset t r a n isolat i on 1 e ve 1 r e ad u n comm itt edec 1 a r e bl De c i mal ( 1 0, 2)d e cl a re b2 De c ini a 1 ( 1 0, 2 )(hoid lock)sei e ct bl=ba 1 a n ce fr o m c a mpus_ c ard wi t hwhere stud c ar d id= 2 02 3 003 2print (第一次数据:+con
5、ve r t (va r char, b 1)waitfor delay 0 0: 0 0: 0 5 sei e ct b2=balance from campus_ card w i th (hold 1 oc k )where stud card id=20 2 3 0 032print (第一次数据:+convert(varchar,b2)begin tranupda t e campus_c ard with (xl o ck)se t b a lance=bcil a nee +10 0wh e r e studcard i d= 202 3 0 0 3 2 wai t for de
6、lay 0 0:00:03,commit结果显示:4 |m因消息第一次数据:50.00第一次数据:5。.。B47SQLE:, 查询已成功执行.消除了反复读错误4 .构造一个出现死锁的情形。(10分)事务一:beg i n transa c t ions e lec t * f r om cam p us_c a rd w it h (ta b lock x )wai t for delay 0 0 : 0 0:05se 1 ect * f rom ic b c_c a rd with (tab 1 o ck x )comm it事务二:begin trans a c t ionsei e ct
7、 * f r om i c b c_ca r d with (tabi o ck x )wait f or delayz 00: 0 0: 05se 1 e ct * from campus_ c ard with (tabi o ckx)s(3消事comm i t状率56,第4行)与另一个进程被死锁在锁资源上,并且已被选作死锁牺牲品。请重新运行该事务。出现了死锁5 .运用db c c log命令查看stude n t数据库的事务日记。(5分)dbcc log (stud ent)Current L5NOperationContextI ransaction IL)Log blockbener
8、ation1| 00000019:0000001b:0001LCX.NULL0000:000002780200000019:0000001b:0002LOP_MODIFY_ROWLCX_BOOT_PA3E0000:000002780300000019:0000001b:0003LOP_MODIFY_ROWLCX_BOOT_PA3E0000:000002780400000019:0000001c:00010000:000002780500000019:0000001c:0002LOP_MODIFY_HEADERLCX_PFS0000:000000000600000019:0000001c:000
9、3LOP_SET_BITSLCX.PFS0000:000000000700000019:0000001c:0004LOPJNSERT.ROWSLCX.CLUSTERED0000:000002780800000019:0000001c:0005LOP_SET_BITSLCX.PFS0000:000000000900000019:0000001c:00060000:0000027801000000019:0000001c:0007LCX.PFS0000:0000000001100000019:0000001c:0008LOP INSERT ROWSLCX CLUSTERED0000:0000027
10、80二.实验中出现的问题以及解决方案(对于未解决问题请将问题列出来)除了标题内容以外,该部分内容中还可以写对于实验的些感受,建议,意见等。学会了一些事务解决的操作,理解了运用锁机制、数据库的隔离级别去解决问题。批阅者:批阅日期:实验成绩:批注:i n s ert into c a mpus_card v a 1 u es(2O2 3 0 033, 70)ins e r t in t o icbc_card v al u e s (12023003 1 , u , 10001i n sert i nto i c bc_c a rd v allies, 2 02300 3 2, , 1000)i
11、n sen int o icbc_ca r d v a 1 ues(202 3 00 3 310 0 0)针对以上数据库按照规定完毕下列实验:1 .编写一个事务解决(begin t ran)实现如下的操作:某学号为20230032的学生要从 银行卡中转账200元到校园卡中,若中间出现故障则进行rollback。(15分)代码:u se s t udentbegin t r a n s a c t i on z h ua n z ha n ggodeclared dec i ma 1 (10, 2)sele c t x=balance from i c bc_c a rd where studc
12、ard id=20 2 3 0 03 2,set x=x- 2 00if (x=0)be g inu p date i cbc_ c a rd set b a 1 a n ce=x wh e r e s t udcardid=/ 202 30032u p dat e camp u s c a rd se t b a la n ce=bala nee +200 wh e re s tud c ard i d =202300 3 2conuni t t r a nende Iseb egi nP r i n t余额局限性,不能转账,r o 1 1 ba c k tranend结果显示:studca
13、rddDx/ B4ASQLEXPRESS.student - dbo.icbc.card B47SQLEXPRESS.st- dbo.camstudcar ddbalance50030.002015003220150033250.0070.00npD米NULLNULL2 .针对本题的数据库和表,分别用品体的例子展现四种数据不一致问题:丢失修改、读脏数据、不可反复读和幻读(删除和插入)。(40分,每种数据不一致10分)1).丢失修改:执行两段代码begin tran sactio ndeclare a de c i m a 1 (1 0 , 2 )s el e ct a=balanc efr o
14、 m i c bc_c a rd whe r e stu d ca r d i d= * 202 3 0 03 2 wa i t f o r de 1 ay 00: 00: 05upda t e iebe c a rds et b a lance= a + 1 where studca r di d =, 20 2 30032c o nun i tse 1 e c t * f rom icbc_ cardb egin transactio nd eclare b dec i mal ( 1 0, 2)s elect b=ba 1 ancef r om i c b c_ca r d w h er
15、e st u dcardid=/ 202300 3 2,wai t f o r del a y 00: 0 0:0 5upd ate icbc cardset ba 1 a n c e=b+ 2 where studc a r d i d=202 3 0 032commits e 1 e c t * fr o m icbc_card两段代码执行后的结果:川s结果五消息balance1000.001002.001000.00balance1000.001002.001000.00studcandid icbdd1 26156631- 20150031012i?i懿35” 201500320132
16、0150033 2015003301出现r丢失修改2).读脏数据:b egin tr a nsa c t i o nd eclare b d ecimal( 1 0,2)sei e c t b=bal a neefrom icb c _ c a r d whe r e stud c a r di d =, 2 0 2 30 0 32zup d a te i c b c _car dset balance=b*2 where st u d c ar d id= 2 0230 0 32,begin t r a n sacti o nsei eel* from icbc_cardcomm itrol
17、l backs e lect * from i cb c _car d结果显示:3),不可反复读:be g in tran s actio nselect* f r om iebe cardbe g in t ra n sactiondec lare b d ccim a 1 (10, 2)se 1 e c t b 二 ba lane ef r o m ic b c_ c a r d wher e studcardi d= 2 0 2 3 0 03 2 ,u pdate i c b c _ca r dset b ala n ce=b * 2 wher e st u d c a rdi d =
18、20 2 30 0 32 commitselect * from i cb c _cardc o mm it结果显示:balance删除:be g in t r ansactionselect *f r om iebec cirdbegin trans a ctiondelet e from iebe cardwhere stu d c a rdi d = 2023 0 ()3 2c o mm itse 1 ect *from icbcca r dcommi t结果显示:studcardid icbdd balance插入:be g i n t ran sac t ionselect *fro
19、m ic b c _ca r dbegin tr a ns a ctioninser t into ic b c_ca r d values。2 0230 0 3 4, , 1 0 00)commi ts elect *f r om icbc_ cardcommi t结果显示:1国结果以消息1studcardidicbcidbalance1I 20150031 |20150031011000.0022015003320150033011000.00studcardidicbcidbalance1j 20150031 |20150031011000.00220150034201500340110
20、00.003I2015003320150033011000.003.运用锁机制、数据库的隔离级别等,设计方案分别解决上述丢失修改、读脏数据和不可反复读(或者幻读)的数据不一致问题。(30分,每种数据不一致1。分,提醒可以用spoc k系统存储过程查看当前锁状况)1 ).丢失修改:begin t r a n sa c ti o ndec 1 are b decimal (10,2)se 1 ect b =b a 1 a neefrom ic b c _card w i th (xl o c k )where s tuclcardid=* 202 3 0 0 3 2wa i t f o r del
21、ay 00: 0 0:05update i cbc_cardset b al a ncc= b + 2 where st u d c a rd i d = 2 0230032,commitsei ect * f rom i ebe cardbegin tr a nsact i o nde c lare a decimal(10, 2 )select a=b ala neef rom ic b c card with (xlo c k ) whore stud car did= 202 300 3 2wait for delay 00:0 0: 05u p date ic b c cardset
22、 b alance=a+1 w h ere s t u d c a rdid=, 202 3 0 0 32c o nimitsele ct* f rom i cb c _card尊结果|当消息 studcardid icbdd balance1| 2015003120150031011000.0022015003220150032011003.0032015003320150033011000.00成功加3,解决了丢失修改2 ).读脏数据begin tranu pdat e camp u s_card wi th (x1 o c k )set b a 1 a n c e=ba 1 a nce-20whe r e st u de a r d i d=,2 0 2 3 0 0 3 2waitfo r delay ) 00:00: 05r o 11 b ackbegin t ran