renumber primary key

前端 未结 6 1008
天命终不由人
天命终不由人 2021-01-12 03:31

How would I reset the primary key counter on a sql table and update each row with a new primary key?

6条回答
  •  逝去的感伤
    2021-01-12 04:18

    Not sure which DBMS you're using but if it happens to be SQL Server:

    SET IDENTITY_INSERT [MyTable] ON
    

    allows you to update/insert the primary key column. Then when you are done updating the keys (you could use a CURSOR for this if the logic is complicated)

    SET IDENTITY_INSERT [MyTable] OFF
    

    Hope that helps!

提交回复
热议问题