Transaction Management in MySQL

This section provides tutorial examples on how MySQL supports transaction management with the InnoDB and BDB storage engine.

MySQL support of transaction management follows the following rules:

Statements related to transaction management:

SET AUTOCOMMIT = 0 | 1;
START TRANSACTOIN;
COMMIT;
ROLLBACK;

Note that:

Here is a simple test code on the default transaction:

-- Rollback.sql
-- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
--
SET AUTOCOMMIT = 0;
USE test;
DROP TABLE IF EXISTS User;
CREATE TABLE User (Login VARCHAR(8), Password CHAR(8)) ENGINE=InnoDB;
INSERT INTO User VALUES ('bill','83BF9598');
COMMIT;
INSERT INTO User VALUES ('jack','8IS3KOXW');
ROLLBACK;
INSERT INTO User VALUES ('john','FC93846F');
COMMIT;
INSERT INTO User VALUES ('mike','86D47E70');

Run Rollback.sql on MySQL 5.7 server, you will get:

C:\herong>set "MySQL=\Program Files\MySQL\MySQL Server 5.7"
C:\herong>"%MySQL%\bin\mysql" --user=root --password=TopSecret test 
   < Rollback.sql

Now check the table again in a new session:

C:\herong>"%MySQL%\bin\mysql" --user=root --password=TopSecret test
mysql> SELECT * FROM user;
+-------+----------+
| Login | Password |
+-------+----------+
| bill  | 83BF9598 |
| john  | FC93846F |
+-------+----------+

Observe that:

Here is another simple test code on non-default transactions:

-- Transaction.sql
-- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
--
SET AUTOCOMMIT = 1;
DROP TABLE IF EXISTS User;
CREATE TABLE User (Login VARCHAR(8), Password CHAR(8)) ENGINE=InnoDB;
START TRANSACTION;
INSERT INTO User VALUES ('bill','83BF9598');
COMMIT;
INSERT INTO User VALUES ('jack','8IS3KOXW');
ROLLBACK;
INSERT INTO User VALUES ('john','FC93846F');
COMMIT;
INSERT INTO User VALUES ('mike','86D47E70');
START TRANSACTION;
INSERT INTO User VALUES ('user1','pass1');
START TRANSACTION;
INSERT INTO User VALUES ('user2','pass2');
START TRANSACTION;
INSERT INTO User VALUES ('user3','pass3');
ROLLBACK;
INSERT INTO User VALUES ('user4','pass4');
ROLLBACK;
INSERT INTO User VALUES ('user5','pass5');
COMMIT;
SELECT 'User table:' AS '---';
SELECT * FROM User;

Output:

---
User table:
Login   Password
bill    83BF9598
jack    8IS3KOXW
john    FC93846F
mike    86D47E70
user1   pass1
user2   pass2
user4   pass4
user5   pass5

Observe that:

Last update: 2015.

Table of Contents

 About This Book

 Introduction of SQL

 MySQL Introduction and Installation

 Introduction of MySQL Programs

 Perl Programs and MySQL Servers

 PHP Programs and MySQL Servers

 Java Programs and MySQL Servers

 Datatypes and Data Literals

 Operations and Expressions

 Character Strings and Bit Strings

 Commonly Used Functions

 Table Column Types for Different Types of Values

 Using DDL to Create Tables and Indexes

 Using DML to Insert, Update and Delete Records

 Using SELECT to Query Database

Transaction Management and Isolation Levels

 What Is a Transaction?

Transaction Management in MySQL

 Transaction Isolation Levels

 "Read Uncommitted" Isolation Level

 "Read Committed" Isolation Level

 "Repeatable Read" Isolation Level

 Locks Used in MySQL

 Defining and Calling Stored Procedures

 Variables, Loops and Cursors Used in Stored Procedures

 Outdated Tutorials

 References

 PDF Printing Version