[英]How to convert primary key from integer to serial?
In a Postgres 9.3 table I have an integer
as primary key with automatic sequence to increment, but I have reached the maximum for integer
.在 Postgres 9.3 表中,我有一个
integer
作为主键,并自动递增,但我已经达到了integer
的最大值。 How to convert it from integer
to serial
?如何将其从
integer
转换为serial
?
I tried:我试过:
ALTER TABLE my_table ALTER COLUMN id SET DATA TYPE bigint;
But the same does not work with the data type serial
instead of bigint
.但同样不适用于数据类型
serial
而不是bigint
。 Seems like I cannot convert to serial
?好像我不能转换为
serial
?
serial
is a pseudo data type, not an actual data type. serial
是一种伪数据类型,而不是实际的数据类型。 It's an integer
underneath with some additional DDL commands executed automatically:它是一个
integer
下面是一些自动执行的额外 DDL 命令:
NOT NULL
and the default to draw from that sequence.NOT NULL
和默认值以从该序列中绘制。 Details:细节:
A bigserial
is the same, built around a bigint
column. bigserial
是相同的,围绕bigint
列构建。 You want bigint
, but you already achieved that.你想要
bigint
,但你已经做到了。 To transform an existing serial
column into a bigserial
(or smallserial
), all you need to do is to ALTER
the data type of the column.要将现有的
serial
列转换为bigserial
(或smallserial
),您需要做的就是ALTER
列的数据类型。 Sequences are generally based on bigint
, so the same sequence can be used for any integer
type.序列通常基于
bigint
,因此相同的序列可用于任何integer
类型。
To "change" a bigint
into a bigserial
or an integer
into a serial
, you just have to do the rest by hand:要将
bigint
“更改”为bigserial
或将integer
“更改”为serial
,您只需手动完成其余工作:
The actual data type is still integer
/ bigint
.实际的数据类型仍然是
integer
/ bigint
。 Some clients like pgAdmin will display the data type serial
in the reverse engineered CREATE TABLE
script, if all criteria for a serial
are met.如果满足
serial
所有条件,一些客户端(如 pgAdmin)将在反向工程的CREATE TABLE
脚本中显示数据类型serial
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.