Skip to content
Databases3 min read

Fix MySQL error 1062: Duplicate entry

ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'

Short answer

Error 1062 means the row you're inserting has a value that already exists in a PRIMARY KEY or UNIQUE column. The message names the duplicate value and the key, so find the existing row first, then decide: update it, skip the duplicate, or fix the AUTO_INCREMENT counter.

ArabyHost teamLast updated: ShareWhatsAppXLinkedIn
Contents
  1. Causes
  2. How to fix it
  3. Prevention
  4. On ArabyHost

A primary key or unique index forbids the same value twice. When a row arrives with a value that’s already there, MySQL rejects it and tells you the value and the key:

ERROR 1062 (23000): Duplicate entry '[email protected]' for key 'users_email_unique'

[email protected] already exists, and the index that blocked the insert is users_email_unique. MySQL 8 prefixes the table name, as in 'users.users_email_unique'.

Causes

  • Importing a dump into a database that already holds the same rows, typically on a second attempt after an import stopped halfway.
  • An AUTO_INCREMENT counter behind the highest existing ID, after manual inserts or a partial import.
  • Duplicate entry '0' for key 'PRIMARY': the ID column lost AUTO_INCREMENT, which often happens after an incomplete WordPress migration.
  • A form submitted twice, or an app that doesn’t check whether an email or username exists before inserting.
  • Adding a UNIQUE index to a column that already contains duplicates.
  • Running a seeder or insert script more than once.

How to fix it

  1. Find the existing row in phpMyAdmin’s SQL tab:
SELECT * FROM users WHERE email = '[email protected]';
  1. When importing, use an empty database or drop the old tables first. When exporting from phpMyAdmin, choose Export → Custom and enable Add DROP TABLE / VIEW / PROCEDURE / FUNCTION / EVENT / TRIGGER statement, so the dump drops each table before creating it.

  2. Fix the AUTO_INCREMENT counter: find the highest ID and set the counter above it:

SELECT MAX(id) FROM orders;
ALTER TABLE orders AUTO_INCREMENT = 1501;
  1. Restore AUTO_INCREMENT when the duplicate value is 0. For the WordPress posts table:
ALTER TABLE wp_posts MODIFY ID BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT;

Look at the row with ID 0 first, as it’s usually an incomplete entry, and repeat for the other affected tables.

  1. Handle expected duplicates in code, for example in a settings table:
INSERT INTO settings (name, value) VALUES ('site_title', 'My shop')
ON DUPLICATE KEY UPDATE value = VALUES(value);

In Laravel use updateOrCreate or firstOrCreate, plus the unique:users,email validation rule on forms.

  1. Before adding a UNIQUE index, find duplicates and remove or merge them:
SELECT email, COUNT(*) FROM users GROUP BY email HAVING COUNT(*) > 1;

Prevention

  • Check whether a value exists before inserting, and disable the submit button after the first click.
  • Write insert scripts so they can run twice without creating duplicates.
  • Take a backup before any import, and never import on top of a database that has data.
  • If a large dump keeps stopping halfway, see phpMyAdmin import file too large.

On ArabyHost

Databases run on MariaDB/MySQL with phpMyAdmin, so you can run the queries above straight from the SQL tab. Daily backups are taken with JetBackup, so if a bad import damages your data you can restore the database from an earlier copy, as described in the WordPress backup guide. When moving a WordPress site, follow the WordPress migration guide to avoid losing AUTO_INCREMENT. If you can’t tell which query failed, the Araby AI assistant reads your error log and explains the fix.

FAQ

What does Duplicate entry '0' for key 'PRIMARY' mean?

The ID column has lost its AUTO_INCREMENT attribute, so every new row gets 0 and collides with the previous one. Restore it with ALTER TABLE ... MODIFY on the ID column.

Is INSERT IGNORE a good fix?

It's fine when you deliberately want to skip duplicates, such as importing a list with repeats. But it also turns other errors into silent warnings, so don't use it instead of understanding why duplicates happen.

Why does re-importing a database cause this error?

The tables still hold the rows from the first import. Import into an empty database, or export with the DROP TABLE option so old tables are dropped before being recreated.

Useful? Share it with someone who needs it:WhatsAppXLinkedIn

Read next

Ready to put your idea online?

Fast, isolated hosting, support that understands you, and an AI assistant that fixes problems with you.