简体   繁体   中英

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.

Changing ids is generally a bad idea. The purpose of an id is to identify rows in a table, both for foreign key relationships and over time.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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