如何使用 PostgreSQL 在 DBeaver 上创建自动递增/SERIAL id 列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52177610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:43:52  来源:igfitidea点击:

How to create auto incrementing / SERIAL id column's on DBeaver using PostgreSQL?

postgresqldbeaver

提问by jwwnz

I am a new user for both PostgreSQL and DBeaver (Community edition ver. 5.1.6) and was looking for a way to create an auto incrementing ID column in a table through the DBeaver GUI.

我是 PostgreSQL 和 DBeaver(社区版 5.1.6 版)的新用户,我正在寻找一种通过 DBeaver GUI在表中创建自动递增 ID 列的方法

From my research I can see that:

从我的研究中我可以看出:

  1. You can set this up easily using SQL eg. id SERIAL NOT_NULL

  2. The underlying problem is that there is no such thing as a 'Serial data type', and that SERIAL equates to nextval('table_name_id_seq').

  3. When I create a table using the SERIAL command in SQL the resulting id column has a nextval('exampletable_id_seq'::regclass') value in the 'Default' attribute.

  1. 您可以使用 SQL 轻松地进行设置,例如。ID 序列号 NOT_NULL

  2. 潜在的问题是没有“串行数据类型”这样的东西,SERIAL 等同于 nextval('table_name_id_seq')。

  3. 当我在 SQL 中使用 SERIAL 命令创建表时,生成的 id 列在“默认”属性中具有 nextval('exampletable_id_seq'::regclass') 值。

I have attempted to manually input the nextval() command within the 'Default' attribute for the column in DBeaver in a new table, for example. nextval('mytable_id_seq') with and without the '::regclass;. However this is not working.

例如,我尝试在新表中 DBeaver 列的“默认”属性中手动输入 nextval() 命令。nextval('mytable_id_seq') 带和不带 '::regclass;。然而,这是行不通的。

I appreciate that doing this in SQL would be easier, and that there is a previously asked question at: Problems de Serial data type in DBeaver & PostgreSQL.

我很欣赏在 SQL 中执行此操作会更容易,并且之前有一个问题:DBeaver & PostgreSQL 中的串行数据类型问题

However, I could not find a satisfactory answer and the option of being able to do this through the GUI would be useful, especially if other setup is being done through the DBeaver GUI.

但是,我找不到令人满意的答案,并且能够通过 GUI 执行此操作的选项会很有用,尤其是在通过 DBeaver GUI 完成其他设置的情况下。

Specifically, my question is:

具体来说,我的问题是:

  1. Is there a functionality for DBeaver to add auto incrementing id's through the GUI?

  2. If so, what would be the steps to do this.

  1. DBeaver 是否有通过 GUI 添加自动递增 ID 的功能?

  2. 如果是这样,执行此操作的步骤是什么。

Thanks very much everyone!

非常感谢大家!

回答by jwwnz

After reviewing the very helpful comments to my question above, I have realised that the question was asked due to my lack of experience with DBeaver.

在查看了上面对我的问题非常有帮助的评论后,我意识到这个问题是由于我缺乏 DBeaver 的经验而提出的。

For completeness I have set out the answer to my question below:

为了完整起见,我在下面列出了我的问题的答案:

  1. Yes there is a way to auto increment directly through the DBeaver GUI.

  2. This can be done by setting up a id variable when a table is created by setting a column with a type of serialand "not null" ticked, then setting the id as primary key through constraints.

  1. 是的,有一种方法可以直接通过 DBeaver GUI 自动递增。

  2. 这可以通过在创建表时设置一个 id 变量来完成,方法是设置一个类型为序列的列并勾选“非空”,然后通过约束将 id 设置为主键。

Screenshot attached below:

下面附上截图:

enter image description here

在此处输入图片说明

Due to the way PostgreSQL works, the 'Serial' type is a pseudotype which allows a sequence of numbers to be used (http://www.postgresqltutorial.com/postgresql-serial/).

由于 PostgreSQL 的工作方式,“Serial”类型是一种伪类型,它允许使用一系列数字(http://www.postgresqltutorial.com/postgresql-serial/)。

So, in DBeaver, once you set up the tables this will be displayed as the following rather than with a 'serial' type:

因此,在 DBeaver 中,一旦您设置了表,这将显示如下而不是“串行”类型:

enter image description here

在此处输入图片说明

I appreciate that this is a very obvious answer to anyone who is familiar with PostgreSQL and/ DBeaver, thank you for your understanding :)

我很欣赏这对熟悉 PostgreSQL 和/DBeaver 的人来说是一个非常明显的答案,感谢您的理解:)