We can define the SAVEPOINT during mysql transaction processing, and then roll back to the status before the specified save point. The syntax for defining the save point and rolling back to the status before the specified save point is as follows. Define the save point --- SAVEPOINT to save the name; roll back to the specified save point --- ROLLBACKTOSAVEPOINT to save the name:
We can define the SAVEPOINT during mysql transaction processing, and then roll back to the status before the specified save point. The syntax for defining the save point and rolling back to the status before the specified save point is as follows. Define the save point --- savepoint to save the name; roll back TO the specified save point --- rollback to savepoint to save the name: The following shows how TO save the name TO the table user
We can define the SAVEPOINT during mysql transaction processing, and then roll back to the status before the specified save point.
The syntax for defining the save point and rolling back to the status before the specified save point is as follows.
- Define save point --- SAVEPOINT save name;
- Roll back TO the specified storage point --- rollback to savepoint:
The following example inserts three data records into the table user, defines a save point after inserting 2nd data records, and finally checks whether the data can be rolled back to this save point.
1. View 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. Start mysql transaction
- Mysql> BEGIN;
- Query OK, 0 rows affected (0.00 sec)
3. Insert two data entries to 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, 'two', '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 | two | 0 |
- + ----- + ---------- + ----- + ------ +
- 4 rows in set (0.00 sec)
4. Specify the storage point and set the name to test.
- Mysql> SAVEPOINT test;
- Query OK, 0 rows affected (0.00 sec)
5. Insert 3rd data records 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 | two | 0 |
- | 5 | three | 0 |
- + ----- + ---------- + ----- + ------ +
- 5 rows in set (0.02 sec)
6. Roll Back to the 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 | two | 0 |
- + ----- + ---------- + ----- + ------ +
- 4 rows in set (0.00 sec)
We can see that the records inserted after the save point test are not displayed, that is, the group is successfully rolled to the State before the definition save point test. You can use the save point to submit only partial transaction processing.