T-SQL Tutorial

Msg 2627 Level 14 - Violation of PRIMARY KEY constraint


On Transact SQL language the Msg 2627 Level 14 - Violation of PRIMARY KEY constraint means that you cannot insert duplicate key in object.

Msg 2627 Level 14 Example:

We have the table TEST:

USE model;
GO
CREATE TABLE TEST(
   ID INT NOT NULL PRIMARY KEY,
   NAME VARCHAR(10) NOT NULL,
   BIRTHDAY date );
GO

IDNAMEBIRTHDAY
1Tom1982-07-15




Invalid insert:

USE model;
GO
INSERT INTO TEST(id, name, birthday) VALUES (1, 'Tom','1982-07-15');
GO

Message
Msg 2627, Level 14, State 1, Line 1
Violation of PRIMARY KEY constraint 'PK__TEST__3214EC27C70092B2'. Cannot insert duplicate key in object 'dbo.TEST'. The duplicate key value is (1). The statement has been terminated.

Correct insert:

USE model;
GO
INSERT INTO TEST(id, name, birthday) VALUES (2, 'Tom','1982-07-15');
GO

Message
(1 row(s) affected)

Other error messages: