开发者

Resetting auto-increment column back to 0 daily

Is there a way in po开发者_如何学Cstgresql to have an auto-incrementing column reset back to zero at a specified time every day?


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.


Basicaly you can reset sequence with this one:

ALTER SEQUENCE your_sequence_name RESTART WITH 1;

Enjoy...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜