Faizy
Faizy

Reputation: 297

add auto_increment to an existing db

I'm new to SQL. I created a table and filled it with some data. Now I want to change the properties of my primary key, so it will auto increment every single time by itself when adding data.

I know how to do it when creating a table, but where do I add auto_increment now, when I already have a created table with filled data?

Upvotes: 0

Views: 71

Answers (2)

Muhammad Hassan
Muhammad Hassan

Reputation: 505

Update

Try

ALTER TABLE [TableName] DROP COLUMN ID 
ALTER TABLE [TableName] ADD ID INT IDENTITY(1,1)

If you had entered data to your table review this link that have an answer to you question SQL Server, How to set auto increment after creating a table without data loss?

Upvotes: 1

starko
starko

Reputation: 1149

You can't change logical structure column when you have some filled data in column. That's impossible. You must deleting data from column and use ALTER TABLE to change properites column

Upvotes: 0

Related Questions