简体   繁体   English

如何将主键从整数转换为串行?

[英]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 命令:

  1. Create a sequence (with matching name by default).创建一个序列(默认名称匹配)。
  2. Set the column NOT NULL and the default to draw from that sequence.设置列NOT NULL和默认值以从该序列中绘制。
  3. Make the column "own" the sequence.使列“拥有”序列。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM