Consider a transaction that inserts a new row into a table but does not commit immediately. What will another session see before and after the COMMIT?
BEGIN TRANSACTION; INSERT INTO employees (id, name) VALUES (101, 'Alice'); -- No COMMIT yet -- Session 2 runs: SELECT * FROM employees WHERE id = 101; COMMIT; -- Session 2 runs again: SELECT * FROM employees WHERE id = 101;
Think about transaction isolation and when changes become visible to other sessions.
Before COMMIT, the inserted row is only visible inside the transaction that made the change. Other sessions cannot see uncommitted changes. After COMMIT, the change is saved and visible to all sessions.
If a transaction updates a row but then issues a ROLLBACK, what is the state of the data?
BEGIN TRANSACTION; UPDATE products SET price = price * 1.1 WHERE id = 5; ROLLBACK; SELECT price FROM products WHERE id = 5;
ROLLBACK undoes all changes made in the current transaction.
ROLLBACK cancels all changes made in the transaction, so the data remains as it was before the transaction started.
Which statement best describes the role of COMMIT in transaction management?
Think about what happens to changes and locks after COMMIT.
COMMIT finalizes all changes made in the transaction and releases any locks, making changes visible to other transactions.
Which of the following SQL snippets will cause a syntax error?
BEGIN TRANSACTION; UPDATE accounts SET balance = balance - 100 WHERE id = 1; COMMIT TRANSACTION; ROLLBACK;
Consider what happens if you try to ROLLBACK after COMMIT in the same transaction.
After COMMIT, the transaction ends. Issuing ROLLBACK without a new transaction causes a syntax or runtime error.
A developer runs the following sequence but notices the data was changed despite a ROLLBACK. What is the most likely cause?
BEGIN TRANSACTION; UPDATE orders SET status = 'shipped' WHERE id = 10; COMMIT; ROLLBACK; SELECT status FROM orders WHERE id = 10;
Think about the order of COMMIT and ROLLBACK commands.
Once COMMIT is executed, changes are permanent. ROLLBACK after COMMIT does nothing because there is no active transaction to roll back.