I have created a table and forgot to set auto-increment to "id" column.
As that column is "primary key" now I'm not able to alter column (change the identity).
What could I do to add the auto-increment option?
I'm using Microsoft SQL Server.
I have created a table and forgot to set auto-increment to "id" column.
As that column is "primary key" now I'm not able to alter column (change the identity).
What could I do to add the auto-increment option?
I'm using Microsoft SQL Server.
Follow the below steps.
Remove the primary key from the column.
Add a new column with auto increment identity value
Update the related tables with new column values where ever the old column is referred as foreign key.
Drop the old column
Rename the new column with dropped column name.
Set it as primary key.
Make sure all other constraints are in place
First you need to drop the "id" column.
ALTER TABLE <Your_Table>
DROP COLUMN id
Then you need to recreate the "id" column, but now with the IDENTITY constraint.
ALTER TABLE <Your_Table>
ADD id INT NOT NULL IDENTITY PRIMARY KEY