MySQL Transaction implementation method steps

Source: Internet
Author: User

Requirements Description:

Case background: In the course of bank transfer, accidents are unavoidable. To avoid unnecessary losses caused by an accident, the transaction is handled in the same way:

A account has an existing balance of $1000 and a transfer of $500 to a balance of 200 B accounts. Possible for a reason:

A account has an error while deducting the transfer amount, using a transaction rollback to return to the initial state

A after the account has successfully deducted the transfer amount, the B account adds the transfer amount error, using the transaction rollback to the initial state

Tip: First build the data table account, the field includes the name (username), the balance (money), and then use the transaction to handle the above two cases respectively.

[SQL]View PlainCopy 
  1. #创建账户表
  2. CREATE TABLE IF not EXISTS account (
  3. ID INT (one)not NULL auto_increment PRIMARY KEY,
  4. Username VARCHAR (+)not NULL,
  5. Money DECIMAL (9,2)
  6. ) Engine=innodb;
  7. #插入用户数据
  8. INSERT into account ( Username,money)VALUES (' A ', 1000.00);
  9. INSERT into account ( Username,money)VALUES (' B ', 200.00);
  10. /* Transaction Processing */
  11. # A account remittance failed
  12. SELECT * from account ;
  13. #第一步 Turn off transaction autocommit mode
  14. SET autocommit=0;
  15. #第二步 Start a transaction
  16. START TRANSACTION;
  17. #第三步 Find the remittance failed, rollback the transaction ROLLBACK | | Remittance successfully commits the event
  18. #假设语法错误
  19. UPDATE account SET money=money-500 WHERE username=' A ';
  20. SELECT * from account ;
  21. UPDATE account SET money=money+200 WHERE username=' B ';
  22. ROLLBACK;
  23. #第四步 Restore the automatic submission of MySQL database
  24. SET autocommit=1;
  25. SELECT * from account ;
  26. /*B Receive remittance failure */
  27. SELECT * from account;
  28. SET autocommit = 0;
  29. START TRANSACTION;
  30. UPDATE account SET money=money-500 WHERE username=' A ';
  31. SELECT * from account ;
  32. #假设语法错误
  33. UPDATE account SET money=money+200 WHERE username =' B ';
  34. ROLLBACK;
  35. SET autocommit = 1;
  36. SELECT * from account ;
  37. #清除数据
  38. <pre name="code" class="sql" >TRUNCATE Account;

MySQL Transaction implementation method steps

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.