Correct a Mistake with a Savepoint
Mediumsql
Learn the Concept: SAVEPOINTWrite a transaction that: debits account 1 by 200, sets a savepoint named sp1, mistakenly credits account 2 by 9999, rolls back to sp1 to undo that mistake, then correctly credits account 2 by 200, and commits.
sqlCREATE TABLE accounts ( id INTEGER PRIMARY KEY, balance INTEGER NOT NULL ) ENGINE=InnoDB;
Sample data:
sqlINSERT INTO accounts (id, balance) VALUES (1, 1000), (2, 500);
Example 1
Input
(none)
Output
id balance 1 800 2 700
Reference: START TRANSACTION; UPDATE accounts SET balance = balance - 200 WHERE id = 1; SAVEPOINT sp1; UPDATE accounts SET balance = balance + 9999 WHERE id = 2; ROLLBACK TO sp1; UPDATE accounts SET balance = balance + 200 WHERE id = 2; COMMIT; — verified end-to-end against the real judge sandbox with ENGINE=InnoDB.
Related Problems
- Duplicate EnrollmentsMedium · sqlSolve Problem
- Employees With No Real DepartmentMedium · sqlSolve Problem
- Union: Honors or Dean's ListMedium · sqlSolve Problem