postgres how to reset or update index(sequence) of table

痞子三分冷 提交于 2020-01-06 15:01:39

问题


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.



来源:https://stackoverflow.com/questions/34718124/postgres-how-to-reset-or-update-indexsequence-of-table

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