Top 10 SQL Errors
How to Fix MySQL "Duplicate entry"
MySQL rejected a row because a UNIQUE or PRIMARY KEY value already exists.
Example error message
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
Common causes
- You are inserting an existing primary key.
- A UNIQUE column already has that value.
- An auto-increment sequence may be out of sync.
- The app is submitting the same record twice.
Quick fix
INSERT INTO users (email) VALUES ('emily@example.com')
ON DUPLICATE KEY UPDATE email = VALUES(email);
Checklist
- Check spelling of table and column names.
- Check the correct database, schema, and table alias.
- Compare your SQL dialect: MySQL, PostgreSQL, SQL Server, Oracle, or SQLite.
- Run a very small SELECT query first, then add conditions step by step.