Reading preferences
Concept

Changing Rows and Using Transactions

Follow inserts, updates, deletes, and a rollback using fictional data.

database, sql, postgresql, insert, update, delete, transactions

Changes deserve a boundary

INSERT adds rows, UPDATE changes existing rows, and DELETE removes rows. A transaction groups operations: after BEGIN, changes are tentative until COMMIT succeeds. ROLLBACK discards changes made in the open transaction. A transaction helps keep a group of database changes together; it does not make an incorrect change correct.

SQL illustration

In a separate PostgreSQL session, start with the same temporary, fictional table. Use a fresh session with no transaction already open. No real library data is needed:

BEGIN;
CREATE TEMP TABLE books (
  id integer PRIMARY KEY,
  title text NOT NULL,
  pages integer
);
INSERT INTO books (id, title, pages) VALUES
  (1, 'Kite', 120), (2, 'Oak', NULL),
  (3, 'Tide', 200), (4, 'Fern', 120);
COMMIT;

Now try a change that is deliberately rolled back:

BEGIN;
INSERT INTO books (id, title, pages) VALUES (5, 'Glow', 60);
UPDATE books SET pages = 150 WHERE id = 1;
DELETE FROM books WHERE id = 2;
SELECT id, title, pages FROM books ORDER BY id;
ROLLBACK;
SELECT id, title, pages FROM books ORDER BY id;

Prepared results — not live execution:

During the transaction: idtitlepages
1Kite150
3Tide200
4Fern120
5Glow60
After rollback: idtitlepages
1Kite120
2OakNULL
3Tide200
4Fern120

The transaction's own SELECT sees its changes. After rollback, the added row is gone, Kite has its original page count, and Oak is back. This example assumes no other session changes these rows.

See the reasoning

See a transaction's changes

Compare the original table with this transaction's view as it changes and rolls back. This is prepared; Intuvo does not run SQL.

Begin

BEGIN

Original committed setup
idtitlepages
1Kite120
2OakNULL
3Tide200
4Fern120
Transaction starts
idtitlepages
1Kite120
2OakNULL
3Tide200
4Fern120

The transaction starts with the four committed setup rows.

Before making a real change

Without WHERE, an UPDATE or DELETE can affect every row. First inspect the intended rows with a SELECT using the same condition, and check the affected-row count. Use a transaction where appropriate, verify changes before committing, and handle errors explicitly. In PostgreSQL, a statement error normally leaves the transaction needing rollback before ordinary commands can continue. Some clients automatically commit individual statements when there is no explicit transaction.

Replacing ROLLBACK with COMMIT would make this group of changes committed if it succeeds; a later ROLLBACK does not undo that commit. Transactions are not backups and cannot reverse all external side effects. Work with appropriate permissions and a tested recovery plan when handling real data.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. What does INSERT do in the example?
2. What can happen if WHERE is omitted from an UPDATE or DELETE?
3. What does ROLLBACK do to changes in the open transaction?
4. What does a successful COMMIT do?

References and further reading