2627: Violation of PRIMARY KEY / UNIQUE KEY constraint. Cannot insert duplicate key in object
SQL ServerWhat does it mean?
You tried to insert or update a value that already exists in column(s) protected by a PRIMARY KEY or UNIQUE constraint. Duplicate keys are not accepted.
Why does it happen?
Inserting the same unique value a second time, an UPDATE colliding with an existing value, a missing uniqueness check, or two concurrent requests producing the same row.
How to fix it
Check whether the row exists before inserting, or use MERGE / IF NOT EXISTS. For idempotent inserts, apply the "INSERT ... WHERE NOT EXISTS" pattern. Catch the error and return a meaningful message. If you genuinely need duplicate data, review the constraint. Use IDENTITY or SEQUENCE to avoid collisions when generating keys.
More from this engine
- 1205Transaction (Process ID) was deadlocked on lock resources with another process and has been chosen as the deadlock victim
- 2601Cannot insert duplicate key row in object with unique index. The duplicate key value is ...
- 547The INSERT/UPDATE/DELETE statement conflicted with the FOREIGN KEY / CHECK constraint
- 18456Login failed for user
- 8152String or binary data would be truncated