更改 Postgres 序列的最小值

发布于 2024-10-30 01:37:13 字数 358 浏览 5 评论 0原文

我正在尝试更改现有序列的最小值。

首先,我尝试

ALTER SEQUENCE product_id_seq MINVALUE 10000;

并得到错误:START值(1)不能小于MINVALUE (10000)

所以我尝试

ALTER SEQUENCE product_id_seq MINVALUE 10000 RESTART WITH 10000;

但得到了同样的错误。

当然,我可以放弃它并创建一个新的,但我认为应该有一种方法可以做到这一点。我正在使用 Postgres 8.4.7。

I am trying to change minimum value of an existing sequence.

First I tried

ALTER SEQUENCE product_id_seq MINVALUE 10000;

and I got ERROR: START value (1) cannot be less than MINVALUE (10000).

So I tried

ALTER SEQUENCE product_id_seq MINVALUE 10000 RESTART WITH 10000;

but got the same error.

Of course I could just drop it and create a new one, but I think there should be a way to do this. I am using Postgres 8.4.7.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巴黎夜雨 2024-11-06 01:37:13

如何一次设置它们

ALTER SEQUENCE product_id_seq
MINVALUE 10000
START 10000
RESTART 10000;

这应该改变最小值、起始值和当前值将所有值设置为 10000,从而使所有内容保持一致。

How about setting them all at once:

ALTER SEQUENCE product_id_seq
MINVALUE 10000
START 10000
RESTART 10000;

That should change the minimum, starting, and current values all to 10000 and thus make everything consistent.

情痴 2024-11-06 01:37:13

PostgreSQL 有几个对序列进行操作的函数。除了此处的其他指南之外,您还可以使用

SELECT setval('product_id_seq ', 10000); -- Next nextval() returns 10001

PostgreSQL has several functions that operate on sequences. In addition to the other guidance here, you could use

SELECT setval('product_id_seq ', 10000); -- Next nextval() returns 10001
数理化全能战士 2024-11-06 01:37:13

我做了以下测试,我的版本是9.0。

--create sequence
skytf=> CREATE SEQUENCE seq_test
skytf->     START WITH 1
skytf->     INCREMENT BY 1
skytf->     NO MINVALUE
skytf->     NO MAXVALUE
skytf->     CACHE 1;
CREATE SEQUENCE

skytf=> \d seq_test
           Sequence "skytf.seq_test"
    Column     |  Type   |        Value        
---------------+---------+---------------------
 sequence_name | name    | seq_test
 last_value    | bigint  | 1
 start_value   | bigint  | 1
 increment_by  | bigint  | 1
 max_value     | bigint  | 9223372036854775807
 min_value     | bigint  | 1
 cache_value   | bigint  | 1
 log_cnt       | bigint  | 1
 is_cycled     | boolean | f
 is_called     | boolean | f


skytf=> select nextval('seq_test');
 nextval 
---------
       1
(1 row)

--alter sequence 
skytf=> alter sequence seq_test  restart with 100;
ALTER SEQUENCE
skytf=> \d seq_test
           Sequence "skytf.seq_test"
    Column     |  Type   |        Value        
---------------+---------+---------------------
 sequence_name | name    | seq_test
 last_value    | bigint  | 100
 start_value   | bigint  | 1
 increment_by  | bigint  | 1
 max_value     | bigint  | 9223372036854775807
 min_value     | bigint  | 1
 cache_value   | bigint  | 1
 log_cnt       | bigint  | 1
 is_cycled     | boolean | f
 is_called     | boolean | f

skytf=> select nextval('seq_test');
 nextval 
---------
     100
(1 row)

I Have done the following test, My version is 9.0.

--create sequence
skytf=> CREATE SEQUENCE seq_test
skytf->     START WITH 1
skytf->     INCREMENT BY 1
skytf->     NO MINVALUE
skytf->     NO MAXVALUE
skytf->     CACHE 1;
CREATE SEQUENCE

skytf=> \d seq_test
           Sequence "skytf.seq_test"
    Column     |  Type   |        Value        
---------------+---------+---------------------
 sequence_name | name    | seq_test
 last_value    | bigint  | 1
 start_value   | bigint  | 1
 increment_by  | bigint  | 1
 max_value     | bigint  | 9223372036854775807
 min_value     | bigint  | 1
 cache_value   | bigint  | 1
 log_cnt       | bigint  | 1
 is_cycled     | boolean | f
 is_called     | boolean | f


skytf=> select nextval('seq_test');
 nextval 
---------
       1
(1 row)

--alter sequence 
skytf=> alter sequence seq_test  restart with 100;
ALTER SEQUENCE
skytf=> \d seq_test
           Sequence "skytf.seq_test"
    Column     |  Type   |        Value        
---------------+---------+---------------------
 sequence_name | name    | seq_test
 last_value    | bigint  | 100
 start_value   | bigint  | 1
 increment_by  | bigint  | 1
 max_value     | bigint  | 9223372036854775807
 min_value     | bigint  | 1
 cache_value   | bigint  | 1
 log_cnt       | bigint  | 1
 is_cycled     | boolean | f
 is_called     | boolean | f

skytf=> select nextval('seq_test');
 nextval 
---------
     100
(1 row)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文