Table '%1!'' already has a primary key defined on it.

Error Message:
Msg 1779, Level 16, State 0, Line 1
Table ‘%1!” already has a primary key defined on it.

Severity level:
16.

Description:
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.

Consequences:
The T-SQL statement can be parsed, but causes the error at runtime.

Resolution:
Error of the Severity level 16 are generated by the user and can be fixed by the SQL Server user.

Versions:
All versions of SQL Server.

Example(s):
USE tempDB
GO
CREATE TABLE #t
(
 c1 INT PRIMARY KEY
 , c2 INT
)
GO
ALTER TABLE #t
 ADD CONSTRAINT PK_t PRIMARY KEY (c2)

GO
DROP TABLE #t

Remarks:
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.

]]>

Leave a comment

Your email address will not be published.