如何将第二个串行整数键列添加到表中? (PostgreSQL的) [英] How can I add a 2nd serial integer key column to a table? (postgresql)
问题描述
我有一个表,其中包含8440个带有自然(字符串)主键的记录。现在,我刚刚发现,为了支持旧版客户端,我还需要记录也具有整数键。将表中的INT列添加到具有唯一约束并用1到8440之间的整数填充的最简单方法是什么?
I have a table with 8440 records with a natural (string) primary key. Now I just discovered that to support a legacy client, I need the records to have integer keys as well. what's the easiest way to add a serial INT column to this table with a unique constraint and populate it with integer values from 1 to 8440?
推荐答案
更改表,添加一个新的类型为 serial
的非空列,并在其上添加一个唯一键。
Alter the table, add a new not null column of type serial
, with a unique key on it.
在Postgres中,序列类型只是int类型的别名,默认值为nextval(some_sequence),后者是在运行时创建的。
In Postgres, the serial type is a mere alias for the int type with a default value of nextval(some_sequence), the latter of which is created on the fly.
这篇关于如何将第二个串行整数键列添加到表中? (PostgreSQL的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!