0
0
MySQLquery~10 mins

Savepoints in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Savepoints
Start Transaction
Set Savepoint A
Execute SQL Statements
Set Savepoint B
Execute More SQL Statements
ROLLBACK TO Savepoint A?
YesUndo to Savepoint A
Continue
COMMIT Transaction
Start a transaction, create savepoints to mark positions, optionally rollback to a savepoint to undo partial work, then commit or rollback the whole transaction.
Execution Sample
MySQL
START TRANSACTION;
INSERT INTO accounts VALUES (1, 'Alice', 100);
SAVEPOINT sp1;
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
ROLLBACK TO SAVEPOINT sp1;
COMMIT;
This code starts a transaction, inserts a row, sets a savepoint, updates it, then rolls back to the savepoint undoing the update but keeping the insert, and finally commits.
Execution Table
StepActionState ChangeResult
1START TRANSACTIONTransaction beginsNo data committed yet
2INSERT INTO accounts VALUES (1, 'Alice', 100)Row insertedaccounts table has 1 row
3SAVEPOINT sp1Savepoint sp1 createdMarks state after insert
4UPDATE accounts SET balance = balance - 50 WHERE id = 1Row updatedAlice's balance is now 50
5ROLLBACK TO SAVEPOINT sp1Undo changes after sp1Update undone, balance back to 100
6COMMITTransaction endsInsert is saved, update is not
💡 Transaction committed after rollback to savepoint sp1, so only changes before sp1 remain
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
accounts tableempty[{id:1, name:'Alice', balance:100}][{id:1, name:'Alice', balance:100}][{id:1, name:'Alice', balance:50}][{id:1, name:'Alice', balance:100}][{id:1, name:'Alice', balance:100}]
Key Moments - 3 Insights
Why does the update disappear after rolling back to savepoint sp1?
Because the rollback to sp1 undoes all changes made after sp1, including the update at step 4, as shown in execution_table row 5.
Does the insert at step 2 get undone by the rollback to sp1?
No, the insert happened before sp1 was created, so it remains after rollback, as shown in variable_tracker and execution_table.
What happens if we rollback without specifying a savepoint?
The entire transaction is undone, losing all changes since START TRANSACTION, unlike rollback to a savepoint which only undoes partial changes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is Alice's balance after step 4?
A100
B50
C0
DRollback to sp1
💡 Hint
Check the 'State Change' and 'Result' columns at step 4 in the execution_table.
At which step does the rollback undo the update?
AStep 5
BStep 3
CStep 4
DStep 6
💡 Hint
Look for the 'ROLLBACK TO SAVEPOINT sp1' action in the execution_table.
If we remove the rollback to savepoint, what would be the final balance of Alice after commit?
A0
B100
C50
DNo change
💡 Hint
Without rollback, the update at step 4 remains, see variable_tracker after step 4.
Concept Snapshot
Savepoints let you mark points inside a transaction.
You can rollback to a savepoint to undo partial changes.
Use SAVEPOINT name; to create one.
Use ROLLBACK TO SAVEPOINT name; to undo to it.
COMMIT saves all changes after rollback.
Useful for complex transactions with partial undo.
Full Transcript
Savepoints in MySQL allow you to mark specific points inside a transaction. You start a transaction with START TRANSACTION, then create savepoints using SAVEPOINT name. You can execute SQL statements after each savepoint. If you want to undo some changes but keep others, you use ROLLBACK TO SAVEPOINT name, which undoes all changes made after that savepoint. Finally, you use COMMIT to save the remaining changes. This lets you control partial rollbacks inside a transaction instead of undoing everything. In the example, after inserting a row and updating it, rolling back to the first savepoint undoes the update but keeps the insert. Then committing saves the insert permanently.