Adding auto incrementing primary key to an existing table in SQL SERVER 2005

前端 未结 5 1495
逝去的感伤
逝去的感伤 2021-01-14 11:26

I have a table with 8,000 rows of data and will be adding more. but I forgot to put a primary key in the beginning. so that each row has a unique key. later i added a primar

5条回答
  •  佛祖请我去吃肉
    2021-01-14 12:04

    Get rid of the column you just added. Then run this

    ALTER TABLE table
    ADD ID INT IDENTITY
    
    ALTER TABLE table
    ADD CONSTRAINT PK_table
    PRIMARY KEY(ID)
    

提交回复
热议问题