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.




Related Articles :

  • No Related Articles Found

No comments yet... Be the first to leave a reply!

Software Reviews | Book Reviews | FAQs | Tips | Articles | Performance Tuning | Audit | BI | Clustering | Developer | Reporting | DBA | ASP.NET Ado | Views tips | | Developer FAQs | Replication Tips | OS Tips | Misc Tips | Index Tuning Tips | Hints Tips | High Availability Tips | Hardware Tips | ETL Tips | Components Tips | Configuration Tips | App Dev Tips | OLAP Tips | Admin Tips | Software Reviews | Error | Clustering FAQs | Performance Tuning FAQs | DBA FAQs |