Overview
Scenarios
Product Architecture
Instance Types
Compatibility Notes
SAVEPOINT savepoint_name;
ROLLBACK TO SAVEPOINT savepoint_name;
RELEASE SAVEPOINT savepoint_name;
Parameter | Required | Description |
savepoint_name | Yes | The unique name identifier for a savepoint supports uppercase and lowercase English letters and numbers. |
ERROR 1305 (42000): SAVEPOINT does not exist.-- Create a test table.CREATE TABLE account (id INT PRIMARY KEY,name VARCHAR(50),balance DECIMAL(10,2));-- Insert test data.INSERT INTO account VALUES (1, 'Alice', 1000.00);INSERT INTO account VALUES (2, 'Bob', 500.00);
-- BEGIN TRANSACTIONBEGIN;-- Check initial stateSELECT * FROM account WHERE id IN (1,2);-- Alice transfers 100 CNY to BobUPDATE account SET balance = balance - 100 WHERE id = 1;SELECT * FROM account WHERE id IN (1,2);-- Set savepoint sp1SAVEPOINT sp1;-- Bob received 100 CNY.UPDATE account SET balance = balance + 100 WHERE id = 2;SELECT * FROM account WHERE id IN (1,2);-- Set savepoint sp2SAVEPOINT sp2;-- Transfer 50 CNY againUPDATE account SET balance = balance - 50 WHERE id = 1;UPDATE account SET balance = balance + 50 WHERE id = 2;SELECT * FROM account WHERE id IN (1,2);-- Set savepoint sp3SAVEPOINT sp3;-- Rollback to savepoint sp2 (undo the second transfer)ROLLBACK TO SAVEPOINT sp2;SELECT * FROM account WHERE id IN (1,2);-- RELEASE SAVEPOINT sp1 (deleting all savepoints from sp1 onwards)RELEASE SAVEPOINT sp1;-- Attempting to roll back to sp3 will fail (because sp3 has been deleted)-- ROLLBACK TO SAVEPOINT sp3; -- This line will throw an error-- COMMIT TRANSACTIONCOMMIT;
-- Query the final balanceSELECT * FROM account;
+----+-------+---------+| id | name | balance |+----+-------+---------+| 1 | Alice | 900.00 || 2 | Bob | 600.00 |+----+-------+---------+
Steps | Alice Balance | Bob Balance | Description |
Initial status | 1000.00 | 500.00 | Before the transaction starts |
First transfer | 900.00 | 500.00 | Alice transfers out 100 CNY |
After sp1 is set | 900.00 | 600.00 | Bob received 100 CNY. |
After sp2 is set | 850.00 | 650.00 | Transfer 50 CNY again |
Rollback to sp2 | 900.00 | 600.00 | Undo the second transfer |
Final commit | 900.00 | 600.00 | Transaction completed |
Was this page helpful?
You can also Contact sales or Submit a Ticket for help.
Help us improve! Rate your documentation experience in 5 mins.
Feedback