MySQL's business

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.