Common SQL Errors

MySQL Error 1062: ER_DUP_ENTRY – Duplicate entry '%s' for key %d

Galaxy Team
August 5, 2025

The ER_DUP_ENTRY error signals that a UNIQUE or PRIMARY KEY index already contains the value you are trying to insert or update.

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 is MySQL error 1062 ER_DUP_ENTRY?

MySQL Error 1062 ER_DUP_ENTRY occurs when an INSERT or UPDATE attempts to write a value that already exists in a UNIQUE or PRIMARY KEY column. Eliminate or modify the duplicate value, or adjust the index definition, to resolve the conflict.

Error Highlights

Typical Error Message

Duplicate entry '%s' for key %d

Error Type

Constraint Violation

Language

MySQL

Symbol

ER_DUP_ENTRY

Error Code

1062

SQL State

Explanation

Table of Contents

What is MySQL error 1062 ER_DUP_ENTRY?

Error 1062 fires when MySQL detects a duplicate value for a column or set of columns covered by a UNIQUE or PRIMARY KEY constraint. The server blocks the statement and returns the message “Duplicate entry '%s' for key %d.”

This runtime error protects data integrity by ensuring that each unique index stores distinct values.

Ignoring it can lead to inconsistent records and unreliable query results.

What Causes This Error?

Duplicate primary-key values trigger the error most often.

Attempting to insert a row whose id already exists in the table immediately fails.

Updates that change a non-unique value to one that is already present also raise the error, even when the original row passed validation.

How to Fix MySQL Error 1062

First identify the conflicting value by reading the error text or running a SELECT query. Remove or change the duplicate row, then re-run the statement.

If the value should not be unique, drop or alter the index.

If duplicates are legitimate only in rare cases, consider a composite UNIQUE index that includes more columns.

Common Scenarios and Solutions

Bulk imports often fail because source data contains repeated keys. Load the data into a staging table, de-duplicate, then move clean rows into production tables.

Parallel workers inserting sequential ids without coordination can collide.

Use AUTO_INCREMENT with an adequate allocation block or switch to UUIDs.

Best Practices to Avoid This Error

Always review your UNIQUE indexes to be sure they match business rules. Test inserts with representative data before deploying schema changes.

Use Galaxy’s AI copilot to scan scripts for potential key conflicts and to preview inserts in a sandbox connection before they reach production.

Related Errors and Solutions

Error 1136 (ER_WRONG_VALUE_COUNT_ON_ROW) signals mismatched column counts during insert.

Unlike 1062, it concerns column alignment, not uniqueness.

Error 1452 (ER_NO_REFERENCED_ROW) indicates a missing parent row required by a FOREIGN KEY. Resolve it by inserting the parent or disabling the constraint temporarily.

.

Common Causes

Related Errors

FAQs

Can I ignore Error 1062?

Ignoring the error is risky because it compromises data integrity. Always address the duplicate or modify the index intentionally.

Does AUTO_INCREMENT prevent duplicates?

Yes, AUTO_INCREMENT guarantees unique numeric values, but manual inserts that specify the id can still collide.

How does Galaxy help avoid this error?

Galaxy’s AI copilot previews inserts, flags potential key clashes, and lets teams run queries in a shared workspace that surfaces constraint violations early.

Is disabling unique checks safe?

Using SET unique_checks=0 is safe only during controlled bulk loads into an empty table, followed by careful validation.

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