学习笔记(50):mySQL数据库开发教程-在存储过程中使用事务

立即学习:https://edu.csdn.net/course/play/4364/77197?utm_source=blogtoedu

表级锁,对于不支持事务的引擎,为了实现隔离手动添加表级锁;

对于支持事务的引擎上的表,会根据操作自动加锁。

create table ta
(
n char(1) not null primary key,
i int
) engine=innodb ---------指定数据库引擎,因为MYISAM不支持事务

insert into ta values('a', 10), ('b', 20)

create table tb
(
n char(1) not null primary key,
i int
) engine=innodb

insert into tb values('a', 10), ('b', 20), ('c', 30), ('d', 40)

# 
create procedure move(nn char(1))
begin
declare t_error int default 0;
declare continue handler for SQLEXCEPTION set t_error=1;
start transaction;
insert into ta select * from tb where n==nn;
delete from tb where n=nn;
if t_error = 1 then 
    rollback;
    else
    commit;
end if;
end

call move('a')
# 转账b->a
create procedure btoa(nn char(1), m int)
begin
declare mm int;
start transaction;
update ta set i=i+m where n = nn;
update tb set i=i-m where n=nn;
select i into mm from tb where n=nn;
if mm<0 then 
    rollback;
    else
    commit;
end if;
end

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值