Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a column to an IDENTITY column and preserve the data?

I had a column which was IDENTITY(1,1). All rows had been numbered consecutively. I did not add or remove any rows. I removed the identity property on this column. Now, I want it back. I think i can reset it to identity with seed = biggest number in existing table.

Could any problems arise if I do this ?

like image 374
Trojan.ZBOT Avatar asked Nov 24 '25 15:11

Trojan.ZBOT


1 Answers

From MSDN SQL Server forum - You have 2 options:

  1. Create a new table with identity & drop the existing table
  2. Create a new column with identity & drop the existing column.

*But take special care when these columns have any constraints / relations

As stated in MSDN documentation about considerations for preserving data during deployment and schema Updates:

Changes to properties of an identity column on a table, such as the identity seed - "Requires data motion" and it can, in some cases, cause data loss during its execution of the synchronization.

The recommendation in this case: "If the deployment or update script contains these types of changes, you might want to modify it manually to preserve the data"

like image 123
3 revsYosi Avatar answered Nov 26 '25 05:11

3 revsYosi