Common SQL Errors

MySQL Error 1169: ER_DUP_UNIQUE - Duplicate Unique Constraint Violation Explained and Fixed

Galaxy Team
August 6, 2025

ER_DUP_UNIQUE is raised when an INSERT or UPDATE duplicates a value protected by 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 is MySQL error 1169 (ER_DUP_UNIQUE)?

MySQL Error 1169 (ER_DUP_UNIQUE) appears when an INSERT or UPDATE tries to write a duplicate value into a column backed by a UNIQUE or PRIMARY KEY index. Remove or change the duplicate value, or adjust the index, to resolve the problem.

Error Highlights

Typical Error Message

Can't write, because of unique constraint, to table '%s'

Error Type

Constraint Violation

Language

MySQL

Symbol

ER_DUP_UNIQUE

Error Code

1169

SQL State

23000

Explanation

Table of Contents

Overview

MySQL raises error 1169 with condition ER_DUP_UNIQUE when a write operation would create duplicate values in a column or set of columns that have a UNIQUE or PRIMARY KEY constraint.

The server stops the statement, returns SQLSTATE 23000, and protects data integrity. Understanding why the duplicate occurs lets you apply the correct fix quickly.

When Does ER_DUP_UNIQUE Occur?

The error happens during INSERT, REPLACE, LOAD DATA, or UPDATE statements that target a table with unique indexes.

If the new key already exists in the index, MySQL blocks the write and throws ER_DUP_UNIQUE.

It can also surface in replication when downstream servers reapply statements that introduce duplicates not present on the primary.

Why Fixing It Matters

Ignoring the error leaves data unchanged and can break application logic that assumes the write succeeded. Continuous failures can clog job queues and replication, slowing downstream analytics tools like Galaxy.

.

Common Causes

Related Errors

FAQs

Can I disable unique checks temporarily?

You can set SQL_MODE=IGNORE_SPACE or use INSERT IGNORE, but disabling constraints is risky and not recommended for production.

How do I let duplicates overwrite automatically?

Use INSERT ... ON DUPLICATE KEY UPDATE or the REPLACE statement to have MySQL update or replace the existing row.

Why does AUTO_INCREMENT cause duplicates after export?

If you insert explicit ids during restore, the AUTO_INCREMENT counter may lag behind, so the next automatic id overlaps. Reset AUTO_INCREMENT to max(id)+1.

Can Galaxy help avoid ER_DUP_UNIQUE?

Galaxy's AI copilot checks existing keys during query writing and warns about potential duplicates, reducing runtime failures.

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