Before you start, you need
The problem
You will practise the same update in two situations: a rehearsal and a confirmed shipment. The new value is the same, but the way you finish must differ.
Before writing, note which order changes, which column changes and which status should remain at the end. Use its id so you do not change other orders.
Open the transaction, update the row and check it with id and status with SELECT. If it is a rehearsal, finish with ROLLBACK. If the shipment is confirmed and the result is correct, finish with COMMIT.
Also check that the other orders are untouched. Seeing the change before finishing and checking the final state are different steps; you need both to know you solved the case.
How to think about it
Does a successful check query on its own justify committing a rehearsal?
- Find the row before you start, and limit the UPDATE to its id.
- Check the changed row inside the transaction. The case describes a rehearsal, so the way you finish must undo it.
- In the challenge, the situation is confirmed. You must change how you finish without widening the scope of the change.
Guided practice
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
Rehearse a change and undo it
Start a transaction, change only order 105 to shipped, check that row's id and status with SELECT and finish with ROLLBACK. At the end, all the data must be as it was at the start.
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 change is a rehearsal; the starting state must be restored at the end.
Include SELECT id, status FROM orders WHERE id = 105 before ending the transaction.
Finish with ROLLBACK so that the final status is pending again.
Solution explained
BEGIN;
UPDATE orders SET status = 'shipped' WHERE id = 105;
SELECT id, status FROM orders WHERE id = 105;
ROLLBACK;The SELECT lets you inspect the change before deciding. ROLLBACK restores the previous state, so the order is pending again.
Challenge
Solve this variation. Spot the part that has changed before you look for help.
Practice
Confirm a shipment that has gone out
Order 105 has been confirmed as shipped. Open a transaction, change only its status to 'shipped', query id and status, and commit the change.
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.
Unlike the previous rehearsal, the business rule confirms the change.
Limit the UPDATE and check the row before finishing.
Finish with COMMIT after the SELECT.
Solution explained
BEGIN; UPDATE orders SET status = 'shipped' WHERE id = 105; SELECT id, status FROM orders WHERE id = 105; COMMIT;The fact is confirmed and checked, so COMMIT is the right choice. The other orders are left as they were.
Key takeaways
- Limit the change to the intended rows.
- Check the provisional state before finishing.
- The situation decides between COMMIT and ROLLBACK.
- Keep data unrelated to the change as it is.