Thursday 12 April 2012

adding a new column as primary key in existing table

Today I come across a little tricky situation to add new column to existing table and make it as a primary key in MS SQL server, the solution is luckily simple,


 alter table my_table add [my_new_id] [int] IDENTITY(1,1);  
 alter table my_table add constraint pk_my_id primary key(my_new_id);  


if you already have a primary key, just remove that and add this new primary key.

0 comments:

Post a Comment