Msg 1779, Level 16, State 0, Line 1
Table ‘%1!” already has a primary key defined on it.
This error message appears, when you try to create a PRIMARY KEY constraint for a table, for which there is already a PRIMARY KEY declared.
The T-SQL statement can be parsed, but causes the error at runtime.
Error of the Severity level 16 are generated by the user and can be fixed by the SQL Server user.
All versions of SQL Server.
CREATE TABLE #t
c1 INT PRIMARY KEY
, c2 INT
ALTER TABLE #t
ADD CONSTRAINT PK_t PRIMARY KEY (c2)
DROP TABLE #t
The above example create the temporary table #t. In the CREATE TABLE block is a PRIMARY KEY constraint declared on the column c1. A little bit below in the ALTER TABLE Statement we try to create a second PRIMARY KEY constraint on the column c2. This raises the error. Possible solutions might be to create a composite PRIMARY KEY on (c1, c2), change the PK constraint from c1 to c2 or to refrain from the creation of the PK constraint on c2. Instead of a PK constraint, you might create a UNIQUE constraint or an UNIQUE index on c2.