SQL
database management
MySQL
data manipulation
query optimization

INSERT INTO ... SELECT FROM ... ON DUPLICATE KEY UPDATE

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

The `INSERT INTO ... SELECT FROM ... ON DUPLICATE KEY UPDATE` syntax is a powerful construct in SQL that allows you to insert rows from one table into another, while providing the means to handle duplicates gracefully. This command is frequently used in MySQL and MariaDB databases to manage scenarios where unique key constraints must be maintained, yet flexibility is desired in how conflicts are resolved.

Technical Overview

This approach combines several SQL operations into one:

  1. INSERT INTO ... SELECT FROM: This part of the statement copies data from a source table (or a result set derived from SELECT) to a target table.
  2. ON DUPLICATE KEY UPDATE: This clause specifies how to deal with duplicate entries based on a key, typically a primary key or unique index.

Syntax Breakdown

  • target_table: The table where the data is to be inserted.
  • source_table: The table from which data is selected.
  • column1, column2, ...: Column names that are involved in the insert operation. These must be present in both the target and source tables.
  • VALUES(column): Retrieves the value that would be inserted in the event of no duplicate key violation. This is useful to define what happens during an update.
  • products
  • new_products
  • No Duplicate Scenario: If a `product_id` from `new_products` does not exist in `products`, it is simply inserted.
  • Duplicate Key Handling: The `ON DUPLICATE KEY UPDATE` clause comes into play for rows with `product_id` = 1. Instead of discarding the new row or causing an error, the clause updates the `price` of the existing row.
  • Simplicity: Combines insert and update logic into a single statement.
  • Efficiency: Reduces the total number of database operations, leading to performance gains, especially with batch operations.
  • Atomicity: Ensures atomic behavior, thus reducing the risk of data anomalies.
  • Index Maintenance: The statement relies on indexes for detecting duplicates. Therefore, improperly indexed tables might experience slower execution times.
  • Transaction Logs: As with many SQL operations, the transaction log can grow substantially if many rows are affected.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.