重置 postgres 中的自动增量计数器
我想强制表的自动增量字段为某个值,我尝试这样做:
ALTER TABLE product AUTO_INCREMENT = 1453
并且
ALTER SEQUENCE product RESTART WITH 1453;
ERROR: relation "your_sequence_name" does not exist
我有一个表 product
,其中包含 Id
和 name字段
I would like to force the auto increment field of a table to some value, I tried with this:
ALTER TABLE product AUTO_INCREMENT = 1453
AND
ALTER SEQUENCE product RESTART WITH 1453;
ERROR: relation "your_sequence_name" does not exist
I have a table product
with Id
and name
field
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
如果您创建了带有
id
列的表product
,则序列不会简单地称为product
,而是product_id_seq
> (即${table}_${column}_seq
)。这是您需要的
ALTER SEQUENCE
命令:您可以使用 psql 中的
\ds
命令查看数据库中的序列。如果您执行\d Product
并查看列的默认约束,则nextval(...)
调用也将指定序列名称。If you created the table
product
with anid
column, then the sequence is not simply calledproduct
, but ratherproduct_id_seq
(that is,${table}_${column}_seq
).This is the
ALTER SEQUENCE
command you need:You can see the sequences in your database using the
\ds
command in psql. If you do\d product
and look at the default constraint for your column, thenextval(...)
call will specify the sequence name too.以下命令会自动为您执行此操作:这还将删除表中的所有数据。所以要小心。
The following command does this automatically for you: This will also delete all the data in the table. So be careful.
这是您正在查找的命令,假设您的产品表序列是product_id_seq:
Here is the command that you are looking for, assuming your sequence for the product table is product_id_seq:
设置序列计数器:
如果您不知道序列名称,请使用 pg_get_serial_sequence 函数:
参数是表名和列名。
或者只需在
psql
提示符下发出\d Product
即可:To set the sequence counter:
If you don't know the sequence name use the
pg_get_serial_sequence
function:The parameters are the table name and the column name.
Or just issue a
\d product
at thepsql
prompt:相同但动态:
SELECT
的使用很奇怪,但它有效。来源:
https://kylewbanks.com/blog/Adding-or -Modifying-a-PostgreSQL-Sequence-Auto-Increment
编辑:按照评论中的建议删除了
+1
Same but dynamic :
The use of a
SELECT
is weird but it works.Source:
https://kylewbanks.com/blog/Adding-or-Modifying-a-PostgreSQL-Sequence-Auto-Increment
Edit: removed
+1
as suggested in the comments要将其设置为下一个最高值,您可以使用:
To set it to the next highest value you can use:
如果您有一个包含 IDENTITY 列的表,并且想要为其重置下一个值,可以使用以下命令:
If you have a table with an IDENTITY column that you want to reset the next value for you can use the following command:
为了访问者的方便而从评论转换
从该消息中并不清楚正确的语法是什么。这是:
Converted from comment for the sake of visitor's convenience
It's not clear from this message what the correct syntax is. It is:
2021 年,Postgres 11.12
ALTER SEQUENCE 对我不起作用,它以某种方式将其重置为 null。
对我有用的是:
Year 2021, Postgres 11.12
ALTER SEQUENCE did not worked for me, it resets it to null somehow.
What worked for me is:
如果您想从 GUI 重置自动增量,请按照以下步骤操作。
if you want to Reset auto increment from GUI, then follow this steps.
要重置自动增量,您必须使用以下查询获取序列名称。
语法:
示例:
查询将返回 autoid 的序列名称为 "Demo_autoid_seq"
然后使用以下查询重置 autoid
语法:
示例:
To reset the auto increment you have to get your sequence name by using following query.
Syntax:
Example:
The query will return the sequence name of autoid as "Demo_autoid_seq"
Then use the following query to reset the autoid
Syntax:
Example:
要获取序列ID,请使用
这将为您提供序列ID,如tableName_ColumnName_seq
要获取最后一个种子号,请使用
或者如果您知道序列ID已经直接使用它。
它将为您提供最后的种子号码
重置种子号码使用
To get sequence id use
This will gives you sequesce id as tableName_ColumnName_seq
To Get Last seed number use
or if you know sequence id already use it directly.
It will gives you last seed number
To Reset seed number use
使用此查询检查架构和表的序列键是什么,
使用此查询将增量值一一增加,
当插入到表时,下一个增量值将用作键(111 )。
使用此查询将特定值设置为增量值
插入表时,下一个增量值将用作键(121)。
Use this query to check what is the Sequence Key with Schema and Table,
Use this query increase increment value one by one,
When inserting to table next incremented value will be used as the key (111).
Use this query to set specific value as the incremented value
When inserting to table next incremented value will be used as the key (121).
我不确定以上所有答案,
如果我没有序列名称怎么办?
如果我不想截断我的表怎么办?
下面的查询帮助我在不影响现有数据的情况下做到了这一点。
I am not sure about all of the above answers,
What if I don't have a sequence name?
What if I don't want to truncate my table?
Below query helped me to do that without affecting the existing data.
这个查询对我有用。 PostgreSQL 版本 14
this query worked for me. Postgresql version 14
如果表像
在
0-9
范围内插入一些记录后,会导致下一次insert
发生冲突,因此要重置START:If table is like
After inserting some records in the range of
0-9
, will cause conflict for nextinsert
, so to reset the START:节点脚本:修复所有表标识:自动增量/nextval,基于上次插入的值。
Node script: Fix all tables identity: auto-increment / nextval, based on last inserted it.
请注意,如果表名带有“_”,它将在序列名称中删除。
例如表名:user_tokens 列:id
序列名称:usertokens_id_seq
Note that if you have table name with '_', it is removed in sequence name.
For example, table name: user_tokens column: id
Sequence name: usertokens_id_seq