Before you start, you need
Review a change before deciding how to finish
Sometimes you need to review one or more changes before deciding whether they should be kept. A transaction treats those changes as a single operation: you can commit them together or undo them together.
BEGIN opens the transaction. Then you make the change and query it. Inside the transaction you can already see its effect, even though you have not committed it yet.
COMMIT keeps the changes, and ROLLBACK undoes them. If you are rehearsing a shipment, undo it. If the shipment is real and the check is correct, commit it. How you finish depends on the situation, not on the SQL running without errors.
Undoing at the end does not justify changing the wrong rows during the rehearsal. Limit the change with WHERE and check the intended row before finishing. The lab checks both the provisional change and how you finish.
BEGIN;
-- changes and verification
COMMIT;
-- or ROLLBACK;See it in action
We want to rehearse shipping order 102 and then undo it.
Before running: If the change is undone with ROLLBACK, which status will the last query show?
BEGIN;
UPDATE orders
SET status = 'shipped'
WHERE id = 102;
SELECT id, status
FROM orders
WHERE id = 102;
ROLLBACK;
SELECT id, status
FROM orders
WHERE id = 102;| id | status |
|---|---|
| 102 | pending |
- BEGIN opens the transaction. The UPDATE provisionally changes the status of the order.
- The first SELECT lets you check shipped before finishing. ROLLBACK undoes that change.
- The final SELECT shows pending again. If the shipment had been confirmed and checked, the right way to finish would have been COMMIT.
- A transaction does not fix a WHERE that is too broad. The scope must be correct, even in a rehearsal you later undo.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Commit a reviewed shipment
Write a block that starts a transaction, changes only order 102 to shipped, includes a SELECT that checks that row, and ends with COMMIT. All other data must stay as it is.
We check the full final state: the change you were asked to make and that every other row is untouched. Review the output of each statement too.
Every attempt resets the data, so include all the steps you need to check in a single attempt.
View tables and data
Open this panel to see the full data.
The case calls for a committed change. Check the order after updating it and before finishing.
Include a checking SELECT.
Finish with COMMIT only after checking.
Solution explained
BEGIN; UPDATE orders SET status = 'shipped' WHERE id = 102; SELECT * FROM orders WHERE id = 102; COMMIT;The transaction gives you a decision point, but it does not replace a correct condition.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Undo a rehearsal
Order 102 has not been confirmed as shipped yet. Open a transaction, change only its status to 'shipped', query id and status to check it, and undo the rehearsal.
We check the full final state: the change you were asked to make and that every other row is untouched. Review the output of each statement too.
Every attempt resets the data, so include all the steps you need to check in a single attempt.
View tables and data
Open this panel to see the full data.
The situation describes a rehearsal, not a confirmed shipment.
Check after the change and before finishing.
The way to finish that undoes the change is ROLLBACK.
Solution explained
BEGIN; UPDATE orders SET status = 'shipped' WHERE id = 102; SELECT id, status FROM orders WHERE id = 102; ROLLBACK;The check shows shipped provisionally; the final status goes back to pending because the situation calls for undoing it.
Key takeaways
- BEGIN opens the transaction.
- COMMIT keeps the changes.
- ROLLBACK undoes pending changes.
- Check before choosing how to finish.
Go deeper
Do not assume a failed statement has automatically undone the whole transaction. Error handling, autocommit and concurrency vary by engine. Check the state, and how to finish, on the system you use.