How to add a new identity column to a table in SQL Server?

后端 未结 3 1077
执念已碎
执念已碎 2020-12-31 00:11

I am using SQL Server 2008 Enterprise. I want to add an identity column (as unique clustered index and primary key) to an existing table. Integer based auto-increasing by 1

3条回答
  •  独厮守ぢ
    2020-12-31 00:41

    have 1 approach in mind, but not sure whether it is feasible at your end or not. But let me assure you, this is a very effective approach. You can create a table having an identity column and insert your entire data in that table. And from there on handling any duplicate data is a child's play. There are two ways of adding an identity column to a table with existing data:

    Create a new table with identity, copy data to this new table then drop the existing table followed by renaming the temp table.
    
    Create a new column with identity & drop the existing column
    

    For reference the I have found 2 articles : http://blog.sqlauthority.com/2009/05/03/sql-server-add-or-remove-identity-property-on-column/ http://cavemansblog.wordpress.com/2009/04/02/sql-how-to-add-an-identity-column-to-a-table-with-data/

提交回复
热议问题