🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

Databases Intermediate

What is Upsert?

A database operation that inserts a new row if it does not exist, or updates the existing row if it does.

Upsert (update + insert) eliminates the need for separate check-then-insert-or-update logic. In PostgreSQL: INSERT INTO table (...) VALUES (...) ON CONFLICT (key) DO UPDATE SET column = EXCLUDED.column. MySQL uses INSERT ... ON DUPLICATE KEY UPDATE.

Upserts are atomic (no race conditions), simpler than alternatives, and efficient for sync operations, bulk imports, and idempotent data loading. The ON CONFLICT clause specifies which constraint determines whether to insert or update.

Related Terms

ORM (Object-Relational Mapping)
A technique that lets you interact with a database using object-oriented code instead of writing raw SQL queries.
Write-Ahead Log (WAL)
A technique where changes are first written to a log before being applied to the database, ensuring crash recovery and data integrity.
Stored Procedure
A precompiled collection of SQL statements stored in the database that can be executed as a single unit.
Database Constraint
Rules enforced by the database to maintain data integrity, including NOT NULL, UNIQUE, CHECK, PRIMARY KEY, and FOREIGN KEY.
Schema
The structure definition of a database including tables, columns, data types, relationships, indexes, and constraints.
Materialized View
A database object that stores the precomputed result of a query, offering faster reads at the cost of periodic refresh.
View All Databases Terms →