Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MySQL Transactions

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 general, a transaction must satisfy four conditions (ACID):: Atomicity (Atomicity, or indivisibility), Consistency (Consistency), Isolation (Isolation, also called independence), Durability (Durability)。

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:

There are two main methods of MySQL transaction processing:

1. Implement using BEGIN, ROLLBACK, COMMIT

2. Directly use SET to change MySQL's auto-commit mode:

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

Example of using transactions in PHP

MySQL ORDER BY Test: