INSERT IGNORE & ON DUPLICATE KEY UPDATE
INSERT IGNORE & ON DUPLICATE KEY UPDATE: Upserting in MySQL
When synchronizing data feeds, importing large CSV files, or tracking user website activity, you will frequently encounter records that already exist in your database. Standard INSERT statements fail completely on duplicate key collisions, aborting entire batches. MySQL provides two powerful solutions: INSERT IGNORE and ON DUPLICATE KEY UPDATE (Upserting).
1. The Duplicate Key Collision Problem
Suppose we have a user analytics table where user_id is unique:
If you run:
2. Silent Skipping with INSERT IGNORE
When you use INSERT IGNORE, MySQL silently skips any row that would cause a duplicate key violation (or invalid data type error), downgrading errors to harmless warnings:
- Rows that cause duplicate key violations are ignored with zero errors.
- Clean rows are inserted normally.
- Limitation: The existing row is not updated or modified in any way.
3. The Power of Upsert: ON DUPLICATE KEY UPDATE
In software engineering, an "Upsert" (Update or Insert) is an operation that:
- 1Inserts a new record if the primary/unique key does not exist.
- 2Updates the existing record if the primary/unique key does exist!
Modern MySQL 8.0.19+ Alias Syntax:
In MySQL 8.0.19+, you can use table and column aliases directly in the update clause:
4. What About REPLACE INTO? (The Hidden Trap)
MySQL also supports REPLACE INTO, but DBAs consider it a dangerous anti-pattern:
REPLACE INTOdoes not update in-place.- It physically DELETES the old row and INSERTS a brand new row!
- Dangers:
- 1It triggers
ON DELETE CASCADEon foreign key child tables, unintentionally wiping related records! - 2It fires
DELETEtriggers. - 3It increments the
AUTO_INCREMENTsequence unnecessarily.
- Always prefer
INSERT ... ON DUPLICATE KEY UPDATEoverREPLACE INTO!
5. Best Practices & Common Pitfalls
- Ensure Unique Index Exists:
ON DUPLICATE KEY UPDATEonly activates if the collision occurs on aPRIMARY KEYorUNIQUEindex. It has zero effect on standard non-unique columns. - Inspect
ROW_COUNT()Return Codes: - If a row is newly inserted, MySQL reports 1 row affected.
- If an existing row is updated, MySQL reports 2 rows affected!
- If an existing row is matched but no values changed, MySQL reports 0 rows affected.
Multiple Choice Questions
1. What does the term "Upsert" signify in database management?
A. Upgrading the server hardware B. Inserting a row if it is new, or updating the existing row if a duplicate key is detected C. Sorting rows in ascending order D. Uploading data to cloud storage Answer: B Explanation: An "Upsert" combines insert and update semantics: inserting when no matching unique key exists, and updating when a collision occurs.
2. How does INSERT IGNORE handle a row that violates a PRIMARY KEY constraint during batch insertion?
A. It terminates the entire transaction immediately B. It converts the error into a warning, skips the duplicate row, and proceeds to insert the remaining valid rows C. It overwrites the primary key with NULL D. It deletes the table Answer: B Explanation: INSERT IGNORE suppresses duplicate key errors, quietly skipping colliding rows while continuing to process valid ones.
3. What is the fundamental danger of using REPLACE INTO instead of INSERT ... ON DUPLICATE KEY UPDATE?
A. REPLACE INTO is only supported on Linux B. REPLACE INTO physically deletes the existing row before inserting, which can trigger cascading deletes on child tables C. REPLACE INTO cannot handle numbers D. REPLACE INTO locks the database for 24 hours Answer: B Explanation: REPLACE INTO performs a DELETE followed by an INSERT, potentially triggering ON DELETE CASCADE constraints and destroying dependent child rows.
4. What row count does MySQL return when ON DUPLICATE KEY UPDATE successfully modifies an existing record?
A. 0 rows affected B. 1 row affected C. 2 rows affected D. -1 rows affected Answer: C Explanation: By MySQL convention, ON DUPLICATE KEY UPDATE returns 1 for a new insert, 2 for an existing updated row, and 0 if the row was matched but values were identical.
5. On which types of columns does ON DUPLICATE KEY UPDATE detect collisions?
A. Any column with an index B. Columns with PRIMARY KEY or UNIQUE constraints only C. Only columns of type VARCHAR D. Only foreign key columns Answer: B Explanation: ON DUPLICATE KEY UPDATE triggers exclusively when an insert causes a collision on a Primary Key or a Unique Key constraint.
Modifying Records with UPDATE
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Inserting Records with INSERT INTO | Modifying Records with UPDATE |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.