Awesome
Awesome

Reputation: 580

How to add Identity Column in SQL server?

enter image description here

Here is my table and Id is unique constraint...Now I want to add Identity Column in ID .Can anyone help me how to do it with TSQL. This is existing table.

Upvotes: 1

Views: 12399

Answers (2)

Eralper
Eralper

Reputation: 6612

You can rename your previous ID column to keep those values if you need them in your application

EXEC sp_rename 'TableName.id', 'oldid', 'COLUMN';

Then add new ID column with Unique constraint as follows

Alter table TableName Add id int identity(1,1) unique not null

Upvotes: 3

Jayasurya Satheesh
Jayasurya Satheesh

Reputation: 8033

You can only have 1 Identity Column per Table. So if you don't have one already, Just alter the table and add the Column. Like this

ALTER TABLE YourTableName
ADD IdCol INT IDENTITY(1,1)

Upvotes: 7

Related Questions