Adding an identity to an existing column

后端 未结 19 2067
温柔的废话
温柔的废话 2020-11-21 13:16

I need to change the primary key of a table to an identity column, and there\'s already a number of rows in table.

I\'ve got a script to clean up the IDs to ensure

19条回答
  •  遇见更好的自我
    2020-11-21 13:51

    I'm a java developer that happened to get on a team without a DBA and one where as a developer, I can't get DBA rights. I was tasked with moving an entire schema between two databases, so without having a DBA, I had to do it and do it by running scripts, not being able to use the GUI in SQL Server 2008 because I didn't have admin privileges.

    Everything was moved without issue, however, when running a stored procedure on the new schema.table, I found I lost the identity field in a table. I double checked the script that created the table and it was there, however, SQL Server didn't get it when I ran the script. I was told later by a DBA that he had seen this same problem before.

    In any event, for SQL Server 2008, these are the steps I took to get this resolved and they worked, so I'm posting this here in the hopes it will be a help to someone. This is what I did as I had FK dependencies on another table that made this more difficult:

    I used this query to verify the identity was indeed missing and to view dependencies on the table.

    1.) Find statistics on a table:

    exec sp_help 'dbo.table_name_old';
    

    2.) Create a duplicate, identical new table, except add an identity field on the PK field where it had been before.

    3.) Disable the identity to move data.

    SET IDENTITY_INSERT dbo.table_name ON 
    

    4.) Transfer the data.

    INSERT INTO dbo.table_name_new
    (
    field1, field2, etc...
    )
    SELECT 
    field1, field2, etc...
    FROM 
    dbo.table_name_old;
    

    5.) Verify the data is there.

    SELECT * FROM dbo.table_name_new
    

    6.) Re-enable the identity.

    SET IDENTITY_INSERT ToyRecP.ToyAwards.lkpFile_New OFF
    

    7.) This is the best script I found to get all the FK relationships to verify which table(s) the original table references as dependencies and I came across many, so it is a keeper!

    SELECT f.name AS ForeignKey,
       OBJECT_NAME(f.parent_object_id) AS TableName,
       COL_NAME(fc.parent_object_id, fc.parent_column_id) AS ColumnName,
       OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
       COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS ReferenceColumnName
    FROM sys.foreign_keys AS f
    INNER JOIN sys.foreign_key_columns AS fc
       ON f.OBJECT_ID = fc.constraint_object_id
       ORDER BY ReferenceTableName;
    

    8.) Make sure you have all the PK and FK scripts for all the tables involved, before this next step.

    9.) You can right-click on each key and script this using SQL Server 2008

    10.) Drop the FK(s) from the dependency table(s) using this syntax:

    ALTER TABLE [dbo].[table_name] DROP CONSTRAINT [Name_of_FK]
    

    11.) Drop the original table:

    DROP TABLE dbo.table_name_old;
    

    13.) These next steps rely on the scripts you created in SQL Server 2008 in step 9.

    --Add the PK to the new table.

    --Add the FK to the new table.

    --Add the FK's back to the dependency table.

    14.) Verify everything is correct and complete. I used the GUI to look at the tables.

    15.) Rename the new table to the original tables name.

    exec sp_RENAME '[Schema_Name.OldTableName]' , '[NewTableName]';
    

    Finally, everything worked!

提交回复
热议问题