mysql------事务

事务用于将某些操作的多个SQL作为原子性操作,也就是这些sql语句要么同时成功,要么都不成功,事务的其他特性在我第一篇博客关于事务的介绍里面有,这里就不多做介绍啦,一旦有某一个出现错误,即可回滚到原来的状态,从而保证数据库数据完整性。

    简单来说:我给一个姑娘转账,姑娘那儿收到了200,你的账户上扣了200,这两个操作是不是两个sql语句,这两个sql语句是你的应用程序发给mysql服务端的,并且这两个sql语句都要一起执行,不然数据就错了,你想想是不是。并且如果你通过应用程序发送这两条sql的时候,由于网络问题,你只发送了一个sql过来,那只有一个账户改了数据,另外一个没改,那数据是不是就出错了啊。这就是事务要完成的事情。

create table user(
id int primary key auto_increment,
name char(32),
balance int
);

insert into user(name,balance)
values
('wsb',1000),
('chao',1000),
('ysb',1000);

#原子操作
start transaction;
update user set balance=900 where name='wsb'; #买支付100元
update user set balance=1010 where name='chao'; #中介拿走10元
update user set balance=1090 where name='ysb'; #卖家拿到90元
commit;  #只要不进行commit操作,就没有保存下来,没有刷到硬盘上
 
#出现异常,回滚到初始状态
start transaction;
update user set balance=900 where name='wsb'; #买支付100元
update user set balance=1010 where name='chao'; #中介拿走10元
uppdate user set balance=1090 where name='ysb'; #卖家拿到90元,出现异常没有拿到
rollback;  #如果上面三个sql语句出现了异常,就直接rollback,数据就直接回到原来的状态了。但是执行了commit之后,rollback这个操作就没法回滚了
#我们要做的是检测这几个sql语句是否异常,没有异常直接commit,有异常就rollback,但是现在单纯的只是开启了事务,但是还没有说如何检测异常,我们先来一个存储过程来捕获异常,等我们学了存储过程,再细说存储过程。
commit;

#通过存储过程来捕获异常:(shit!,写存储过程的是,注意每一行都不要缩进!!!按照下面的缩进来写,居然让我翻车了!!!我记住你了~~~),我的代码直接黏贴就能用。

delimiter //
create PROCEDURE p5()
BEGIN
DECLARE exit handler for sqlexception  
BEGIN 
rollback; 
END;
START TRANSACTION; 
update user set balance=900 where name='wsb'; #买支付100元
update user set balance=1010 where name='chao'; #中介拿走10元
#update user2 set balance=1090 where name='ysb'; #卖家拿到90元
update user set balance=1090 where name='ysb'; #卖家拿到90元
COMMIT;
END //
delimiter ;
 

mysql> select * from user;
+----+------+---------+
| id | name | balance |
+----+------+---------+
|  1 | wsb  |    1000 |
|  2 | chao |    1000 |
|  3 | ysb  |    1000 |
+----+------+---------+
rows in set (0.00 sec)

原文地址:https://www.cnblogs.com/steve214/p/10111215.html