MySQL Transactions
MySQL transactions are mainly used to handle data with a large volume of operations and high complexity. For example, in a personnel management system, when you delete an employee, you need to delete not only the employee's basic profile but also information related to that employee, such as email, articles, and so on; in this way, these database operation statements form a transaction!
In MySQL, a transaction is the execution of a group of SQL statements, which are treated as a single unit of work.
- In MySQL, only databases or tables that use the InnoDB database engine support transactions.
- Transaction processing can be used to maintain database integrity, ensuring that a batch of SQL statements is either all executed or none executed.
- Transactions are used to manage insert、update、delete Statement
In general, a transaction must satisfy four conditions (ACID):: Atomicity (Atomicity, or indivisibility), Consistency (Consistency), Isolation (Isolation, also called independence), Durability (Durability)。
-
Atomicity:All operations in a transaction are either all completed or none completed; they will not stop at some intermediate point. If an error occurs during execution, the transaction will be rolled back (Rollback) to the state before the transaction began, as if the transaction had never been executed.
-
Consistency:Before a transaction begins and after it ends, database integrity has not been violated. This means the data written must fully comply with all predefined rules, including the accuracy of the data, its connectivity, and the ability of the subsequent database to spontaneously complete the scheduled work.
-
Isolation:A database allows multiple concurrent transactions to read, write, and modify its data simultaneously. Isolation can prevent data inconsistency caused by interleaved execution when multiple transactions run concurrently. Transaction isolation is divided into different levels, including Read uncommitted, Read committed, Repeatable read, and Serializable.
-
Durability:After transaction processing ends, the modifications to the data are permanent and will not be lost even in the event of a system failure.
Under the default settings of the MySQL command line, transactions are auto-committed, meaning a COMMIT operation is performed immediately after an SQL statement is executed. Therefore, to explicitly start a transaction you must use the command BEGIN or START TRANSACTION, or execute the command SET AUTOCOMMIT=0 to disable auto-commit for the current session.
Transaction control statements:
-
BEGIN or START TRANSACTION explicitly starts a transaction;
-
COMMIT can also use COMMIT WORK, but the two are equivalent. COMMIT commits the transaction and makes all modifications made to the database permanent;
-
ROLLBACK can also use ROLLBACK WORK, but the two are equivalent. A rollback ends the user's transaction and undoes all uncommitted changes in progress;
-
SAVEPOINT identifier: SAVEPOINT allows a savepoint to be created within a transaction; a transaction can have multiple SAVEPOINTs;
-
RELEASE SAVEPOINT identifier deletes a savepoint of a transaction; when the specified savepoint does not exist, executing this statement throws an exception;
-
ROLLBACK TO identifier rolls the transaction back to the marked point;
-
SET TRANSACTION is used to set the transaction isolation level. The isolation levels provided by the InnoDB storage engine are READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, and SERIALIZABLE.

There are two main methods of MySQL transaction processing:
1. Implement using BEGIN, ROLLBACK, COMMIT
- BEGIN or START TRANSACTION: used to start a transaction.
- ROLLBACK Transaction rollback, cancels previous changes.
- COMMIT: transaction confirmation, commits the transaction, making the changes permanent.
2. Directly use SET to change MySQL's auto-commit mode:
- SET AUTOCOMMIT=0 Disable auto-commit
- SET AUTOCOMMIT=1 Enable auto-commit
BEGIN or START TRANSACTION -- used to start a transaction:
BEGIN; -- 或者使用 START TRANSACTION;
COMMIT -- used to commit the transaction, saving all changes permanently to the database:
COMMIT;
ROLLBACK -- used to roll back the transaction, undoing all changes made since the last commit:
ROLLBACK;
SAVEPOINT -- used to set a savepoint within a transaction so that you can later roll back to that point:
SAVEPOINT savepoint_name;
ROLLBACK TO SAVEPOINT -- used to roll back to a previously set savepoint:
ROLLBACK TO SAVEPOINT savepoint_name;
The following is a simple example of a MySQL transaction:
Example
-- Start the transaction
START
TRANSACTION
;
-- Execute some SQL statements
UPDATE
accounts
SET
balance
=
balance
100
WHERE
user_id
=
1
;
UPDATE
accounts
SET
balance
=
balance
100
WHERE
user_id
=
2
;
-- Decide whether to commit or roll back
IF
(
Conditional
)
THEN
COMMIT;
-- Commit the transaction
ELSE
ROLLBACK
;
-- Roll back the transaction
END
IF
;
Example
A simple transaction example:
Transaction test
mysql
use
RUNOOB
;
Database
changed
mysql
CREATE
TABLE
runoob_transaction_test
(
id
int
(
5
)
)
engine
=
innodb
;
Create a data table
Query
OK
,
0
rows
affected
(
0.04
sec
)
mysql
select
from
runoob_transaction_test
;
Empty
set
(
0.01
sec
)
mysql
begin
;
Start a transaction
Query
OK
,
0
rows
affected
(
0.00
sec
)
mysql
insert
into
runoob_transaction_test
value
(
5
)
;
Query
OK
,
1
rows
affected
(
0.01
sec
)
mysql
insert
into
runoob_transaction_test
value
(
6
)
;
Query
OK
,
1
rows
affected
(
0.00
sec
)
mysql
commit
;
Commit a transaction
Query
OK
,
0
rows
affected
(
0.01
sec
)
mysql
select
from
runoob_transaction_test
; +------+ |
id
| +------+ |
5
| |
6
| +------+
2
rows
in
set
(
0.01
sec
)
mysql
begin
;
Start a transaction
Query
OK
,
0
rows
affected
(
0.00
sec
)
mysql
insert
into
runoob_transaction_test
values
(
7
)
;
Query
OK
,
1
rows
affected
(
0.00
sec
)
mysql
rollback
;
Rollback
Query
OK
,
0
rows
affected
(
0.00
sec
)
mysql
select
from
runoob_transaction_test
;
No data was inserted because of the rollback
+------+ |
id
| +------+ |
5
| |
6
| +------+
2
rows
in
set
(
0.01
sec
)
mysql