The 1062 error occurs when an INSERT or UPDATE tries to store a value that already exists in a UNIQUE or PRIMARY KEY index.
MySQL error 1062 “Duplicate entry for key” signals that your INSERT or UPDATE is trying to put a value that already exists in a column protected by a UNIQUE or PRIMARY KEY constraint. Identify the conflicting value, then either change the data, delete the old row, or use INSERT ... ON DUPLICATE KEY UPDATE to resolve the conflict.
ERROR 1062 (23000): Duplicate entry 'some_value' for key 'key_name'
The 1062 error fires when MySQL rejects a row because it would violate a UNIQUE or PRIMARY KEY constraint. The engine detects that the value you are inserting or updating already exists in the indexed column combination.
The error belongs to SQLSTATE 23000 (Integrity Constraint Violation). It protects data consistency by preventing two identical keys in the same table.
Re-inserting an existing primary-key value, such as an auto-increment id, immediately triggers the duplicate entry error. This often happens during bulk data loads, migrations, or manual test inserts.
Accidentally duplicating a business-key column protected by a UNIQUE index—like email, username, or order_number—also raises 1062.
Even a hidden generated index on a UNIQUE constraint can fail silently until runtime.
Concurrent sessions inserting the same key without proper locking can cause race-condition collisions that surface as duplicate entry errors.
.
First locate the conflicting key with the value shown in the error message. Query the table to confirm the existing row.
Option 1: Change or skip the incoming row so the key is unique. Option 2: Delete or update the existing row if it is obsolete. Option 3: Use INSERT … ON DUPLICATE KEY UPDATE (or REPLACE INTO) to merge rows automatically.
Bulk CSV import: add IGNORE to LOAD DATA or INSERT statements so MySQL silently discards duplicates, then review skipped rows.
ETL upserts: use INSERT … ON DUPLICATE KEY UPDATE to turn would-be duplicates into UPDATEs.
Auto-increment drift after manual id inserts: reset the sequence with ALTER TABLE table_name AUTO_INCREMENT = max(id)+1.
Always query for existing keys before bulk writes. A Galaxy SQL editor autocomplete for index metadata helps developers see uniqueness constraints early.
Wrap multi-row INSERTs in transactions with proper error handling to retry only the failed rows, not the entire batch.
Add application-level validation for business-key uniqueness before hitting the database.
Error 1213 Deadlock found: usually appears when duplicate entry retries are poorly managed; resolve by reducing lock scope.
Error 1452 Cannot add or update child row: arises from foreign-key violations rather than unique-key duplication; fix missing parent rows.
Yes. Add the IGNORE keyword to INSERT or use INSERT … ON DUPLICATE KEY UPDATE. MySQL will skip or merge duplicates instead of stopping execution.
REPLACE deletes the existing row, then inserts the new one, resetting foreign keys. Upsert (ON DUPLICATE KEY UPDATE) updates the existing row in place, preserving references.
Always reset AUTO_INCREMENT to MAX(id)+1 after manual inserts or restores. This guarantees new ids do not overlap.
Galaxy displays table indexes and highlights UNIQUE constraints in its metadata panel, making it easier to foresee duplicate key conflicts before running a query.