博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
oracle 事务测试
阅读量:4309 次
发布时间:2019-06-06

本文共 3288 字,大约阅读时间需要 10 分钟。

此文章是根据官方改变

模拟帐户转账流程
1.JOHN帐户扣除-DAVID帐户增加-记录日志-事务提交
三个操作必须全部完成此事务才完成,否则失败
创建帐户余额表自增字段
自增序列
create  
sequence  
saving_seq  
increment  
by  
1  
start  
with  
1  
maxvalue  
999999999999999999  
nocycle  
cache  
20
;
创建支票表自增字段
自增序列;
create  
sequence  
check_seq  
increment  
by  
1  
start  
with  
1  
maxvalue  
999999999999999999  
nocycle  
cache  
20
;
创建日志记录自增字段自增序列;l
create
 
sequence log
_seq 
increment
 
by
 
1 
start
 
with
 
1 
maxvalue
 
999999999999999999
 
nocycle 
cache
 
20
 ;
 
2.创建余额表saving_accounts
create  
table  
saving_accounts
(account_id  
int  
primary  
key  
,
account_name  
varchar2
(  
20
) ,
pay  
number
(  
15
,
2  
) );
comment  
on  
table  
saving_accounts  
is
   
'帐户余额表'  
;
comment  
on  
column  
saving_accounts.account_id  
is  
'帐户ID'  
;
comment  
on  
column  
saving_accounts.account_name  
is  
'帐户名称'  
;
comment  
on  
column  
saving_accounts.pay  
is  
'帐户余额'  
;
 
创建支票余额表
create  
table  
checking_accounts
(check_id  
int  
primary  
key  
,
check_name  
varchar2
(  
20
) ,
check_pay  
number
(  
15
,
2  
) );
comment  
on  
table  
checking_accounts  
is
   
'支票帐户余额表'  
;
comment  
on  
column  
checking_accounts.check_id  
is  
'支票帐户ID'  
;
comment  
on  
column  
checking_accounts.check_name  
is  
'支票帐户名称'  
;
comment  
on  
column  
checking_accounts.check_pay  
is  
'支票帐户余额'  
;
 
创建转账日志表
create  
table  
log_accounts
(log_id  
int  
primary  
key  
,
log_date  
date
   
default  
(
sysdate  
)  
not  
null
,
account_id  
int  
not  
null  
,
check_id  
int  
not  
null  
,
change_pay  
number
(  
15
,
2  
) );
comment  
on  
table  
log_accounts  
is
   
'转账日志表'  
;
comment  
on  
column  
log_accounts.log_id  
is  
'转账日志ID'  
;
comment  
on  
column  
log_accounts.log_date  
is  
'转账日期'  
;
comment  
on  
column  
log_accounts.account_id  
is  
'转账帐户ID'  
;
comment  
on  
column  
log_accounts.check_id  
is  
'支票帐户ID'  
;
comment  
on  
column  
log_accounts.change_pay  
is  
'支票帐户余额'  
;
 
查询建表是否成功
select  
*  
from  
log_accounts;
select  
*  
from  
saving_accounts;
select  
*  
from  
checking_accounts;
 
3.插入数据
插入 
saving_accounts
insert into saving_accounts
values(
saving_seq.nextval,'john',1000);
insert into saving_accounts
values(
saving_seq.nextval,'david',2000);
insert into saving_accounts
values(
saving_seq.nextval,'alex',3000);
insert into saving_accounts
values(
saving_seq.nextval,'lily',5000);
insert into saving_accounts
values(
saving_seq.nextval,'joe',1500);
commit;
 
插入checking_accounts
insert into 
checking_accounts
values
( 
check_seq
.nextval,'john',2000);
insert into 
checking_accounts
values
( 
check_seq
.nextval,'david',500);
insert into 
checking_accounts
values
( 
check_seq
.nextval,'alex',2000);
insert into 
checking_accounts
values
( 
check_seq
.nextval,'lily',1500);
insert into 
checking_accounts
values
( 
check_seq
.nextval,'joe',4000);
commit;
 
用户转账的步骤(转账到支票)
如john
防止事务失败可以加入异常处理
begin
savepoint sp1
--SET TRANSACTION NAME 'account_update'; 可以设置事务名称transaction name
--减少john 帐户余额200转入到david
update saving_accounts a
set pay=pay-200
where a.account_id=1;
--SAVEPOINT after_update_savind_accounts; 设置rollback点
--增加david支票余额
update checking_accounts b
set check_pay=check_pay+200
where b.check_id=2;
--写入日志表
insert into 
log_accounts(log_id,account_id,check_id,change_pay)
values(log
_seq.nextval,1,2,200);
--ROLLBACK TO SAVEPOINT  after_update_savind_accounts; 回滚到saingpoint  after_update_savind_accounts
--rollback 将回滚事务account_update
exception
  when others then
      rollback to savepoint sp1;
end;
commit work;
 
总结:
如果在一个 SQL 语句在执行过程中发生了错误,那么此语句对数据库产生的影响将被回滚(roll back)。回滚后就如同此语句从未执行过。

转载于:https://www.cnblogs.com/shawnloong/p/3295503.html

你可能感兴趣的文章
HTTP协议
查看>>
HTTPS
查看>>
git add . git add -u git add -A区别
查看>>
apache下虚拟域名配置
查看>>
session和cookie区别与联系
查看>>
PHP 实现笛卡尔积
查看>>
Laravel中的$loop
查看>>
CentOS7 重置root密码
查看>>
Centos安装Python3
查看>>
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Laravel 操作redis的各种数据类型
查看>>
Laravel框架学习笔记之任务调度(定时任务)
查看>>
laravel 定时任务秒级执行
查看>>
浅析 Laravel 官方文档推荐的 Nginx 配置
查看>>
Swagger在Laravel项目中的使用
查看>>
Laravel 的生命周期
查看>>
CentOS Docker 安装
查看>>
Nginx
查看>>
Navicat远程连接云主机数据库
查看>>