Common SQL Errors

MySQL 1062 “Duplicate entry for key” Error Explained and Fixed

June 25, 2025

The 1062 error occurs when an INSERT or UPDATE tries to store a value that already exists in a UNIQUE or PRIMARY KEY index.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What does "Duplicate entry for key" mean in MySQL?

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 Highlights

Typical Error Message

ERROR 1062 (23000): Duplicate entry 'some_value' for key 'key_name'

Error Type

Constraint Violation

Language

MySQL / MariaDB

Symbol

Error Code

SQL State

Explanation

Table of Contents

What is MySQL error 1062: “Duplicate entry for key”?

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.

What Causes This Error?

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.

.

How to Fix MySQL Duplicate Entry for Key

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.

Common Scenarios and Solutions

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.

Best Practices to Avoid This Error

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.

Related Errors and Solutions

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.

Common Causes

Related Errors

FAQs

Can I Ignore Duplicate Key Errors Automatically?

Yes. Add the IGNORE keyword to INSERT or use INSERT … ON DUPLICATE KEY UPDATE. MySQL will skip or merge duplicates instead of stopping execution.

What’s the Difference Between REPLACE and Upsert?

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.

How Do I Prevent Auto-Increment Collisions After Importing Data?

Always reset AUTO_INCREMENT to MAX(id)+1 after manual inserts or restores. This guarantees new ids do not overlap.

Does Galaxy Help Identify Duplicate Key Issues?

Galaxy displays table indexes and highlights UNIQUE constraints in its metadata panel, making it easier to foresee duplicate key conflicts before running a query.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo