MySql transaction usage and example _ MySQL

Source: Internet
Author: User
Tags savepoint
In MySql, transactions are a series of steps in a logical unit of work. Transactions are used to ensure the security of data operations.

Transaction features:

1. Atomicity (Atomicity)
2. Consistency (stability and Consistency)
3. Isolation (Isolation)
4. Durability (reliability)

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

Show engines // view data engines 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:In addition to commit, rollback ends, and DDL or DCL statements end.

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

Mysql uses the transaction keyword begin // to 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 data engine

Example:

beginupdate person set name='efgh' where id =10select * from personrollbackselect * from person

Example:

alter table person type=INNODBbeginupdate person set name='efgh' where id =10select * from personcommitselect * from personbegindelete from person where id=21update person set name='efgh' where id =10commit/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. // incorrect syntax
3. finally, commit does not submit uncanceled storage points to the data.

Example of transaction retention point:

begin;update score set score=40 where scoreid=1;savepoint s1;update score set score=50 where scoreid=2;select * from score;rollback to savepoint s1;select * from score;commit;

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.