On Duplicate Key Update same as insert
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Databases often require handling situations where an attempt is made to insert a new record with a key that already exists. A common solution is to use `ON DUPLICATE KEY UPDATE`, a part of SQL syntax provided by some RDBMS like MySQL, which allows you to specify an update when a duplicate key conflict occurs. We'll dive into the `ON DUPLICATE KEY UPDATE` clause, its technicalities, use cases, and explore how it differs from a straightforward `INSERT` operation.
Understanding `ON DUPLICATE KEY UPDATE`
When you insert data into a table, there are scenarios where the data you're trying to insert has a primary key or unique index that already exists in the table. In such cases, a standard `INSERT` statement would result in an error due to key duplication. The `ON DUPLICATE KEY UPDATE` clause allows an alternative approach: instead of failing, the query performs an update on the existing record.
Basic Syntax
Here’s the basic syntax for using `ON DUPLICATE KEY UPDATE`:
- `INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...)` denotes the insert operation with specified columns and values.
- `ON DUPLICATE KEY UPDATE column1 = value1, column2 = value2, ...` specifies how to update the record if a duplicate key conflict occurs.
- Insert vs Update: Using `ON DUPLICATE KEY UPDATE` is efficient as it combines two actions into one statement. However, frequent updates can lead to increased table fragmentation and a higher number of write operations.
- Indexes: Ensure that the table has appropriate indexes to optimize the performance of this operation. Without indexes, the RDBMS may need to perform full table scans to check for duplicates, degrading performance.
- The auto-increment value is incremented for every attempted insert, regardless of whether it results in an actual insertion or an update. Thus, skipped numeric sequences in the primary key can occur.
- Primary Keys: Suitable for tables where records are uniquely identified by this key, ensuring that each insert or update maintains data integrity.
- Unique Keys: Offers flexibility to update specific attributes based on unique constraints, allowing for combined uniqueness across multiple columns.
- Merge Statement: Available in databases like SQL Server (as `MERGE`), it combines `INSERT`, `UPDATE`, and `DELETE` operations into a single statement for handling similar logic.
- Upsert Patterns: In PostgreSQL, use the `INSERT ... ON CONFLICT` construct for a similar effect. The behavior is akin to `ON DUPLICATE KEY UPDATE` but with syntax and capability differences.
Related reading
- OneToOneField vs ForeignKey in Django
- opensource tool for Oracle Change data capture - Alternative to GoldenGate
- Opentelemetry traceid for Couchbase Database Change Protocol
- Optimal JVM settings for Cassandra
- Optimal settings for Cassandra Java driver to write to the local data centre only
- Optimistic concurrency control clarification
- Optimistic Offline Lock Achieve this in database offering Serializability without Linearizability? (i.e., DB does not provide strict serializability)
- Optimistic vs. Pessimistic locking

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.