将PostgreSQL主键重置为1 [英] Reset PostgreSQL primary key to 1
问题描述
有没有一种方法可以重置PostgreSQL表的主键,使其在填充表上再次从1开始?
Is there a way to reset the primary key of a PostgreSQL table to start at 1 again on a populated table?
现在它从1000000起生成数字。我希望所有这些都重置并开始为1,保持所有现有数据不变。
Right now it's generating numbers from 1000000 and up. I want it all to reset and start to 1, keeping all my existing data intact.
推荐答案
自动递增的主键(即,数据类型为串行主键
的列与序列。您可以使用 setval(< seqname> ;,< next_value>)
函数为任何序列设置下一个值。请注意,要真正单独执行该功能,您需要使用 SELECT
,如下所示: SELECT setval(< seqname> ;,< next_value>)
Primary keys that autoincrement (i.e., columns with data type serial primary key
) are associated with a sequence. You can set the next value for any sequence using the setval(<seqname>, <next_value>)
function. Note that to actually execute the function by itself you need to use SELECT
, like this: SELECT setval(<seqname>, <next_value>)
使用序列时自动创建的序列的名称为< table> _< column> _seq
The name of the auto created sequences when using serial are <table>_<column>_seq
这篇关于将PostgreSQL主键重置为1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!