简体   繁体   English

重置列自动递增

[英]Reset Column auto-increment

I have a table where a certain column has active auto-increment, but I did some tests and during the test I created several records and exclude some of them. 我有一个表,其中某个列具有活动的自动增量,但是我进行了一些测试,在测试期间,我创建了多个记录,并排除了其中一些记录。 I would like to generate new numbers for those who stayed, without having to delete ... Ex: they were 3 record, one with code 5, the other with 16 and the other with 18. I wanted the first one to be 1, the second 2 and the third 3. Of course the new ones follow the sequence. 我想为那些留下来的人生成新的数字,而不必删除...例如:它们是3条记录,一个记录为5,另一个记录为16,另一个记录为18。我希望第一个记录为1。第二个和第三个3.当然,新的遵循该顺序。

Changing ids is generally a bad idea. 更改ID通常是一个坏主意。 The purpose of an id is to identify rows in a table, both for foreign key relationships and over time. id的目的是为了识别表中的行,以了解外键关系以及随着时间的推移。

But you can change the numbers if you need to: 但是,如果需要,您可以更改数字:

with toupdate as (
      select t.*, row_number() over (order by id) as new_id
      from t
     )
update toupdate
    set id = new_id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM