Challenge - 5 Problems
Savepoint Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output after rolling back to a savepoint
Consider the following MySQL transaction:
What will be the output of the SELECT query?
START TRANSACTION;
INSERT INTO accounts (id, balance) VALUES (1, 1000);
SAVEPOINT sp1;
UPDATE accounts SET balance = balance - 200 WHERE id = 1;
ROLLBACK TO SAVEPOINT sp1;
SELECT balance FROM accounts WHERE id = 1;
What will be the output of the SELECT query?
MySQL
START TRANSACTION; INSERT INTO accounts (id, balance) VALUES (1, 1000); SAVEPOINT sp1; UPDATE accounts SET balance = balance - 200 WHERE id = 1; ROLLBACK TO SAVEPOINT sp1; SELECT balance FROM accounts WHERE id = 1;
Attempts:
2 left
💡 Hint
Rolling back to a savepoint undoes changes after that savepoint.
✗ Incorrect
The UPDATE reduces balance to 800, but rolling back to savepoint 'sp1' undoes that update, so balance remains 1000.
🧠 Conceptual
intermediate1:30remaining
Purpose of savepoints in transactions
What is the main purpose of using savepoints within a database transaction?
Attempts:
2 left
💡 Hint
Think about what rollback to a savepoint does.
✗ Incorrect
Savepoints let you undo part of a transaction without rolling back everything, so you can fix mistakes without losing all progress.
📝 Syntax
advanced1:30remaining
Identify the syntax error in savepoint usage
Which of the following MySQL statements contains a syntax error related to savepoints?
Attempts:
2 left
💡 Hint
Check the correct syntax for rolling back to a savepoint.
✗ Incorrect
The correct syntax is 'ROLLBACK TO SAVEPOINT sp1;'. 'ROLLBACK SAVEPOINT sp1;' is invalid and causes a syntax error.
🔧 Debug
advanced2:00remaining
Why does releasing a savepoint cause an error?
Given the following sequence:
Why does the last statement cause an error?
START TRANSACTION;
SAVEPOINT sp1;
RELEASE SAVEPOINT sp1;
ROLLBACK TO SAVEPOINT sp1;
Why does the last statement cause an error?
MySQL
START TRANSACTION; SAVEPOINT sp1; RELEASE SAVEPOINT sp1; ROLLBACK TO SAVEPOINT sp1;
Attempts:
2 left
💡 Hint
What does releasing a savepoint do?
✗ Incorrect
Releasing a savepoint removes it. Trying to rollback to a released savepoint causes an error because it does not exist anymore.
❓ optimization
expert2:30remaining
Optimizing transaction control with savepoints
You have a long transaction with multiple steps. You want to undo only the last step if it fails, without aborting the entire transaction. Which approach is best?
Attempts:
2 left
💡 Hint
Think about partial rollback within a transaction.
✗ Incorrect
Savepoints allow you to rollback only part of a transaction, so you can undo the last step without losing earlier changes.