gpt4 book ai didi

postgresql - 在 Postgresql 中,序列号设置为从 1 开始,但实际上返回的是 ID 为 2 的新记录?

转载 作者:行者123 更新时间:2023-11-29 12:36:11 25 4
gpt4 key购买 nike

使用 setval('sequence',1) 将序列的起始值设置为 1。但是当插入记录时,第一个 'sequence' 编号实际上是 2。

如何让实际的第一条记录的序号为 1?

最佳答案

来自fine manual :

setval
Reset the sequence object's counter value. The two-parameter form sets the sequence's last_value field to the specified value and sets its is_called field to true, meaning that the next nextval will advance the sequence before returning a value. [...]

SELECT setval('foo', 42);           Next nextval will return 43
SELECT setval('foo', 42, true); Same as above
SELECT setval('foo', 42, false); Next nextval will return 42

因此调用 setval('sequence', 1) 将序列的当前值设置为 1下一个 值将是 2。您可能需要 setval 的三个参数形式:

setval('sequence', 1, false)

因此序列上的 is_called 标志将为 false,并且 nextval('sequence') 将为 1。另请注意,绑定(bind)到序列的列的默认值为 nextval('sequence')

关于postgresql - 在 Postgresql 中,序列号设置为从 1 开始,但实际上返回的是 ID 为 2 的新记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41150544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com