We can define the SavePoint (savepoint) during the MySQL transaction process and roll back to the state before the specified savepoint.
The syntax for defining a savepoint and rolling back to the state before the specified savepoint is as follows.
- Define save Point---savepoint save name;
- Roll back to the specified savepoint---ROLLBACK to savepoint save name:
The following demonstration will insert 3 consecutive data into the table user, define a savepoint after inserting the 2nd data, and finally see if you can roll back to this savepoint.
1. View the data in the user table
- Mysql> select * from user;
- +-----+----------+-----+------+
- | Mid | name | SCX | Word |
- +-----+----------+-----+------+
- | 1 | Zhangsan | 0 | NULL |
- | 2 | Wangwu | 1 | NULL |
- +-----+----------+-----+------+
- 2 rows in Set (0.05 sec)
2. mysql Transaction start
- Mysql> BEGIN;
- Query OK, 0 rows Affected (0.00 sec)
3. Insert 2 data into the table user
- mysql> INSERT into User VALUES (' 3 ', ' One ', ' 0 ', ');
- Query OK, 1 row affected (0.08 sec)
- mysql> INSERT into User VALUES (' 4, ' both ', ' 0 ', ');
- Query OK, 1 row Affected (0.00 sec)
- Mysql> select * from user;
- +-----+----------+-----+------+
- | Mid | name | SCX | Word |
- +-----+----------+-----+------+
- | 1 | Zhangsan | 0 | NULL |
- | 2 | Wangwu | 1 | NULL |
- | 3 | One | 0 | |
- | 4 | both | 0 | |
- +-----+----------+-----+------+
- 4 rows in Set (0.00 sec)
4. Specify the savepoint and save the name as test
- mysql> savepoint test;
- Query OK, 0 rows Affected (0.00 sec)
5. Insert the 3rd data into the table user
- mysql> INSERT into User VALUES (' 5 ', ' Three ', ' 0 ', ');
- Query OK, 1 row Affected (0.00 sec)
- Mysql> select * from user;
- +-----+----------+-----+------+
- | Mid | name | SCX | Word |
- +-----+----------+-----+------+
- | 1 | Zhangsan | 0 | NULL |
- | 2 | Wangwu | 1 | NULL |
- | 3 | One | 0 | |
- | 4 | both | 0 | |
- | 5 | Three | 0 | |
- +-----+----------+-----+------+
- 5 rows in Set (0.02 sec)
6. Roll back to save point test
- Mysql> ROLLBACK to savepoint test;
- Query OK, 0 rows affected (0.31 sec)
- Mysql> select * from user;
- +-----+----------+-----+------+
- | Mid | name | SCX | Word |
- +-----+----------+-----+------+
- | 1 | Zhangsan | 0 | NULL |
- | 2 | Wangwu | 1 | NULL |
- | 3 | One | 0 | |
- | 4 | both | 0 | |
- +-----+----------+-----+------+
- 4 rows in Set (0.00 sec)
We can see that the record inserted after the save point test is not displayed, that is, the success group rolled to the state before the save point test was defined. The use of SavePoint enables the ability to commit only part of the processing in a transaction.
Article source page programming, reprint please specify Source: http://uphtm.com/database/192.html
MySQL transaction partial rollback-rollback to the specified save point