How do I update if exists, insert if not AKA upsert or merge in MySQL?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In MySQL, managing data efficiently often requires a mechanism to handle situations where you need to insert a record if it doesn't exist, or update it if it does. This operation is commonly known as "upsert" or "merge". MySQL provides several ways to achieve this functionality. Below, we delve into different methods to implement upsert operations and discuss their specifics.
Upsert with `INSERT ... ON DUPLICATE KEY UPDATE`
One of the most straightforward methods to perform an upsert in MySQL is using the `INSERT ... ON DUPLICATE KEY UPDATE` syntax. This approach works well when you have a unique key on the table, which could be a primary key or another unique index.
Syntax Example
- INSERT INTO ... VALUES: Attempts to insert a new row into `your_table`.
- ON DUPLICATE KEY UPDATE: If a duplicate key conflict occurs (i.e., a row with the same key already exists), the `UPDATE` clause is executed rather than producing an error.
- VALUES(column): Refers to the value intended for insertion. It allows you to use the same values in both the `INSERT` and `UPDATE` parts.
- It requires a unique key to detect duplicates.
- Allows direct use of the planned insert values in the update operation.
- REPLACE INTO: Triggers a duplicate key update by deleting the existing row with the same key before insertion.
- This operation is non-atomic; it effectively performs a DELETE followed by an INSERT.
- It's suitable when you want to ensure that the data is overwritten entirely, as the row is deleted before insertion.
- May trigger unwanted side effects like deleting associated foreign key records due to the DELETE operation.
- CTE `existing`: Checks for the existence of the record in the table.
- INSERT ... SELECT: Only proceeds with the insert if no matching record is found.
- ON DUPLICATE KEY UPDATE: Updates the existing entry if the key is duplicated.
- Provides more control using conditional logic.
- Ideal if conditional checks need to be more complex or derive from multiple columns or tables.
- Transaction Management: For operations requiring atomicity and rollback, ensure that upserts are wrapped inside transactions, especially when they involve multiple tables or CTEs.
- Performance Implications: Indexes play a critical role in upserts. Ensure that indexes are tailored for the specific key checks occurring in these operations to avoid unnecessary full table scans.
- Error Handling: Understand the implications of upsert operations (like triggers on `REPLACE INTO`) within your database schema, and handle exceptions as needed to avoid data consistency issues.
Related reading
- How do I use TTL on clickhouse table?
- How do I view the SQL generated by the Entity Framework?
- How do I write LINQ's .Skip1000.Take100 in pure SQL?
- How do I write one to many query in Dapper.Net?
- How do I update or sync a forked repository on GitHub?
- How do I update the password for Git?
- How do MySQL indexes work?
- How do parameterized queries help against SQL injection?

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.