Sub topic 16
Insert-or-Update with Upsert
The problem an upsert solves
This chapter assumes you can write basic SELECT statements with WHERE, understand tables and keys, and know that a UNIQUE constraint prevents duplicate values.
An upsert combines an insert and an update. It inserts a row when no matching row exists. If a row already matches a key or unique constraint, it updates that row instead.
For example, suppose each customer has a unique email address. You receive a customer record from an external system. You want to add new customers but update the name when the email already exists.
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL
);The email constraint gives the database a reliable way to identify an existing customer.
PostgreSQL and SQLite syntax
PostgreSQL and SQLite use ON CONFLICT. This example inserts a customer, or updates the name when the id already exists:
INSERT INTO customers (id, name, email)
VALUES (1, 'Ada King', '[email protected]')
ON CONFLICT (id) DO UPDATE
SET name = EXCLUDED.name;EXCLUDED means the row that the statement tried to insert. The existing row remains in customers. Therefore, EXCLUDED.name is the incoming name.
You can also use a unique email as the conflict target:
INSERT INTO customers (id, name, email)
VALUES (1, 'Ada Lovelace', '[email protected]')
ON CONFLICT (email) DO UPDATE
SET name = EXCLUDED.name;The conflict target must identify a primary key or a suitable unique constraint. You can choose to ignore a conflict instead:
INSERT INTO customers (id, name, email)
VALUES (1, 'Ada Lovelace', '[email protected]')
ON CONFLICT (id) DO NOTHING;Other database dialects
Upsert syntax is not portable. MySQL uses ON DUPLICATE KEY UPDATE:
INSERT INTO customers (id, name, email)
VALUES (1, 'Ada King', '[email protected]')
ON DUPLICATE KEY UPDATE
name = VALUES(name);Newer MySQL versions may use an alias for the incoming row instead of VALUES(name), depending on the version and configuration.
SQL Server supports MERGE, but MERGE has complex concurrency and correctness concerns. Many teams prefer separate statements inside a carefully designed transaction, or an INSERT that handles a duplicate-key error. Check your database documentation before choosing a dialect-specific form.
Choosing what to update
Do not overwrite every column automatically. Immutable fields, such as a creation timestamp, should usually remain unchanged. Update only fields that the incoming record is allowed to change.
For example, this changes the name but preserves the existing email and identifier:
INSERT INTO customers (id, name, email)
VALUES (1, 'Ada King', '[email protected]')
ON CONFLICT (id) DO UPDATE
SET name = EXCLUDED.name;The database checks the key and then follows one path:
An upsert is often safer than “check with SELECT, then insert or update” in application code. Two requests can perform the check at the same time. A database constraint and a single upsert statement give the database responsibility for resolving the conflict.
Exercise
In the PostgreSQL example, what does EXCLUDED.name refer to?
Show answer
It refers to the name from the incoming row that the statement attempted to insert.