{"id":3456,"date":"2015-12-16T10:02:41","date_gmt":"2015-12-16T15:02:41","guid":{"rendered":"https:\/\/academy.sqlbak.com\/?p=3456"},"modified":"2023-10-17T04:54:32","modified_gmt":"2023-10-17T08:54:32","slug":"violation-of-constraint-cannot-insert-duplicate-key-in-object-msg-2627","status":"publish","type":"post","link":"https:\/\/academy.sqlbak.com\/violation-of-constraint-cannot-insert-duplicate-key-in-object-msg-2627\/","title":{"rendered":"Violation of … constraint ‘…’. Cannot insert duplicate key in object ‘…’ (Msg 2627)"},"content":{"rendered":"
SQL Server’s error “Violation of … constraint ‘…’. Cannot insert duplicate key in object ‘…’<\/em>” can appear when a user tries to insert a non-unique value into a table’s unique column such as the primary key. Look at the following syntax:<\/p>\n <\/p>\n The user created the new table Table_1,\u00a0and started to fill it adding a\u00a0row:<\/p>\n then the next row:<\/p>\n After execution of this command the following error appeared:<\/p>\n This message says that the user tries to insert duplicate values into a unique column. To avoid this error just select an unique\u00a0value, for example, 2:<\/p>\n SQL Server’s error “Violation of … constraint ‘…’. Cannot insert duplicate key in object ‘…’” can appear when a user tries to insert a non-unique value into a table’s unique column such as the primary key. Look at the following syntax: CREATE TABLE Table_1 (ID INT NOT NULL PRIMARY KEY, NAME VARCHAR(10) NOT NULL, AGE […]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[159],"tags":[],"yoast_head":"\nCREATE TABLE<\/span> Table_1\r\n(ID INT<\/span> NOT NULL<\/span> PRIMARY KEY<\/span>, \r\n NAME VARCHAR<\/span>(10) NOT NULL<\/span>, \r\n AGE date<\/span>);<\/pre>\n
INSERT INTO<\/span> Table_1 (id, name, age) VALUES<\/span> (1, 'John'<\/span>,'30'<\/span>);<\/pre>\n
INSERT INTO<\/span> Table_1 (id, name, age) VALUES<\/span> (1, 'John'<\/span>,'30'<\/span>);<\/pre>\n
Msg 2627, Level 14, State 1, Line 10<\/span>\r\nViolation of PRIMARY KEY constraint 'PK__Table_1__3214EC27204DF2A8'. Cannot insert duplicate key in object 'dbo<\/span>.Table_1'. The duplicate key value is (2).<\/span><\/pre>\n
INSERT INTO<\/span> Table_1 (id, name, age) VALUES<\/span> (2, 'John'<\/span>,'30'<\/span>);<\/pre>\n","protected":false},"excerpt":{"rendered":"