Recipe · SQL recipes

UPSERT in SQL: ON CONFLICT and ON DUPLICATE KEY UPDATE

UPSERT tries to insert and, if it hits a conflict on a unique key, applies the action you defined to the existing row.

Before continuing: This entry is part of the UPDATE guide. If you are new to the topic, start with UPDATE.

Query

Runnable demo in SQLite
CREATE TEMP TABLE inventory_upsert_demo (
  product_id INTEGER PRIMARY KEY,
  stock INTEGER NOT NULL
);

INSERT INTO inventory_upsert_demo (product_id, stock)
VALUES (1, 5);

INSERT INTO inventory_upsert_demo (product_id, stock)
VALUES (1, 8)
ON CONFLICT (product_id) DO UPDATE
SET stock = excluded.stock;

SELECT product_id, stock
FROM inventory_upsert_demo;

The second insert proposes another row with product_id = 1. Because that key already exists, the branch DO UPDATE uses excluded.stock to take the value that was being inserted and update the existing row.

How it works

A INSERT on its own tries to create a row. If a PRIMARY KEY or a UNIQUE rule detects a duplicate, the engine may reject the operation. An UPSERT adds an alternative: when that conflict happens, update the row that already exists or do nothing.

A useful question: “if the same logical identifier arrives again, do I want to reject it, ignore it or replace certain values?”. The answer tells you whether you need a plain INSERT, DO NOTHING or an update on conflict.

Cases that change the result

The conflict target must reflect a real rule

In PostgreSQL and SQLite, the target of ON CONFLICT usually names columns that must be unique. Do not pick a column just to “make the UPSERT work”: there must be a unique constraint or index that defines what “the same entity” means for your data.

If your rule is “one product per product_id”, the primary key is a good arbiter. If the real rule is “one user per email”, you also need to think about how you handle NULL, email changes and normalisation.

UPSERT is not the same as MERGE

MERGE compares a source with a target table and can chain different actions depending on whether rows match. An UPSERT with INSERT ... ON CONFLICT starts from a row you are trying to insert and defines what to do if a uniqueness rule blocks that insert. They overlap in some cases, but they are not interchangeable in every scenario.

Avoid this confusion

Using UPSERT without a clear logical key

If you do not know which columns identify the entity uniquely, the update may hit a different row from the one you expected. Define the uniqueness rule first.

Overwriting columns that should not change

Do not copy every proposed value into the DO UPDATE out of convenience. Update only the attributes that should be refreshed when a conflict occurs.

Copying the syntax between engines

ON CONFLICT and ON DUPLICATE KEY UPDATE are not the same grammar. Keep the shared intent separate from each engine's specific syntax.

Key differences between engines
Common variants of the UPSERT pattern
EngineMain formImportant detail
PostgreSQLON CONFLICT (...) DO UPDATECan infer or name the uniqueness conflict, and exposes the proposed row through excluded.
SQLiteON CONFLICT (...) DO UPDATEIts UPSERT follows the PostgreSQL model with its own extensions; it is not universal SQL syntax either.
MySQL 8.4ON DUPLICATE KEY UPDATETriggered by a duplicate primary key or unique index; the documentation recommends row aliases over the old VALUES(col), which is deprecated.
MySQL 8.4
INSERT INTO inventory (product_id, stock)
VALUES (1, 8) AS new
ON DUPLICATE KEY UPDATE
stock = new.stock;

In MySQL, a table with several unique keys can make it less obvious which one triggers the update. The documentation itself advises caution with ON DUPLICATE KEY UPDATE when there are several unique indexes.