SQL
Database Management
MySQL
Upsert
Data Insertion

On Duplicate Key Update same as insert

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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.

Course illustration
Course illustration

All Rights Reserved.