PostgreSQL中如何设置自增主键?
我在 PostgreSQL 中有一个包含很多列的表,我想添加一个自动增量主键。
我尝试创建一个名为 id
类型为 BIGSERIAL
的列,但 pgadmin 响应错误:
错误:序列必须与其链接到的表具有相同的所有者。
有谁知道如何解决这个问题?如何在 PostgreSQL 中添加或创建自动递增主键而不重新创建表?
I have a table in PostgreSQL with many columns, and I want to add an auto increment primary key.
I tried to create a column called id
of type BIGSERIAL
but pgadmin responded with an error:
ERROR: sequence must have same owner as table it is linked to.
Does anyone know how to fix this issue? How do I add or create an auto-incrementing primary key in PostgreSQL without recreating the table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
尝试此命令:
使用与您创建表相同的数据库用户进行尝试。
Try this command:
Try it with the same DB-user as the one you have created the table.
postgresql 中自动递增主键:
创建表:
将值插入表中:
从表中选择 *:
观察 mytable_key 列已被自动递增。
Postgresql COPY 不观察序列
自动递增序列键不会像 COPY 命令那样通过从文件批量导入数据来断言自身。你会得到一个
psql:错误:重复的键值违反了唯一约束
。解决此问题的一种方法是将其复制到新表中,然后使用插入:使用
COPY
观察mytable_key
上发生自动递增。专业提示:
您应该始终在表上使用主键,因为 postgresql 内部使用哈希表结构来提高插入、删除、更新和选择的速度。如果主键列(强制唯一且非空)可用,则可以依赖它为哈希函数提供唯一的种子。如果没有可用的主键列,则哈希函数会变得低效,因为它会选择其他一些列集作为键。
如果您想更多地控制序列键的行为,请参阅 postgresql 序列。
Auto incrementing primary key in postgresql:
Create your table:
Insert values into your table:
select * from your table:
Observe that mytable_key column has been auto incremented.
Postgresql COPY doesn't observe serials
An auto incrementing serial key doesn't assert itself with bulk imports of data from file, like
COPY
command does. You'll get apsql:ERROR: duplicate key value violates unique constraint
. One way to fix that is copy it into a new table then use insert thustly:Observe Auto-incrementing happens on
mytable_key
withCOPY
.ProTips:
You should always be using a primary key on your table because postgresql internally uses hash table structures to increase the speed of inserts, deletes, updates and selects. If a primary key column (which is forced unique and non-null) is available, it can be depended on to provide a unique seed for the hash function. If no primary key column is available, the hash function becomes inefficient as it selects some other set of columns as a key.
If you want more control over the behavior of the serial key, then see postgresql sequences.
serial
是自动生成唯一值的旧方法,它不是SQL
标准的一部分。在
PostgreSQL 10
之后,您可以使用生成的身份
,它符合SQL
标准:或者
默认和始终之间的区别:
GENERATED ALWAYS AS IDENTITY
列中,PostgreSQL
将发出错误。GENERATED BY DEFAULT
还指示PostgreSQL
为标识列生成一个值。但是,如果您为插入或更新提供值,PostgreSQL
将使用该值插入到标识列中,而不是使用系统生成的值。了解更多信息。
serial
is the old way to auto generate unique values and it is not part of theSQL
standard.After
PostgreSQL 10
, you can usegenerated as identity
, it is compliant withSQL
standard:or
The difference between by default and always:
GENERATED ALWAYS
instructsPostgreSQL
to always generate a value for the identity column. If you attempt to insert (or update) values into theGENERATED ALWAYS AS IDENTITY
column,PostgreSQL
will issue an error.GENERATED BY DEFAULT
also instructsPostgreSQL
to generate a value for the identity column. However, if you supply a value for insert or update,PostgreSQL
will use that value to insert into the identity column instead of using the system-generated value.For more information.
使用自定义序列在 postgresql 中创建自动递增主键:
第 1 步,创建序列:
第 2 步,创建表
第 3 步,插入表
第 4 步,观察行
这两行的键从 1 开始,按序列定义以 1 递增。
奖励精英专业提示:
程序员讨厌打字,而且输入
nextval('splog_adfarm_seq')
很烦人。您可以为该参数键入DEFAULT
,如下所示:为了使上述工作正常进行,您必须在 splog_adfarm 表上为该键列定义默认值。哪个更漂亮。
Create an auto incrementing primary key in postgresql, using a custom sequence:
Step 1, create your sequence:
Step 2, create your table
Step 3, insert into your table
Step 4, observe the rows
The two rows have keys that start at 1 and are incremented by 1, as defined by the sequence.
Bonus Elite ProTip:
Programmers hate typing, and typing out the
nextval('splog_adfarm_seq')
is annoying. You can typeDEFAULT
for that parameter instead, like this:For the above to work, you have to define a default value for that key column on splog_adfarm table. Which is prettier.
如果你想在 pgadmin 中执行此操作,那就容易得多。在postgressql中,要为列添加自动增量,我们首先需要创建一个自动增量序列并将其添加到所需的列中。我确实喜欢这个。
1)首先您需要确保您的表有主键。还要保留主键的数据类型为bigint 或smallint。 (我使用bigint,找不到称为serial的数据类型,如其他答案中提到的)
2)然后通过右键单击序列->添加序列添加新序列。
如果表中没有数据,则保持顺序不变,不要进行任何更改。只需保存它即可。
如果存在现有数据,请将主键列中的最后一个或最高值添加到定义选项卡中的当前值,如下所示。
3) 最后,将
nextval('your_sequence_name'::regclass)
行添加到主键中的默认值,如下所示。确保此处的序列名称正确。这就是全部,自动增量应该可以工作。
If you want to do this in pgadmin, it is much easier. It seems in postgressql, to add a auto increment to a column, we first need to create a auto increment sequence and add it to the required column. I did like this.
1) Firstly you need to make sure there is a primary key for your table. Also keep the data type of the primary key in bigint or smallint. (I used bigint, could not find a datatype called serial as mentioned in other answers elsewhere)
2)Then add a sequence by right clicking on sequence-> add new sequence.
If there is no data in the table, leave the sequence as it is, don't make any changes. Just save it.
If there is existing data, add the last or highest value in the primary key column to the Current value in Definitions tab as shown below.
3)Finally, add the line
nextval('your_sequence_name'::regclass)
to the Default value in your primary key as shown below.Make sure the sequence name is correct here. This is all and auto increment should work.
如果要在序列中使用数字,请使用类似的内容定义一个新序列
,然后更改表以使用该序列作为 id:
If you want to use numbers in a sequence, define a new sequence with something like
and then alter the table to use the sequence for the id:
我尝试了以下脚本来成功自动增加 PostgreSQL 中的主键。
编辑:
SERIAL 关键字会自动为相应列创建序列。
I have tried the following script to successfully auto-increment the primary key in PostgreSQL.
EDIT:
SERIAL keyword automatically create a sequence for respective column.
在 PgAdmin 上执行此操作的步骤:
字段默认值。
Steps to do it on PgAdmin:
the field default.
也许我回答这个问题有点晚了,但我正在工作中研究这个主题:)
我想写专栏 'a_code' = c1,c2,c3,c4...
首先我打开了一个专栏名称为
ref_id
,类型为serial
。然后我用这个命令解决了我的问题:
Maybe I'm a bit of late to answer this question, but I'm working on this subject at my job :)
I wanted to write column 'a_code' = c1,c2,c3,c4...
Firstly I opened a column with the name
ref_id
and the typeserial
.Then I solved my problem with this command: