PrepZone Logo
PrepZone
Back to Database

Database · Medium

How do you write an UPSERT? Explain INSERT ... ON CONFLICT and MERGE.

DatabaseSQLFresher

Answer preview

An UPSERT inserts a row if its key is absent and updates it if present, atomically and without a race condition. PostgreSQL and MySQL/SQLite use INSERT ... ON CONFLICT / ON DUPLICATE KEY UPDATE; the ANSI standard statement is MERGE, supported by Oracle, SQL Server, and PostgreSQL…