Violation of … constraint ‘…’. Cannot insert duplicate key in object ‘…’ (Msg 2627)

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 date);

The user created the new table Table_1, and started to fill it adding a row:

INSERT INTO Table_1 (id, name, age) VALUES (1, 'John','30');

then the next row:

INSERT INTO Table_1 (id, name, age) VALUES (1, 'John','30');

After execution of this command the following error appeared:

Msg 2627, Level 14, State 1, Line 10
Violation of PRIMARY KEY constraint 'PK__Table_1__3214EC27204DF2A8'. Cannot insert duplicate key in object 'dbo.Table_1'. The duplicate key value is (2).

This message says that the user tries to insert duplicate values into a unique column. To avoid this error just select an unique value, for example, 2:

INSERT INTO Table_1 (id, name, age) VALUES (2, 'John','30');