MySQL must know must know the study notes Chapter 26 Management Transaction Processing

Not all engines support transaction processing. MyISAM does not support, InnoDB supports.

Transaction processing can be used to maintain the integrity of the database. It ensures that batches of MySQL operations are either completely executed or not executed at all. If no error occurs, the entire set of statements are written to the database table. If an error occurs, a rollback is performed to restore the database to a known and safe state.

Terminology:
1. Transaction: a set of SQL statements.
2. Rollback: Cancel the specified SQL statement.
3. Submit: Write the stored SQL statement result to the database table.
4. Reserved point: Refers to the dying placeholder set in the transaction processing, which can be released for rollback.

Identify the beginning of the transaction:

START TRANSACTION;

Roll back the MySQL statement:

SELECT语句;    -- 查看结果集
START TRANSACTION;
DELETE语句;
SELECT语句;    -- 会发现指定行被删除
ROLLBACK;    -- 回退删除语句
SELECT语句;    -- 删除行又回来了

ROLLBACK can only be used within a transaction.

The ROLLBACK statements are INSERT, UPDATE, and DELETE. CREATE and DROP statements can be used in transaction processing blocks, but they will not be undone when the rollback is executed.

Generally, MySQL statements are executed and written directly against database tables, and are implicitly submitted. During transaction processing, the commit needs to be displayed:

START TRANSACTION;
DELETE语句1;
DELETE语句2;
COMMIT;

When the DELETE statement 2 fails, the entire transaction will be cancelled.

After COMMIT or ROLLBACK, the transaction will be closed automatically.

If an error occurs in some transactions, you do not need to roll back all of them. You can place a placeholder in a certain position in the transaction block. If you need to roll back, you can roll back to a certain placeholder.

Create a placeholder:

SAVEPOINT savePointName;

Each reserved point has a unique placeholder name to identify it, and fall back to a certain reserved point:

ROLLBACK TO savePointName

Any number of reserved points can be set.

The reserved point is automatically released after the transaction is completed. In MySQL 5 and later, you can use RELEASE SAVEPOINT to display the released reserved point.

Change MySQL default auto-commit to not auto-commit changes:

SET autocommit = 0;

The aotucommit flag is for each connection and not the server.

Guess you like

Origin blog.csdn.net/tus00000/article/details/111713884