1382
MariaDBerrordatahigh confidence

Duplicate unique key

Production Risk

Medium — insert fails; application must handle the error.

What this means

A duplicate value was detected on a unique key that is not the primary key.

Why it happens
  1. 1Inserting or updating a row so that a non-primary unique index becomes duplicated
How to reproduce
trigger — this will error
trigger — this will error
INSERT INTO t (email) VALUES ('dup@example.com'); -- email has UNIQUE KEY

expected output

ERROR 1382 (23000): Duplicate entry 'dup@example.com' for key 'email'

Fix 1

Use INSERT IGNORE

Use INSERT IGNORE
INSERT IGNORE INTO t (email) VALUES ('dup@example.com');

Why this works

Silently skips the duplicate row.

Fix 2

Use INSERT ... ON DUPLICATE KEY UPDATE

Use INSERT ... ON DUPLICATE KEY UPDATE
INSERT INTO t (email, col) VALUES ('dup@example.com', 'val') ON DUPLICATE KEY UPDATE col = VALUES(col);

Why this works

Updates the existing row on duplicate.

Sources
Official documentation ↗

MySQL 8.0 — 1382 ER_DUP_UNIQUE

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MariaDB errors