DBAtlas · Database operations atlas

2627: Violation of PRIMARY KEY / UNIQUE KEY constraint. Cannot insert duplicate key in object

SQL Server

What 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.

Open in DBAtlas →

More from this engine