Zest.

SQL: Upsert (insert or update)

A short, tested SQL example for upsert (insert or update). Copy it and adjust the names and paths for your project.

SQL
-- PostgreSQL / SQLite
INSERT INTO counters (name, n) VALUES ('visits', 1)
ON CONFLICT (name) DO UPDATE SET n = counters.n + 1;

-- MySQL
INSERT INTO counters (name, n) VALUES ('visits', 1)
ON DUPLICATE KEY UPDATE n = n + 1;

Find it in Zest

Search for “sql upsert (insert or update)” in Zest and this example appears above the results, with other matches to choose from. Searches stay private.

More SQL examples

All SQL examples