Writing data safely
INSERT, UPDATE, DELETE, and RETURNING
Multi-row INSERT, upsert and what it misses, the UPDATE with no WHERE, DELETE against TRUNCATE, and the RETURNING clause some engines will not give you.
You run an import, the row counts look right, and three weeks later the finance report is out by an amount nobody can trace. Writes fail in one direction: they touch more rows than you meant. A missing WHERE clause, a unique key you forgot existed, a TRUNCATE inside a transaction you believed was still open. The statement is four words long and the damage is proportional to the size of the table, not to the size of the mistake.
The examples ran on MySQL 9.6 and SQLite 3.51, and the output is pasted as it came back. PostgreSQL is named where its behaviour differs.
A multi-row INSERT is one statement, so it fails as one
There is no reason to send one INSERT per row. Several value tuples in one statement cost one round trip instead of many, and on a busy connection the round trips are most of the cost. The second form takes its rows from a query:
CREATE TABLE product (
product_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
sku VARCHAR(32) NOT NULL,
name VARCHAR(80) NOT NULL,
price_cents INT NOT NULL DEFAULT 0,
stock INT NOT NULL DEFAULT 0,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY uq_product_sku (sku),
CONSTRAINT chk_price CHECK (price_cents >= 0),
CONSTRAINT chk_stock CHECK (stock >= 0)
) ENGINE=InnoDB;
INSERT INTO product (sku, name, price_cents, stock) VALUES
('KB-01', 'Mechanical Keyboard', 8900, 12),
('MS-02', 'Trackball Mouse', 4500, 7),
('HP-03', 'Closed Headphones', 12900, 3);
INSERT INTO product (sku, name, price_cents, stock)
SELECT CONCAT('ARCH-', sku), CONCAT(name, ' (archive copy)'), price_cents, 0
FROM product
WHERE stock < 10;
SELECT product_id, sku, name, price_cents, stock FROM product ORDER BY product_id;
+------------+------------+----------------------------------+-------------+-------+
| product_id | sku | name | price_cents | stock |
+------------+------------+----------------------------------+-------------+-------+
| 1 | KB-01 | Mechanical Keyboard | 8900 | 12 |
| 2 | MS-02 | Trackball Mouse | 4500 | 7 |
| 3 | HP-03 | Closed Headphones | 12900 | 3 |
| 4 | ARCH-MS-02 | Trackball Mouse (archive copy) | 4500 | 0 |
| 5 | ARCH-HP-03 | Closed Headphones (archive copy) | 12900 | 0 |
+------------+------------+----------------------------------+-------------+-------+
The derived rows got their own ids, because INSERT ... SELECT matches nothing. More importantly, the statement is atomic: if row three violates a constraint, the rows before it do not survive.
CREATE TABLE multi (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
sku VARCHAR(16) NOT NULL UNIQUE,
stock INT NOT NULL,
CONSTRAINT chk_multi_stock CHECK (stock >= 0)
) ENGINE=InnoDB;
INSERT INTO multi (sku, stock) VALUES ('A',5),('B',-1),('C',9);
SELECT COUNT(*) AS rows_after_failed_batch FROM multi;
INSERT INTO multi (sku, stock) VALUES ('D',1);
SELECT GROUP_CONCAT(id ORDER BY id) AS ids FROM multi;
ERROR 3819 (HY000) at line 9: Check constraint 'chk_multi_stock' is violated.
+-------------------------+
| rows_after_failed_batch |
+-------------------------+
| 0 |
+-------------------------+
+------+
| ids |
+------+
| 4 |
+------+
Zero rows, and the next insert arrived at id 4. The batch consumed 1, 2 and 3 before it found the problem, and no rollback returns auto-increment values to the pool. Gaps in an id column are not corruption.
Defaults belong to the column, not to the statement. Omitting a column uses its default; writing NULL into it does not:
INSERT INTO product (sku, name) VALUES ('CB-04', 'Braided Cable');
INSERT INTO product (sku, name, price_cents) VALUES ('CB-05', 'USB Hub', NULL);
+------------+-------+-------------+-------+---------------------+
| product_id | sku | price_cents | stock | updated_at |
+------------+-------+-------------+-------+---------------------+
| 7 | CB-04 | 0 | 0 | 2026-10-16 12:42:50 |
+------------+-------+-------------+-------+---------------------+
ERROR 1048 (23000) at line 6: Column 'price_cents' cannot be null
Triggers follow rows, not statements. A multi-row insert fires BEFORE INSERT and AFTER INSERT once per row, so a trigger that writes an audit row writes two for a two-row insert. That rules triggers out for anything with a statement-level cost.
An upsert resolves against a unique key, not against your intent
The upsert is the shape you want when a batch may contain rows that already exist. MySQL spells it ON DUPLICATE KEY UPDATE; PostgreSQL and SQLite spell it ON CONFLICT ... DO UPDATE. Both check the unique indexes on the target table, find a collision, and run the update branch instead of the insert.
The problem is the phrase "a collision". With two unique keys, the engine resolves against whichever one it finds first, which is not necessarily the row you had in mind.
CREATE TABLE account (
account_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(80) NOT NULL,
username VARCHAR(40) NOT NULL,
logins INT NOT NULL DEFAULT 0,
UNIQUE KEY uq_email (email),
UNIQUE KEY uq_username (username)
) ENGINE=InnoDB;
INSERT INTO account (email, username) VALUES
('ana@example.com', 'ana'),
('bo@example.com', 'bo');
INSERT INTO account (email, username, logins) VALUES ('ana@example.com', 'ana', 1) AS new
ON DUPLICATE KEY UPDATE logins = account.logins + 1;
INSERT INTO account (email, username, logins) VALUES ('bo@example.com', 'ana', 1) AS new
ON DUPLICATE KEY UPDATE username = new.username, logins = account.logins + 1;
ERROR 1062 (23000) at line 18: Duplicate entry 'ana' for key 'account.uq_username'
The second statement matched on email, landed on Bo's row, and then tried to set username to ana, which collides with Ana's row. It failed on a key it never mentioned. Had the two keys pointed at compatible values, it would have quietly updated a row chosen by index order.
Two other things an upsert does not do. It does not protect against a lost update: logins = new.logins writes the value the caller read, so two concurrent callers both writing 5 leave 5, not 10. And it does not spare your triggers, which fire on every call, including the calls that end up updating:
+----+--------------------------+
| id | msg |
+----+--------------------------+
| 1 | BEFORE INSERT KB-01 |
| 2 | BEFORE INSERT KB-01 |
| 3 | BEFORE UPDATE KB-01 1->2 |
+----+--------------------------+
+-------+------+
| sku | hits |
+-------+------+
| KB-01 | 2 |
+-------+------+
That ran twice against an empty table. The first call inserted and wrote trace row 1. The second wrote trace row 2 through BEFORE INSERT and trace row 3 through BEFORE UPDATE, though no insert happened. An insert trigger that increments a counter double-counts.
One more cost, visible only in the id column. Two statements reached the table and ids 1 and 3 were used, because the duplicate-key branch consumed id 2 on its way to deciding to update.
+-------------------+
| ids_actually_used |
+-------------------+
| 1,3 |
+-------------------+
+-----+------+
| sku | hits |
+-----+------+
| A-1 | 5 |
| A-2 | 0 |
+-----+------+
MySQL's row-alias form is INSERT ... VALUES (...) AS new ON DUPLICATE KEY UPDATE col = new.col, and with the alias in place you must qualify every target-table column, which is why the example writes account.logins. SQLite names the proposed row excluded, so the increment is hits = hits + excluded.hits. Adding it rather than assigning it is the difference between an increment and a clobber.
UPDATE without a WHERE is the most expensive typo in the discipline
A bad constraint, a duplicate key and an orphaned foreign key all announce themselves with an error. This one announces itself with a success message. Here is a 3,000-row ledger, settled one account at a time:
mysql> UPDATE ledger SET settled = 1 WHERE account_id = 7;
Query OK, 15 rows affected
Rows matched: 15 Changed: 15 Warnings: 0
mysql> UPDATE ledger SET settled = 1;
Query OK, 2985 rows affected
Rows matched: 3000 Changed: 2985 Warnings: 0
Rows matched: 3000 is the number that matters, and the client prints it second. A script that checks the return value sees two successes, and the second one settled the entire ledger.
MySQL's command-line client has a guard for this, and it belongs on in any session where you type rather than run migrations:
mysql --safe-updates mydb
ERROR 1175 (HY000) at line 1: You are using safe update mode and you tried to update a
table without a WHERE that uses a KEY column.
Safe update mode rejects an UPDATE or DELETE whose WHERE does not constrain a key column. It is a client-side check, so it protects the operator and not the application. There is no server-side equivalent, because a full-table update is a legitimate operation.
What the server does give you is statement atomicity plus constraints. A multi-row update that violates a CHECK halfway through leaves every row untouched, including the rows already processed:
CREATE TABLE constraint_demo (
id INT NOT NULL PRIMARY KEY,
v INT NOT NULL,
CONSTRAINT chk_v CHECK (v >= 0)
) ENGINE=InnoDB;
INSERT INTO constraint_demo VALUES (1, 5), (2, 1), (3, 9);
UPDATE constraint_demo SET v = v - 4;
ERROR 3819 (HY000) at line 10: Check constraint 'chk_v' is violated.
+----+---+
| id | v |
+----+---+
| 1 | 5 |
| 2 | 1 |
| 3 | 9 |
+----+---+
Row 1 would have become 1, which is legal. Row 2 would have become -3, which is not. The whole statement rolled back, so the repair is to fix the data or the predicate and run it again. Foreign keys and unique indexes behave the same way and report in their own words:
ERROR 1062 (23000): Duplicate entry '1' for key 'constraint_demo.PRIMARY'
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
(`m3lab`.`stock_movement`, CONSTRAINT `fk_movement_product` FOREIGN KEY
(`product_id`) REFERENCES `product` (`product_id`))
SQLite agrees on statement atomicity: the same three-row table and the same failing update leave all three rows at their original values. The engines differ on plenty, but not on this.
DELETE leaves a trail that TRUNCATE skips
DELETE FROM t removes rows through the normal write path. It fires ON DELETE triggers, checks foreign keys per row, writes undo information, and can be rolled back. TRUNCATE TABLE drops and recreates the storage, which is why it is fast and why none of that happens.
Start with the trigger. A table with an AFTER DELETE trigger that logs each removal, emptied two different ways:
+----------+------------+
| doc_rows | audit_rows |
+----------+------------+
| 0 | 1 |
+----------+------------+
One row was deleted with DELETE FROM doc WHERE title = 'a', which wrote one audit row. The table was then emptied with TRUNCATE TABLE doc, which wrote nothing. MySQL's manual states that truncate operations do not invoke ON DELETE triggers, so any audit trail fed by a delete trigger is bypassed and the row count will not tell you.
Foreign keys are the second difference, and it is sharper than it looks. TRUNCATE fails when any foreign key references the table at all, whether or not the referencing table holds rows:
TRUNCATE TABLE product;
ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint
(`m3lab`.`stock_movement`, CONSTRAINT `fk_movement_product`)
DELETE FROM product;
Query OK, 0 rows affected
The child table was empty both times. DELETE succeeded because there was nothing to violate; TRUNCATE refused because the constraint exists. MySQL has no CASCADE on TRUNCATE, so the choices are to drop the constraint, truncate the child too, or use DELETE. PostgreSQL has CASCADE, and requires it or a matching truncate of the referencing tables.
The third difference is the one that costs weekends. In MySQL TRUNCATE is DDL, and DDL causes an implicit commit:
BEGIN;
INSERT INTO pending (note) VALUES ('should have been rolled back');
TRUNCATE TABLE doc;
ROLLBACK;
+-----------------------------+
| pending_rows_after_rollback |
+-----------------------------+
| 1 |
+-----------------------------+
The ROLLBACK returned success and the insert is still there, because the TRUNCATE committed it on the way past. CREATE TABLE, ALTER TABLE and DROP TABLE behave the same, so a transaction containing DDL is not a transaction in the sense you were relying on:
BEGIN;
INSERT INTO ddl_demo (v) VALUES (1);
CREATE TABLE created_inside_tx (id INT) ENGINE=InnoDB;
ROLLBACK;
SELECT (SELECT COUNT(*) FROM ddl_demo) AS ddl_demo_rows;
+---------------+
| ddl_demo_rows |
+---------------+
| 1 |
+---------------+
PostgreSQL and SQLite do not do this. Both have transactional DDL, and a PostgreSQL TRUNCATE is documented as safely rolled back if the surrounding transaction does not commit. MySQL's TRUNCATE also resets AUTO_INCREMENT, while DELETE does not. SQLite has no TRUNCATE at all:
sqlite> TRUNCATE TABLE product;
Error: in prepare, near "TRUNCATE": syntax error
DELETE FROM product is the only way to empty a table there, so the trigger fires and the foreign keys are checked. If your SQL has to run on all three engines, DELETE FROM is the portable spelling, and TRUNCATE is the optimisation you reach for once you have checked the constraints, the triggers and the transaction around it.
RETURNING hands back the row the engine actually wrote
Every write above needed a follow-up SELECT to find out what happened, and a follow-up SELECT is a second round trip that can also be wrong: it races a concurrent writer, and its predicate may not match the rows the write touched. RETURNING removes the round trip by making the write return rows.
SQLite added it in 3.35.0, released 2021-03-12, and it works on insert, update and delete. PostgreSQL has had it far longer:
INSERT INTO product (sku, name, price_cents) VALUES
('KB-01','Mechanical Keyboard',8900),
('MS-02','Trackball Mouse',4500)
RETURNING product_id, sku, price_cents;
UPDATE product SET price_cents = price_cents + 1000 WHERE sku = 'MS-02'
RETURNING sku, price_cents;
┌────────────┬───────┬─────────────┐
│ product_id │ sku │ price_cents │
├────────────┼───────┼─────────────┤
│ 1 │ KB-01 │ 8900 │
│ 2 │ MS-02 │ 4500 │
└────────────┴───────┴─────────────┘
┌───────┬─────────────┐
│ sku │ price_cents │
├───────┼─────────────┤
│ MS-02 │ 5500 │
└───────┴─────────────┘
PostgreSQL's manual is explicit that the data available to RETURNING is the row as the triggers left it, which makes it the right way to read a column a trigger computed. PostgreSQL also allows RETURNING old.col, new.col; SQLite returns the row as written and has no old/new keywords.
MySQL has no RETURNING clause. The syntax is rejected outright:
INSERT INTO product (sku,name) VALUES ('RT-01','probe') RETURNING product_id;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near
'RETURNING product_id' at line 1
The nearest substitute is LAST_INSERT_ID(). For a multi-row insert it returns the id of the first row in the batch, not the last, and it says nothing about an UPDATE or a DELETE:
+----------------+------------+
| last_insert_id | actual_ids |
+----------------+------------+
| 2 | 2,3,4 |
+----------------+------------+
On MySQL the honest pattern for write-then-read is a transaction: do the write, select the affected rows by the key you used, and commit. The select is only correct inside the same transaction, which is the subject of the next lesson.
What this lesson leaves out
Batching a large DELETE or UPDATE so it does not hold locks for minutes, and the lock ordering that keeps two such statements from colliding, are in lesson nine.
Create the product table from the top of this lesson with both CHECK constraints, the stock_movement table that references it, and triggers logging initial stock and adjustments.
Before running each statement, write down how many rows you expect it to change and where you expect it to fail.
INSERT INTO product (sku, name, price_cents, stock) VALUES
('TR-01', 'Trigger A', 100, 5),
('TR-02', 'Trigger B', 200, -1);
INSERT INTO product (sku, name, price_cents) VALUES ('KB-01', 'duplicate');
UPDATE product SET stock = stock + 3 WHERE sku IN ('TR-01', 'TR-02');
Then answer three questions: how many rows stock_movement holds after the second value fails, what the next auto-increment value is after that failure, and what the third statement does differently from the first.
Show the solution and the reasoning
The first statement inserts nothing. The stock = -1 row violates chk_stock, and statement-level atomicity rolls back the row already accepted. If you predicted one row for TR-01, the useful part of the exercise is the reason it is wrong: atomicity is a property of the statement, not the row, and that is the same property that makes a four-row INSERT safe to retry as a unit.
stock_movement holds nothing, for the same reason. The trigger for TR-01 fired and wrote its audit row, and that row was rolled back with everything else. A trigger's writes belong to the statement that fired it, so an audit log cannot record a change that did not survive.
The next auto-increment value is past the failed batch, because InnoDB allocates values before it checks constraints. This matters in one direction only: never derive anything from id arithmetic, such as "the count of rows equals the highest id". Uniqueness is what the column is for.
The third statement is the one that behaves. It matches two rows through the unique key sku, so it takes a record lock rather than a range lock, fires the AFTER UPDATE trigger once per matched row, and writes two movement rows with a delta of +3. Change the predicate to WHERE stock < 10 and the same statement scans an index range and can lock more rows than it changes.
If you expected the second statement to succeed, the correction is worth writing down. KB-01 already exists, so it is a unique-key violation reported as ERROR 1062, and nothing changes. It is not an upsert, because nothing in it asks for one.
Official reference
The MySQL manual and the PostgreSQL and SQLite documentation for what this lesson uses.
Progress
Mark this lesson done and it stays marked in this browser.