dataiotech Labs
dbaguidedbaguideDBAdataxtoolsdataopsphere
Home/Error Library/mysql-1062-duplicate-entry-unique-key
MySQL
MySQL 8.0 - 8.4Data & Constraints
3 min read
ERROR 1062 (23000): Duplicate entry 'test@example.com' for key 'users.email_UNIQUE'

MySQL Unique Key Violation: Concurrency Race Conditions, ETL Ingestion Duplicates & Idempotent Patterns

Diagnosing error 1062 triggered by app race conditions and bulk insert duplicates, remediation with INSERT IGNORE and ON DUPLICATE KEY UPDATE.

Reviewed by:Senior Database Architect & SRE (dataiotech Labs)
Tested Versions: MySQL 8.0 / 8.4 LTS / 9.0

Operational Verification & Reliability Metadata

Last Technical Review: 2 Kasım 2026
Tested VersionsMySQL 8.0.36, 8.4.1 LTS
Required PrivilegesSELECT, INSERT, UPDATE on target table
Risk Assessment
MediumON DUPLICATE KEY UPDATE can consume AUTO_INCREMENT IDs rapidly, exhausting INT ranges
Expected OutputDuplicate identification and idempotent INSERT returning successfully
Rollback Procedure:Delete or revert offending duplicate row
MySQL Docs: INSERT ... ON DUPLICATE KEY UPDATE
First 60 Seconds Emergency Protocol:

The error string is a symptom, not the root cause. Do not restart the server blindly. Run the diagnostic queries below to pinpoint the actual failure mode.

▸Symptom

User registration or batch data loading fails with:

plaintext
ERROR 1062 (23000): Duplicate entry 'test@example.com' for key 'users.email_UNIQUE'

▸What You See in Production

Double-clicking submit buttons or parallel worker threads attempting simultaneous inserts yield an unhandled 500 Internal Server Error.


▸Root Cause Matrix

ProbabilityRoot CauseDescription
%60Concurrent Race ConditionApplication issues SELECT before INSERT; interleaved transactions produce duplicate inserts.
%30ETL Re-runsReprocessing data ingestion batches without idempotent insert logic.
%10Collation Case InsensitivityUnder ci collations, uppercase and lowercase strings collide on unique indexes.

▸EMERGENCY RUNBOOK (0-5 min)

sql
-- 1. Inspect existing duplicate row SELECT id, email, created_at FROM users WHERE email = 'test@example.com'; -- 2. Idempotent insert pattern INSERT INTO users (email, full_name, updated_at) VALUES ('test@example.com', 'Test User', NOW()) ON DUPLICATE KEY UPDATE full_name = VALUES(full_name), updated_at = NOW();

▸PERMANENT BLUEPRINT

  1. 1Eliminate Check-Then-Act: Rely on atomic unique constraints with upsert patterns.
  2. 2Capacity Protection: Use BIGINT UNSIGNED for primary keys to prevent auto_increment exhaustion.

▸Verification

sql
SHOW INDEX FROM users WHERE Key_name = 'email_UNIQUE';

Other Database Error Triage Playbooks

Other battle-tested diagnostic guides for high-severity database errors.

All Errors (82)