Alter table column for primary key and identity

后端 未结 2 1122
甜味超标
甜味超标 2021-02-20 15:47

I have table created and want to alter that table. I want to add a primary key and identity(1,1).

I can apply primary key but applying identity gives error.

2条回答
  •  爱一瞬间的悲伤
    2021-02-20 16:01

    I found this because I was looking for the same answer to assign IDENTITY programmatically to a table in SQL Server 2019. My situation is that the table I was trying to apply this to was an imported table where the column that I was attempting to apply IDENTITY to had IDENTITY in the table where it was imported from so it met the requirements of IDENTITY - each value was unique and had the correct data type. This and other research helped me to understand I could not do this programmatically.

    But it does not prevent me from actually accomplishing the result. Instead of programmatically, I can open the table in design view in SSMS and assign IDENTITY there so long as seed and identity interval match the original setup of the table. Obviously, not as quick a solution if a lot of tables are involved but protects the integrity of IDENTITY values if they are used as Foreign Keys in other associated tables.

提交回复
热议问题