Resetting auto-increment column back to 0 daily

微笑、不失礼 提交于 2019-12-07 07:58:41

问题


Is there a way in postgresql to have an auto-incrementing column reset back to zero at a specified time every day?


回答1:


It could be pretty trivial with a cronjob

0 0 * * * echo "SELECT setval('public.my_table_id_seq', 1, false)" | psql -U my_db_user -d my_db_name

Alternately, you could set your "serial" column DEFAULT to call a stored procedure, which would check for a day rollover, reset the sequence if appropriate, and then return the result of nextval().

But other than that, no, I wouldn't expect that there's a magic ALTER SEQUENCE my_seq RESET AT INERVAL '1 day' or anything like that.

Edit: incorporated duckyfuzz's comment.




回答2:


Basicaly you can reset sequence with this one:

ALTER SEQUENCE your_sequence_name RESTART WITH 1;

Enjoy...



来源:https://stackoverflow.com/questions/2025236/resetting-auto-increment-column-back-to-0-daily

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