Home >Database >Mysql Tutorial >How to Make an Existing Column an Identity Column in SQL Server?
Transforming an Existing Column into an Identity Column in SQL Server
Modifying a table's primary key to an identity column, particularly when the table already contains significant data, requires careful consideration. This guide outlines the best practices for this process.
SQL Solutions for Adding Identity Properties:
Directly altering an existing column to become an identity column isn't feasible in SQL Server. However, two effective workarounds exist:
Construct a New Table with Identity:
<code class="language-sql">CREATE TABLE dbo.Tmp_Names ( Id int NOT NULL IDENTITY(1, 1), Name varchar(50) NULL )</code>
Populate the new table with data from the original table, preserving existing data. Finally, replace the old table with the new one.
Introduce a New Identity Column:
<code class="language-sql">ALTER TABLE Names ADD Id_new Int IDENTITY(1, 1)</code>
Remove the original column and rename the new identity column to match the old primary key.
Important Notes:
The above is the detailed content of How to Make an Existing Column an Identity Column in SQL Server?. For more information, please follow other related articles on the PHP Chinese website!