MySQL Constraints

MySQL Error 1062: Duplicate Entry for Key (Fix Guide)

Fix MySQL Error 1062 duplicate entry. Learn how to handle unique constraint violations with INSERT IGNORE, ON DUPLICATE KEY UPDATE, and REPLACE.

Mar 12, 2026 5 min read

The Error Message

Duplicate entry 'value' for key 'PRIMARY'

What Causes MySQL 1062?

MySQL Error 1062 fires when you try to INSERT or UPDATE a row with a value that already exists in a column with a UNIQUE or PRIMARY KEY constraint.

Common Causes

Inserting a duplicate primary key

You are inserting a row with an ID that already exists in the table.

Unique column violation

A column like email or username has a UNIQUE constraint and the value already exists.

Auto-increment gap or reset

The auto-increment counter was reset or manually set, causing ID collisions.

Bulk import with duplicates

Importing a CSV or SQL dump that contains duplicate rows.

How to Fix It

Step 1: Use INSERT IGNORE to skip duplicates

INSERT IGNORE skips the row if a duplicate key violation occurs, without throwing an error.

-- Silently skip rows that would cause duplicates:
INSERT IGNORE INTO users (email, name)
VALUES ('john@example.com', 'John');

Step 2: Use ON DUPLICATE KEY UPDATE

This is the best approach for upserts — insert if new, update if exists.

-- Update the existing row instead:
INSERT INTO users (email, name, updated_at)
VALUES ('john@example.com', 'John Updated', NOW())
ON DUPLICATE KEY UPDATE
  name = VALUES(name),
  updated_at = VALUES(updated_at);

Step 3: Check for existing data first

Query first to see if the data already exists, then decide whether to insert or update.

-- Check before inserting:
SELECT id, email FROM users
WHERE email = 'john@example.com';

-- Or use NOT EXISTS:
INSERT INTO users (email, name)
SELECT 'john@example.com', 'John'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM users WHERE email = 'john@example.com'
);

Step 4: Fix auto-increment counter

If auto-increment is out of sync, resetting it prevents future collisions.

-- Reset auto-increment to max existing ID + 1:
ALTER TABLE users AUTO_INCREMENT = 1;

-- Or set it to a specific value:
SELECT MAX(id) FROM users;
ALTER TABLE users AUTO_INCREMENT = 1001;

How to Prevent This Error

Design your schema with proper unique constraints from the start. Use upsert patterns (ON DUPLICATE KEY UPDATE) when importing data. Always use auto-increment for primary keys unless you have a specific reason not to.

Fix MySQL Errors with AI2SQL

Instead of debugging SQL syntax manually, describe what you need in plain English and let AI2SQL generate the correct query for MySQL.

Try AI2SQL Free

No credit card required

Frequently Asked Questions

What does MySQL Error 1062 mean?

Error 1062 means you tried to insert a value that already exists in a column with a UNIQUE or PRIMARY KEY constraint. MySQL won't allow duplicate values in these columns.

How do I insert without getting duplicate errors?

Use INSERT IGNORE to skip duplicates silently, or ON DUPLICATE KEY UPDATE to update the existing row. Both prevent Error 1062.

Can AI2SQL generate upsert queries?

Yes. Tell AI2SQL 'insert user if not exists, otherwise update their name' and it generates the correct ON DUPLICATE KEY UPDATE syntax.

Stop Debugging SQL Errors Manually

Describe what you need in plain English. AI2SQL generates correct MySQL queries instantly.

Try AI2SQL Free

No credit card required