Stop 09 · Modifying DataTopics · 6/9

UPSERT (ON CONFLICT and ON DUPLICATE KEY UPDATE)

Part of the SQL Roadmap

In short

Syncing a product feed or a daily stock file means some rows are new and some already exist. An upsert handles both in one statement, with no need to check first.

An upsert inserts a row, or updates it if a row with the same key already exists. PostgreSQL uses ON CONFLICT, MySQL uses ON DUPLICATE KEY UPDATE, and SQL Server uses MERGE.

The jaadu flips through a fat paper address book, crossing out an old number on an existing card instead of writing a brand new card.

Think of it like this

Saving a number in your phone

You save Priya's number. If she isn't in your contacts yet, a new contact is created. If she is, the phone offers to update her existing contact instead of making a duplicate Priya. That's an upsert: insert if new, update if it already exists.

Watch it happen

Two upserts for the same SKU

Stock feed
inventory
result

TEA-500, stock 40

1/5Monday's feed sends Assam Tea 500g with 40 units.

The big ideas

What you need to know

Needs a unique key

The conflict is detected by a PRIMARY KEY or UNIQUE constraint. Without one, there's nothing to clash with.

EXCLUDED is the new row

In PostgreSQL, EXCLUDED.stock means the value you tried to insert. inventory.stock is the one already stored.

DO NOTHING option

ON CONFLICT DO NOTHING quietly skips duplicates. Handy when you only want to add new rows.

Three dialects

PostgreSQL and SQLite use ON CONFLICT, MySQL uses ON DUPLICATE KEY UPDATE and SQL Server uses MERGE.

See it in code

Walk through the code

sql
1INSERT INTO inventory (sku, product_name, stock, price)2VALUES ('TEA-500', 'Assam Tea 500g', 25, 260.00)3ON CONFLICT (sku) DO UPDATE4SET stock      = inventory.stock + EXCLUDED.stock,5    price      = EXCLUDED.price,6    updated_at = NOW();

1/4A normal insert. If the SKU is new, this is all that happens.

Insert the SKU, or add to its stock and refresh its price if it exists. The MySQL version is INSERT ... ON DUPLICATE KEY UPDATE stock = stock + VALUES(stock), price = VALUES(price).

Save yourself the pain

Common mistakes

Oops

Writing ON CONFLICT (sku) when sku has no unique constraint or index.

Do this instead

PostgreSQL will refuse it. Add UNIQUE (sku) or a unique index on sku first.

Oops

Using DO NOTHING when the feed carries updated prices.

Do this instead

DO NOTHING skips the whole row, so prices never change. Use DO UPDATE for the columns that should refresh.

Your mission

Sync a stock feed

~20 min

Quick check

In ON CONFLICT (sku) DO UPDATE SET price = EXCLUDED.price, what is EXCLUDED.price?

Go deeper

  1. 1Write an upsert that updates a product's price if it already exists.
  2. 2Make sure the table has a unique key for the conflict check.
  3. 3Learn the syntax for the database you use most.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge