postgres how to reset or update index(sequence) of

2019-09-10 06:36发布

问题:

i deleted 2000 row in my table and then i inserted same 2000 records but their index(id auto increment field) starting from 2001, now i want to update those index 2001 - 4000 to 1-2000

回答1:

To update your id run the following command.

UPDATE table SET id = id - 2000;

This will update the id of records in your table and then you need update the table's sequence

ALTER SEQUENCE table_id_seq RESTART WITH 2001;

This will allow you to insert the data with the id 2001

Another way, delete all the records from the table and update the sequence of the table and reinsert the data.

Hope this will be helpful.