MySQL transaction operation examples and precautions

Source: Internet
Author: User
Tags savepoint

This article mainly introducesMySQL transactionThe following describes some operation examples and precautions.

Transaction Features:

  • Atomicity (Atomicity)
  • Consistency (stability, Consistency)
  • Isolation (Isolation)
  • Durability)

Note:Transactions are only valid for statements that affect data.

Show engines // view the Data Engine supported by mysql locks.

MyISAM does not support transactions. InnoDB supports transactions.

By default, MySQL runs in the automatic submission mode, which means that no small command is executed as a single command.

To enable mysql to support transactions, you only need to modify the Data Engine alter table person type = INNODB ).

Use the start transaction or begin command to start a transaction, and use commit or rollback to end the transaction.

The end of a transaction: Except for commit, rollback ends and DDL or DCL statements ends.

Save point: you can use the savepoint name command in the transaction to set some save points. In the future, when you use rollback to savepoint name to end a transaction, data before name is saved, subsequent data is not saved.

Mysql uses transaction keywords

  • Begin // open a transaction.
  • Commit // submit it to the database.
  • Rollback // cancel the operation.
  • Savepoint // save, partially cancel, partially submit.
  • Alter table person type = INNODB // modify the Data Engine.

Example:

 
 
  1. begin  
  2.  
  3. update person set name='efgh' where id =10 
  4.  
  5. select * from person  
  6.  
  7. rollback  
  8.  
  9. select * from person 

Example:

 
 
  1. alter table person type=INNODB 
  2.  
  3. begin  
  4.  
  5. update person set name='efgh' where id =10 
  6.  
  7. select * from person  
  8.  
  9. commit  
  10.  
  11. select * from person  
  12.  
  13. begin  
  14.  
  15. delete from person where id=21 
  16.  
  17. update person set name='efgh' where id =10 
  18.  
  19. commit/rollback 

The Save point must be used for the preceding submission.

Note:

1. You can only cancel rollback to savepoint p1 from a storage point.

2. A commit to savepoint p2 file cannot be submitted. // The incorrect syntax is as follows.

3. Finally, commit does not submit uncanceled storage points to the data.

Example of transaction retention point:

 
 
  1. begin;  
  2.  
  3. update score set score=40 where scoreid=1;  
  4.  
  5. savepoint s1;  
  6.  
  7. update score set score=50 where scoreid=2;  
  8.  
  9. select * from score;  
  10.  
  11. rollback to savepoint s1;  
  12.  
  13. select * from score;  
  14.  
  15. commit; 

Here is an introduction to MySQL transaction operations. If you want to know

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.