西北工业大学大数据库实验报告材料7
- 格式:doc
- 大小:197.00 KB
- 文档页数:10
假设学校允许学生将银行卡和校园卡进行绑定,在student数据库中有如下的基本表,其中校园卡编号cardid即为学生的学号:
icbc_card(studcardid,icbcid,balance) //校园卡ID,工行卡ID,银行卡余额 campus_card(studcardid,balance) //校园卡ID,校园卡余额创建数据库代码如下:
use student
create table campus_card
( studcardid Char(8),
balance Decimal(10,2)
)
create table icbc_card
( studcardid Char(8),
icbcid Char(10),
lance Decimal(10,2),
)
示例数据如下:
insert into campus_card values('20150031', 30)
insert into campus_card values('20150032', 50)
insert into campus_card values('20150033', 70)
insert into icbc_card values('20150031','2015003101', 1000)
insert into icbc_card values('20150032','2015003201', 1000)
insert into icbc_card values('20150033','2015003301', 1000)
针对以上数据库按照要求完成下列实验:
1.编写一个事务处理(begin tran)实现如下的操作:某学号为20150032的学生要从银
行卡中转账200元到校园卡中,若中间出现故障则进行rollback。(15分)
代码:
use student
begin transaction zhuanzhang
go
declare x decimal(10, 2)
select x=balance from icbc_card where studcardid='20150032'
set x=x-200
if(x>=0)
begin
update icbc_card set balance=x where studcardid='20150032'
update campus_card set balance=balance+200 where studcardid='20150032' commit tran
end
else
begin
print'余额不足,不能转账'
rollback tran
end
结果显示:
2.针对本题的数据库和表,分别用具体的例子展现四种数据不一致问题:丢失修改、
读脏数据、不可重复读和幻读(删除和插入)。(40分,每种数据不一致10分)
1).丢失修改:
执行两段代码
begin transaction
declare a decimal(10,2)
select a=balance
from icbc_card where studcardid='20150032'
waitfor delay '00:00:05'
update icbc_card
set balance=a+1 where studcardid='20150032'
commit
select * from icbc_card
-------------------------------------
begin transaction
declare b decimal(10,2)
select b=balance
from icbc_card where studcardid='20150032' waitfor delay '00:00:05'
update icbc_card
set balance=b+2 where studcardid='20150032' commit
select * from icbc_card
两段代码执行后的结果:
出现了丢失修改
2).读脏数据:
begin transaction
declare b decimal(10,2)
select b=balance
from icbc_card where studcardid='20150032' update icbc_card
set balance=b*2 where studcardid='20150032'
begin transaction
select * from icbc_card
commit
rollback
select * from icbc_card
结果显示:
3).不可重复读:
begin transaction
select * from icbc_card
begin transaction
declare b decimal(10,2)
select b=balance
from icbc_card where studcardid='20150032' update icbc_card
set balance=b*2 where studcardid='20150032' commit
select * from icbc_card
commit
结果显示:
4).幻读
删除:
begin transaction