在 postgres 中重置自动增量计数器 [英] Reset auto increment counter in postgres

查看:31
本文介绍了在 postgres 中重置自动增量计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将表的自动增量字段强制为某个值,我尝试过:

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

ALTER SEQUENCE product  RESTART WITH 1453;
ERROR:  relation "your_sequence_name" does not exist

我是 postgres 的新手 :(

I'm new to postgres :(

我有一个表 product 带有 Idname 字段

I have a table product with Id and name field

推荐答案

如果您创建的表 product 带有一个 id 列,那么该序列不会被简单地称为 product,而是product_id_seq(即${table}_${column}_seq).

If you created the table product with an id column, then the sequence is not simply called product, but rather product_id_seq (that is, ${table}_${column}_seq).

这是您需要的 ALTER SEQUENCE 命令:

This is the ALTER SEQUENCE command you need:

ALTER SEQUENCE product_id_seq RESTART WITH 1453

您可以使用 psql 中的 ds 命令查看数据库中的序列.如果您执行 d product 并查看您的列的默认约束,nextval(...) 调用也会指定序列名称.

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, the nextval(...) call will specify the sequence name too.

这篇关于在 postgres 中重置自动增量计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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