Menu
CoddyTech

Try Changes in a Transaction

Part of the SQL Foundations section of Coddy's PostgreSQL journey. Lesson 19 of 22.

BEGIN starts a transaction: a group of changes that can succeed together. COMMIT keeps them and ROLLBACK discards them. You can inspect changes inside a transaction before deciding.

Input table products:

idnamecategorypricestockactivenote
1Maptravel125TrueNULL
2Notebookbooks80TrueRecycled
3Atlasbooks243FalseNULL
4Mughome128TrueBlue
5lamphome302FalseClearance
6Mini maptravel610TrueNULL
BEGIN;
UPDATE products SET stock = 0 WHERE id = 1;
ROLLBACK;
SELECT id, stock FROM products WHERE id = 1;

The final stock is still 5 because the rollback undid the update. Place the result SELECT after ROLLBACK when you want to inspect restored data. This is a local exercise database, not the store itself.

BEGIN groups work; COMMIT saves it and ROLLBACK undoes it. End with a query when the exercise needs a result table.

challenge icon

Challenge

Medium

Start a transaction, change product id 4 stock to 99, roll back, then return id and stock for id 4 to verify restoration.

The input tables are shown in the test panel. Each run starts from fresh data. Return the named columns in the stated order; do not hard-code the displayed rows.

Try it yourself

-- Write your SQL here.
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in SQL Foundations

Practice on your own: PostgreSQL playground