Auto increment primary key in SQL Server Management Studio 2012

Ledgemonkey picture Ledgemonkey · Jun 12, 2012 · Viewed 1.2M times · Source

How do I auto increment the primary key in a SQL Server database table, I've had a look through the forum but can't see how.

I've looked the the properties but can't see an option, I have seen an answer where you go to the Identity specification property and set it to yes and set the Identity increment to 1, but that section is grayed out and I can't change the no to yes.

There must be a simple way to do this but I can't find it.

Answer

Raab picture Raab · Jun 12, 2012

Make sure that the Key column's datatype is int and then setting identity manually, as image shows

enter image description here

Or just run this code

-- ID is the name of the  [to be] identity column
ALTER TABLE [yourTable] DROP COLUMN ID 
ALTER TABLE [yourTable] ADD ID INT IDENTITY(1,1)

the code will run, if ID is not the only column in the table

image reference fifo's