如何在PostgreSQL中设置自动递增主键?

编程入门 行业动态 更新时间:2024-10-25 23:35:29
本文介绍了如何在PostgreSQL中设置自动递增主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在PostgreSQL中有一个具有22列的表,我想添加一个自动增量主键.

I have a table in PostgreSQL with 22 columns, and I want to add an auto increment primary key.

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

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.

有人知道如何解决此问题吗?如何在PostgreSQL中添加创建自动递增主键而又不重新创建表的方法?

Does anyone know how to fix this issue? How do I add a create an auto-incrementing primary key in PostgreSQL without recreating the table again?

推荐答案

尝试以下命令:

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中设置自动递增主键?

本文发布于:2023-10-16 15:59:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1498048.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:设置自动   主键   如何在   PostgreSQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!