如何在 PostgreSQL 中设置自增主键? [英] How to set auto increment primary key in PostgreSQL?

查看:115
本文介绍了如何在 PostgreSQL 中设置自增主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 PostgreSQL 中有一个包含很多列的表,我想添加一个自增主键.

我尝试创建一个名为 id 且类型为 BIGSERIAL 的列,但 pgadmin 响应错误:

<块引用>

错误:序列必须与其链接的表具有相同的所有者.

有人知道如何解决这个问题吗?如何在不重新创建表的情况下在 PostgreSQL 中添加或创建自动递增的主键?

解决方案

试试这个命令:

ALTER TABLE your_table ADD COLUMN key_column BIGSERIAL PRIMARY KEY;

使用与您创建表的数据库用户相同的数据库用户进行尝试.

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?

解决方案

Try this command:

ALTER TABLE your_table ADD COLUMN key_column BIGSERIAL PRIMARY KEY;

Try it with the same DB-user as the one you have created the table.

这篇关于如何在 PostgreSQL 中设置自增主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆