Home > Database > Mysql Tutorial > A brief discussion on MySQL transaction management (basics)

A brief discussion on MySQL transaction management (basics)

青灯夜游
Release: 2019-02-26 10:53:03
forward
2534 people have browsed it

The content of this article is a brief discussion of MySQL transaction management (basics). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Transaction processing is used to maintain the integrity of the database and ensure that mysql operations either succeed or fail (myisam does not support transactions)

1. Keywords

  1. Transaction refers to a set of SQL statements;

  2. Rollback refers to the process of undoing the specified SQL statement;

  3. Commit (commit) refers to writing the unstored SQL statement results into the database table;

  4. The savepoint (savepoint) refers to the settings during transaction processing A temporary place-holder to which you can issue a rollback (as opposed to rolling back the entire transaction).

2. Use rollback

select * from orderitems;
START TRANSACTION;
DELETE FROM orderitems;
select * from orderitems;
ROLLBACK;
select * from orderitems;
Copy after login

3. Use commit

START TRANSACTION;
DELETE FROM orderitems where order_num = 20010;
DELETE FROM orders WHERE order_num = 20010;
COMMIT
Copy after login

Assume that the second deletion fails, roll back, and undo the statements in the transaction processing block

4. Use retention points

Complex transaction processing may require partial commit or rollback.
In order to support rolling back part of the transaction, placeholders must be placed at appropriate locations in the transaction block. This way, if you need to roll back, you can fall back to a placeholder.
These placeholders are called retention points. To create a placeholder, use SAVEPOINT as follows

Create a retention point

SAVEPOINT delete1
Copy after login

Fallback to a retention point

ROLLBACK TO delete1
Copy after login

#tips: The more reservation points, the better, it is convenient and flexible to use, but there is no need to come just come! Everything is done in moderation

Release retention points

1. The retention points are automatically released after the transaction is completed (execute a ROLLBACK or COMMIT)

2. Release savepoint delete1Clearly release the retention point

5. Change the default to commit behavior

mysql automatically commits all changes.

Do not automatically submit changes

set autocommit = 0;
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of A brief discussion on MySQL transaction management (basics). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:https://segmentfault.com/a/1190000018282782
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template