Full answer
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);Here the statement runs without a WHERE inside a transaction. Before ROLLBACK, every customer in this session reads Portugal; after it, the final SELECT shows the original countries again:
sql
BEGIN;
UPDATE customers SET country = 'Portugal';
SELECT id, country FROM customers ORDER BY id;
ROLLBACK;
SELECT id, country FROM customers ORDER BY id;| id | country |
|---|---|
| 1 | Spain |
| 2 | UAE |
| 3 | France |
| 4 | India |
Had it run in autocommit mode, the change would already be permanent, and the only way back would be a backup or a manual repair.
Habits that prevent this:
- Write the WHERE first, as a SELECT, and check the row count.
- Start manual sessions with BEGIN, and COMMIT only after checking.
- Use RETURNING to see exactly which rows changed.
- Some clients have a safe-updates mode that rejects UPDATE without WHERE (for example MySQL's
--safe-updates, not run here).
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
BEGIN;
UPDATE customers SET country = 'Portugal';
SELECT id, country FROM customers ORDER BY id;
ROLLBACK;
SELECT id, country FROM customers ORDER BY id;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- A missing or always-true WHERE updates all rows
- Preview with SELECT using the same WHERE
- Use a transaction and check the count before COMMIT
- RETURNING shows the affected rows
Common mistakes
- Running ad hoc updates in autocommit mode
- Copying a WHERE that refers to the wrong alias
Follow-ups
- How would you recover if the change was already committed?
- What does a point-in-time restore involve?
Tips
- Describe a safe manual-change routine