ReNumber Auto Increment column in SQL

为君一笑 提交于 2019-12-23 19:05:18

问题


Heres my problem.

I have an autoincrement primary key in sql table lets say table look like this

ID  |  Name|

1     John

2     Jack

3     Bill 

4     Joe

Then i delete row 2 Jack

ID  |  Name|

1     John


3     Bill 

4     Joe

And what I want to achieve is to change id column so the table will look like this

ID  |  Name|

1     John


2     Bill 

3     Joe

Is there a way to do it ? thanks in advance


回答1:


I will never do that but you can:

  1. create a new autoincrement primary key named ID2
  2. delete ID column
  3. rename ID2 column as ID



回答2:


Set identity_insert Table off;
Update Table set ID = 3 where ID = 4;
...
Set identity_insert Table on;

Where Table name is Table



来源:https://stackoverflow.com/questions/16691885/renumber-auto-increment-column-in-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!