0
0
SQLquery~10 mins

Savepoints within transactions in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Savepoints within transactions
Start Transaction
Execute SQL Statements
Create Savepoint
Execute More SQL Statements
Rollback to Savepoint?
YesUndo to Savepoint
Continue
Commit or Rollback Transaction
End
You start a transaction, run some commands, create savepoints to mark spots, and can rollback to these savepoints without undoing everything. Finally, you commit or rollback the whole transaction.
Execution Sample
SQL
BEGIN;
INSERT INTO accounts VALUES (1, 'Alice', 100);
SAVEPOINT sp1;
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
ROLLBACK TO sp1;
COMMIT;
This transaction inserts a row, sets a savepoint, updates the balance, then rolls back to the savepoint undoing the update, and finally commits.
Execution Table
StepActionEffect on DataTransaction State
1BEGIN;Transaction started, no data changed yetActive
2INSERT INTO accounts VALUES (1, 'Alice', 100);Row added: Alice with balance 100Active
3SAVEPOINT sp1;Savepoint 'sp1' createdActive
4UPDATE accounts SET balance = balance - 50 WHERE id = 1;Alice's balance changed from 100 to 50Active
5ROLLBACK TO sp1;Undo update, balance back to 100Active
6COMMIT;All changes before commit saved permanentlyCommitted
💡 Transaction committed, changes saved except the undone update rolled back to savepoint
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5Final
accounts table (Alice's balance)N/A10050100100
Transaction StateInactiveActiveActiveActiveCommitted
Savepoint sp1NoneNoneCreatedNoneNone
Key Moments - 3 Insights
Why does the balance return to 100 after rolling back to the savepoint?
Because the rollback to savepoint undoes all changes made after the savepoint was created, as shown in step 5 of the execution_table.
Does rolling back to a savepoint end the transaction?
No, the transaction remains active after rollback to savepoint, allowing further commands before commit or full rollback, as seen in the transaction state column.
What happens if we rollback the entire transaction instead of to a savepoint?
All changes made in the transaction are undone and the transaction ends without committing, unlike rollback to 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?
A50
B100
C0
D150
💡 Hint
Check the 'Effect on Data' column at step 4 in the execution_table.
At which step does the transaction state change to 'Committed'?
AStep 3
BStep 5
CStep 6
DStep 2
💡 Hint
Look at the 'Transaction State' column in the execution_table for when it changes to 'Committed'.
If we remove the rollback to savepoint command, what would Alice's balance be after commit?
A100
B50
C0
DRollback error
💡 Hint
Refer to the variable_tracker for balance changes and consider what rollback to savepoint does.
Concept Snapshot
BEGIN starts a transaction
SAVEPOINT marks a point to rollback to
ROLLBACK TO savepoint undoes changes after that point
COMMIT saves all changes permanently
Rollback to savepoint keeps transaction active
Full rollback undoes all and ends transaction
Full Transcript
This visual execution shows how savepoints work inside a transaction. First, the transaction begins. Then, a row is inserted into the accounts table. A savepoint named sp1 is created to mark this spot. Next, the balance for Alice is updated from 100 to 50. Rolling back to the savepoint undoes this update, restoring the balance to 100. Finally, the transaction commits, saving all changes except the undone update. The transaction state changes from inactive to active at the start, and to committed at the end. Savepoints allow partial undo without ending the transaction, unlike a full rollback which cancels everything.