// dialect comparison

Upsert: insert or update in one statement

Change the row if it is already there, insert it if it is not: the statement every engine spells differently.

Filed under Inserting & updating

The verdict

PostgreSQL and SQLite have the shortest upsert with ON CONFLICT, and MySQL's ON DUPLICATE KEY UPDATE is close behind for tables with one unique key. SQL Server and Oracle need a full MERGE, and on SQL Server it needs HOLDLOCK to be safe under concurrency.

Where they differ

PostgreSQL and SQLite share the ON CONFLICT clause (an extension, not standard SQL), MySQL has ON DUPLICATE KEY UPDATE, and SQL Server and Oracle use the standard MERGE. SQLite's INSERT OR REPLACE and MySQL's REPLACE look similar but delete the old row and insert a new one, which resets columns you didn't list and, in MySQL, fires delete triggers.

How each engine writes it

SQL Server

MERGE INTO customers WITH (HOLDLOCK) AS c
USING (VALUES (7, '[email protected]')) AS s(id, email)
ON c.id = s.id
WHEN MATCHED THEN UPDATE SET c.email = s.email
WHEN NOT MATCHED THEN INSERT (id, email) VALUES (s.id, s.email);

No ON CONFLICT. Without HOLDLOCK, two sessions can both see no match and both insert, and one fails with a duplicate key error.

PostgreSQL

INSERT INTO customers (id, email) VALUES (7, '[email protected]')
ON CONFLICT (id) DO UPDATE SET email = EXCLUDED.email;

The conflict target must match a unique index or constraint. EXCLUDED is the row you tried to insert; DO NOTHING skips duplicates. MERGE also exists since PostgreSQL 15.

MySQL

INSERT INTO customers (id, email) VALUES (7, '[email protected]') AS new
ON DUPLICATE KEY UPDATE email = new.email;

Fires on a collision with any unique key, not a chosen one. The row alias (AS new) needs 8.0.19+; it replaces VALUES(email), which is deprecated.

SQLite

INSERT INTO customers (id, email) VALUES (7, '[email protected]')
ON CONFLICT (id) DO UPDATE SET email = excluded.email;

3.24+. INSERT OR REPLACE also works but deletes the old row first, resetting columns you didn't list; its delete triggers fire only with PRAGMA recursive_triggers = ON.

Oracle

MERGE INTO customers c
USING (SELECT 7 AS id, '[email protected]' AS email FROM dual) s
ON (c.id = s.id)
WHEN MATCHED THEN UPDATE SET c.email = s.email
WHEN NOT MATCHED THEN INSERT (id, email) VALUES (s.id, s.email);

MERGE is the only upsert. It fails with ORA-30926 when one target row matches several source rows. 23ai lets you drop FROM dual.

Questions that come up

Why does INSERT OR REPLACE lose my other columns?

It deletes the conflicting row and inserts a new one, so every column you didn't list gets its default. Use ON CONFLICT … DO UPDATE to keep them.

Can I upsert several rows at once?

Yes. Use a multi-row VALUES list or an INSERT … SELECT, and the conflict clause applies per row in PostgreSQL, SQLite and MySQL. PostgreSQL raises an error if the same key appears twice in one statement. With MERGE, put the batch in the USING source.

How do I tell whether the row was inserted or updated?

PostgreSQL: RETURNING (xmax = 0) AS inserted on ON CONFLICT, or merge_action() in MERGE … RETURNING (17+). MySQL: affected rows is 1 for an insert, 2 for an update, 0 for an unchanged row. SQL Server: OUTPUT $action on the MERGE.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.