Common SQL Errors

MySQL Error 1859: ER_DUP_UNKNOWN_IN_INDEX - How to Fix and Prevent

Galaxy Team
August 7, 2025

<p>The error appears when you create or modify a UNIQUE or PRIMARY KEY and MySQL detects duplicate values in the target columns.</p>

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 1859?

<p>MySQL Error 1859 ER_DUP_UNKNOWN_IN_INDEX occurs when a UNIQUE or PRIMARY KEY you are adding contains duplicate values. Deduplicate the offending rows, then rerun the ALTER or CREATE statement to eliminate the error.</p>

Error Highlights

Typical Error Message

Duplicate entry for key '%s'

Error Type

Constraint Error

Language

MySQL

Symbol

ER_DUP_UNKNOWN_IN_INDEX

Error Code

1859

SQL State

23000

Explanation

Table of Contents

What is MySQL error 1859 ER_DUP_UNKNOWN_IN_INDEX?

The database returns error code 1859 with message 'Duplicate entry for key' when you attempt to create or change a UNIQUE index or PRIMARY KEY that would contain duplicate values. MySQL aborts the statement to protect data integrity.

The error was introduced in MySQL 5.7.1 and inherits SQLSTATE 23000 which denotes constraint violations. Fixing it is critical because the failing DDL leaves your schema in its previous state and may block deployments.

What Causes This Error?

The most common trigger is an ALTER TABLE that adds a unique constraint to a column set that already has duplicate rows. MySQL scans existing data during the DDL operation and raises the error as soon as the first duplicate appears.

The same check runs when you insert or update rows into a table that has a UNIQUE index in place but where earlier bad data slipped in through disabled checks or bulk imports.

How to Fix MySQL Error 1859

First locate the duplicate values with GROUP BY or window functions. Next, remove or update the offending rows so each key combination is unique. Finally, rerun the ALTER TABLE or INSERT that previously failed.

For OLTP systems with minimal downtime, move duplicates to a staging table inside a transaction, commit the clean set, then recreate the unique index. Galaxy's AI copilot can generate the deduplication query and share it with teammates for review.

Common Scenarios and Solutions

Schema migrations managed by tools like Liquibase often fail in CI when historical test data contains duplicates. Add a pre-migration cleanup script that deletes or merges duplicates before the DDL step.

Bulk CSV imports using LOAD DATA can bypass foreign key checks, leaving duplicates that surface later. Always import into a temporary table, run DISTINCT inserts into the target, and validate with NOT EXISTS checks.

Best Practices to Avoid This Error

Design tables with the correct UNIQUE constraints from day one so duplicates never accumulate. Use INSERT ... ON DUPLICATE KEY UPDATE for idempotent upserts instead of blind INSERTs.

Monitor information_schema.statistics and run periodic COUNT DISTINCT audits. Galaxy Collections let teams publish canonical queries that flag duplicate keys and schedule them as recurring checks.

Related Errors and Solutions

Error 1062 Duplicate entry for key occurs when the index name is known at runtime, while 1859 indicates MySQL could not identify which pending index caused the clash during DDL. The remediation steps are the same: locate and remove duplicates, then reapply the constraint.

Common Causes

Existing duplicate rows

Columns targeted by a new UNIQUE index already contain duplicate values.

Wrong composite key choice

The chosen column combination is not truly unique across historical data.

Bulk imports without constraints

Data loaded with disabled unique checks allowed duplicates to slip in.

Application logic bugs

Race conditions inserted identical keys before the index was added.

Related Errors

Error 1062 Duplicate entry

Occurs during INSERT or UPDATE when a value violates an existing UNIQUE index.

Error 1586 Unique constraint is violated

Seen in MySQL when handling foreign data wrapper replication duplicates.

Error 1169 Can't write, duplicate key in table

Older alias for duplicate key errors during INSERT SELECT operations.

FAQs

Does ER_DUP_UNKNOWN_IN_INDEX always involve ALTER TABLE?

No. It can also appear during CREATE TABLE ... SELECT or online ADD INDEX statements when duplicates exist.

How do I find which values are duplicated?

Use GROUP BY with HAVING COUNT(*) > 1 or temporary UNIQUE index with IGNORE to let MySQL report duplicates.

Will enabling ALLOW_INVALID_DATES affect this error?

No. The setting relates to date validation, not uniqueness checks.

Can I bypass the error with IGNORE?

Using ALTER TABLE IGNORE created in old versions is deprecated and removed in MySQL 8.0. Clean data instead.

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