Skip to content
 

Add Primary Key

In order to add a primary key to an existing table we need to use the Alter Table command.

Add a primary key for a new column

ALTER TABLE dbo.Presidents
ADD PresidentID int NOT NULL IDENTITY(1,1) CONSTRAINT PK_PresidentID PRIMARY KEY NONCLUSTERED

Note however that the ordering of the identity field will not be predictable for the records that already exist in the table. For newly inserted records, the identity field will increment properly.

Add a primary key for an existing column

ALTER TABLE dbo.Presidents WITH NOCHECK
ADD CONSTRAINT PK_PresidentNumber PRIMARY KEY NONCLUSTERED (PresidentNumber)

Related Posts:

Ask a question or post a comment