Module 6 · Changing data

Control changes with COMMIT and ROLLBACK

By the end you will be able to review a change before keeping or undoing it.

About 14 min

0 of 52 lessons

Before you start, you need

Update rows with UPDATE · Delete rows with DELETE

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.

Minimal syntax
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?

SQL
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;
Example result: 1 rows
idstatus
102pending
  1. BEGIN opens the transaction. The UPDATE provisionally changes the status of the order.
  2. The first SELECT lets you check shipped before finishing. ROLLBACK undoes that change.
  3. The final SELECT shows pending again. If the shipment had been confirmed and checked, the right way to finish would have been COMMIT.
  4. 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

Not started

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.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Undo a rehearsal

Not started

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.

Key takeaways

  • BEGIN opens the transaction.
  • COMMIT keeps the changes.
  • ROLLBACK undoes pending changes.
  • Check before choosing how to finish.

Next: Practice: modify data inside a transaction

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.