简体   繁体   中英

Alter / Update PostgreSQL Sequence

How can I update the last_value field in my sequence and add 1 to it?

桌子

The query I tried: ALTER SEQUENCE "seq_hours" SET 'last_value' = 'last_value' + 1

However this did not work.

Use setval() with a subquery to get the value from a table, eg

SELECT setval('seq_hours', (SELECT max(last_value)+1 FROM t));

EDIT : This solution only makes sense if you want to set the current value of a sequence based on a value from a given table. If you only want the next possible value of a sequence you should use nextval as @a_horse_with_no_name suggested (see comments)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM