将自动递增列重置为每天0 [英] Resetting auto-increment column back to 0 daily

查看:326
本文介绍了将自动递增列重置为每天0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在postgresql中有一种方法让自动递增列在每天的指定时间重置为零。

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

推荐答案

p>

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

或者,您可以将serial列设置为DEFAULT,存储过程,这将检查一天的翻转,重置序列,如果适当,然后返回的结果nextval()。

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().

但除此之外,不,我不会期望有一个神奇的ALTER序列my_seq重置AT INERVAL'1天'或任何类似的。

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.

编辑:合并duckyfuzz的评论。

incorporated duckyfuzz's comment.

这篇关于将自动递增列重置为每天0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆