PostgreSQL INSERT with ON CONFLICT DO NOTHING
PostgreSQL INSERT with ON CONFLICT DO NOTHING is a SQL statement in the PostgreSQL Specific category. PostgreSQL: silently skip insertion if a conflict occurs (unique key violation), no error raised. The syntax is INSERT INTO t VALUES (...) ON CONFLICT DO NOTHING;. It returns conflict-ignored insert. A typical example: CREATE TABLE users (id INT PRIMARY KEY, name TEXT); INSERT INTO users VALUES (1, 'Alice'); -- OK INSERT INTO users VALUES (1, 'Bob') ON CONFLICT DO NOTHING; -- No error, no update (row stays as Alice) -- Returns 0 rows affected -- Comparison: -- ON CONFLICT DO NOTHING: skip silently -- ON CONFLICT DO UPDATE: upsert -- Without ON CONFLICT: throws duplicate key error -- Multiple rows, some conflicting: INSERT… A close relative is SERIAL, which postgreSQL auto-increment type. Creates an INTEGER column with a sequence. Equivalent to AUTO_INCREMENT in MySQL. A close relative is RETURNING, which postgreSQL extension that returns values from INSERT/UPDATE/DELETE statements (like OUTPUT in SQL Server).