首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在重命名序列后更改sequence_name参数(PostgreSQL)

如何在重命名序列后更改sequence_name参数(PostgreSQL)
EN

Database Administration用户
提问于 2018-03-19 16:16:20
回答 2查看 3.1K关注 0票数 8

我用以下命令重命名了一个序列:

代码语言:javascript
运行
复制
ALTER SEQUENCE TableIdSeq RENAME TO NewTableIdSeq;

但是,当我发出\d NewTableIdSeq命令时,我得到以下输出:

代码语言:javascript
运行
复制
       Sequence "dev.newtableidseq"
    Column     |  Type   |        Value
---------------+---------+---------------------
 sequence_name | name    | tableidseq            <--------------- HASN'T CHANGED!!
 last_value    | bigint  | 3
 start_value   | bigint  | 1
 increment_by  | bigint  | 1
 max_value     | bigint  | 9223372036854775807
 min_value     | bigint  | 1
 cache_value   | bigint  | 1
 log_cnt       | bigint  | 30
 is_cycled     | boolean | f
 is_called     | boolean | t

如您所见,序列的sequence_name属性仍然设置为旧名称。我怎么才能在这里改名字呢?

EN

回答 2

Database Administration用户

回答已采纳

发布于 2019-06-07 08:00:49

不必为那事担心了。存储在序列中的序列名称将被忽略。这个字段在PostgreSQL 10中消失了。

票数 5
EN

Database Administration用户

发布于 2018-03-19 18:09:42

您应该重命名序列,然后更改该列的默认值。

让我举一个例子:

代码语言:javascript
运行
复制
create sequence tbl_id_seq;

create table tbl 
(
    id int default nextval('tbl_id_seq'), 
    f1 int, 
    f2 int
);

insert into tbl (f1, f2) values (1,2),(3,4),(5,6);    
select * from tbl;

id | f1 | f2
-: | -: | -:
 1 |  1 |  2
 2 |  3 |  4
 3 |  5 |  6

-- rename sequence
alter sequence tbl_id_seq rename to tbl_id_new_seq;

-- alter default value 
alter table tbl alter column id set default nextval('tbl_id_new_seq');

-- restart sequence
alter sequence tbl_id_new_seq restart with 1;

-- check it by inserting new values
insert into tbl (f1, f2) values (1,2),(3,4),(5,6);
select * from tbl;

id | f1 | f2
-: | -: | -:
 1 |  1 |  2
 2 |  3 |  4
 3 |  5 |  6
 1 |  1 |  2
 2 |  3 |  4
 3 |  5 |  6

现在使用\d tbl

代码语言:javascript
运行
复制
+-------------------------------------------------------------------------------+
|                               Table "public.tbl"                              |
+-------------------------------------------------------------------------------+
| Column |   Type  | Collation | Nullable |               Default               |
+--------+---------+-----------+----------+-------------------------------------+
| id     | integer |           |          | nextval('tbl_id_new_seq'::regclass' |
+--------+---------+-----------+----------+-------------------------------------+
| f1     | integer |           |          |                                     |
+--------+---------+-----------+----------+-------------------------------------+
| f2     | integer |           |          |                                     |
+--------+---------+-----------+----------+-------------------------------------+

对于\d tbl_id_new_seq

代码语言:javascript
运行
复制
postgres=# \d tbl_id_new_seq;
Sequence "public.tbl_id_new_seq"
-[ RECORD 1 ]------------------
Type      | bigint
Start     | 1
Minimum   | 1
Maximum   | 9223372036854775807
Increment | 1
Cycles?   | no
Cache     | 1
票数 0
EN
页面原文内容由Database Administration提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://dba.stackexchange.com/questions/201703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档