Transactions are used to manipulate multiple SQL for some operations as atomic, and once an error occurs, it can be rolled back to its original state, guaranteeing database data integrity. To illustrate:
CREATE table user2 (ID int primary key auto_increment,name char (+), balance int); INSERT into User2 (name,balance) VALUES (' WSB ', +), (' Egon ', +), (' YSB ', +); #出现异常, roll back to the initial state start transaction;update User2 set balance=900 where name= ' WSB '; #买支付100元update User2 set balance=1010 where name= ' Egon '; #中介拿走10元uppdate User2 set balance=1090 where name= ' YSB '; #卖家拿到90元, an exception did not get rollback;mysql> select * FROM user;+----+------+---------+| ID | name | Balance |+----+------+---------+| 1 | WSB | | | 2 | Egon | | | 3 | YSB | |+----+------+---------+rows in Set (0.00 sec) #原子操作start transaction;update user2 set balance=900 where name= ' WSB '; #买支付100元update User2 set balance=1010 where name= ' Egon '; #中介拿走10元update User2 set balance=1090 where name= ' YSB '; #卖家拿到90元commit;
Here is the action: when Ip_return_code is 1 o'clock, it represents an exception and immediately rolls back. When it is 2 o'clock, a warning appears, and the original state is immediately rolled back. 0 indicates success
Delimiter//create PROCEDURE b6 (out p_return_code tinyint) BEGIN DECLARE exit handler for SqlException BEGIN --ERROR Set p_return_code = 1; Rollback; END; DECLARE exit handler for sqlwarning BEGIN --WARNING Set p_return_code = 2; Rollback; END; START TRANSACTION; INSERT into Blog (name,sub_time) VALUES (' yyy ', now ()); COMMIT; --SUCCESS Set p_return_code = 0; #0代表执行成功END//delimiter; set @res =123;call b6 (@res); select @res;
When the Select @res returns 0, the insert is executed successfully. At this point, the query can see that YYY has been successfully inserted into the blog table
MySQL's business