Sub topic 13
Changing and Removing Rows Safely
Assume you have PostgreSQL installed, can connect with psql, and already created the books table from the earlier chapter. The table contains id, title, author, and published_year columns.
Changing or removing rows is different from retrieving them. A SELECT shows data without changing it. An UPDATE changes existing rows, and a DELETE removes rows. Both commands can affect many rows, so always control them with a WHERE clause.
Updating Existing Rows
Use UPDATE with this general form:
UPDATE table_name
SET column_name = new_value
WHERE condition;For example, suppose the publication year for Dune was entered incorrectly:
UPDATE books
SET published_year = 1966
WHERE title = 'Dune';The WHERE clause identifies the row to change. Without it, PostgreSQL changes the column in every row:
UPDATE books
SET published_year = 1966;This would give every book the same publication year.
Prefer a condition that identifies one specific row. A primary key is usually the safest choice:
UPDATE books
SET published_year = 1966
WHERE id = 1;Before updating, inspect the target rows with the same condition:
SELECT id, title, published_year
FROM books
WHERE id = 1;This lets you confirm that the condition matches what you intend.
Deleting Rows
Use DELETE with this form:
DELETE FROM table_name
WHERE condition;To remove Emma:
DELETE FROM books
WHERE title = 'Emma';As with UPDATE, omitting WHERE removes every row while leaving the table itself:
DELETE FROM books;A delete cannot normally be undone after the transaction is committed. Check the target first:
SELECT id, title
FROM books
WHERE title = 'Emma';Then delete by primary key if the result identifies the intended row:
DELETE FROM books
WHERE id = 3;The following workflow reduces accidental changes:
flowchart TD
A[Write a SELECT with the intended WHERE condition] --> B[Inspect the matching rows]
B --> C{Is the result correct?}
C -- No --> D[Revise the condition]
D --> A
C -- Yes --> E[Run UPDATE or DELETE]
E --> F[Run SELECT to verify the result]Using Transactions for a Safety Net
A transaction groups statements so you can either keep all changes or undo them. Start one with BEGIN. Use ROLLBACK to discard changes or COMMIT to make them permanent.
BEGIN;
UPDATE books
SET published_year = 1966
WHERE id = 1;
SELECT id, title, published_year
FROM books
WHERE id = 1;
COMMIT;If the verification query shows a mistake, use ROLLBACK instead of COMMIT:
BEGIN;
DELETE FROM books
WHERE id = 3;
SELECT *
FROM books;
ROLLBACK;The deleted row is restored when the rollback succeeds.
Comparing the Commands
| Command | Changes table structure? | Changes or removes rows? | Can use WHERE? |
|---|---|---|---|
| UPDATE | No | Changes column values | Yes |
| DELETE | No | Removes rows | Yes |
| DROP TABLE | Yes | Removes the table and its data | No |
Exercise
What is the safest first step before running DELETE FROM books WHERE author = 'Jane Austen';?
Show answer
Run SELECT * FROM books WHERE author = 'Jane Austen';, inspect the matching rows, and delete only after confirming they are intended.