Adding column with primary key in existing table

后端 未结 5 1927
感动是毒
感动是毒 2021-02-02 12:29

I am trying to add primary key to newly added column in existing table name Product_Details.

New Column added: Product_Detail_ID (int

5条回答
  •  梦毁少年i
    2021-02-02 12:57

    If you want SQL Server to automatically provide values for the new column, make it an identity.

    ALTER TABLE Product_Details DROP COLUMN Product_Detail_ID
    GO
    ALTER TABLE Product_Details ADD Product_Detail_ID int identity(1,1) not null
    GO
    ALTER TABLE Product_Details
    add CONSTRAINT pk_Product_Detils_Product_Detail_ID primary key(Product_Detail_ID)
    GO
    

提交回复
热议问题