leora
leora

Reputation: 196499

is there anyway to add a primary autonumber key to an already existing database table

i have inherited a database application from someone else and there are a few tables that dont have any primary keys. I want to add a new column into an already existing table and have it autonumber (starting from 1). how would i go about doing this?

Upvotes: 3

Views: 5476

Answers (1)

Aaronaught
Aaronaught

Reputation: 122634

The SQL Server syntax is:

ALTER TABLE SomeTable ADD
    ID int NOT NULL IDENTITY(1, 1),
    CONSTRAINT PK_SomeTable PRIMARY KEY [NON]CLUSTERED (ID)

Upvotes: 5

Related Questions