Purpose
This statement is used to create a savepoint before committing a transaction, allowing for partial rollback of the transaction.
Note
- The name of a savepoint must be unique within the same transaction. A later savepoint with the same name will overwrite an earlier one.
- After creating a savepoint, you can continue processing, commit the transaction, rollback the entire transaction, or rollback the transaction to the specified savepoint. For more information about transaction rollback, see ROLLBACK.
Syntax
SAVEPOINT savepoint_name
Parameters
| Parameter | Description |
|---|---|
| savepoint_name | The name of the savepoint. |
Examples
Create the sample table employee.
obclient> CREATE TABLE employee (id INT, name VARCHAR(10), salary INT);
obclient> INSERT INTO employee VALUES (1, 'Alice', 1000), (2, 'Bob', 2000), (3, 'Alice', 3000);
obclient> SELECT * FROM employee;
+------+-------+--------+
| ID | NAME | SALARY |
+------+-------+--------+
| 1 | Alice | 1000 |
| 2 | Bob | 2000 |
| 3 | Alice | 3000 |
+------+-------+--------+
3 rows in set
Update the
employeetable, changing thenameof the record withid = 3to 'Carol', and create a savepoint namedpoint_after_update.obclient> UPDATE employee SET name = 'Carol' WHERE id = 3; obclient> SAVEPOINT point_after_update;Insert a new row into the
employeetable and query the table.obclient> INSERT INTO employee VALUES (4, 'David', 4000); obclient> SELECT * FROM employee;Query OK, 1 row affected +------+-------+--------+ | ID | NAME | SALARY | +------+-------+--------+ | 1 | Alice | 1000 | | 2 | Bob | 2000 | | 3 | Carol | 3000 | | 4 | David | 4000 | +------+-------+--------+ 4 rows in setRollback the transaction to the
point_after_updatesavepoint and query the table.obclient> ROLLBACK TO SAVEPOINT point_after_update; obclient> SELECT * FROM employee;Query OK, 0 rows affected +------+-------+--------+ | ID | NAME | SALARY | +------+-------+--------+ | 1 | Alice | 1000 | | 2 | Bob | 2000 | | 3 | Carol | 3000 | +------+-------+--------+ 3 rows in setobclient> COMMIT;
