postgresql 在 PgAdmin 中添加外键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34584356/
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-09-09 06:08:08  来源:igfitidea点击:

Add a Foreign Key in PgAdmin

postgresqlforeign-keysforeign-key-relationshippgadmin

提问by rotarydial

I have studidtemplatestable below:

我有下studidtemplates表:

enter image description here

在此处输入图片说明

template_idis the primary_key

template_id是primary_key

enter image description here

在此处输入图片说明

I want to create a new table referencing template_idas a foreign key. It is named studidtemplatetextelements. See image below:

我想创建一个引用template_idforeign key. 它被命名为studidtemplatetextelements。见下图:

enter image description here

在此处输入图片说明

I created a column template_idin the second table and want to make it a foreign key referencing template_idin studidtemplatestable. I did it by clicking the button in the Constraintstab,pointed by an arrow in the image below.

我创建了一个列template_id在第二个表,并希望把它的外键引用template_idstudidtemplates表。我是通过单击Constraints选项卡中的按钮来完成的,该按钮由下图中的箭头指向。

enter image description here

在此处输入图片说明

I notice something different. In 'Referencing' option there's no template_idoption available. See image below:

我注意到一些不同的东西。在“参考”选项中没有template_id可用选项。见下图:

enter image description here

在此处输入图片说明

Where am I missing?

我在哪里失踪?

回答by rotarydial

In pgAdmin 4, here are the steps:

在 pgAdmin 4 中,步骤如下:

  1. Right-click on the table and select Properties.
  2. Within the dialog that appears, click Constraints/ Foreign Key.
  3. Click the +icon on the upper-right of the Foreign key table.
  4. Click the pencil icon, which is all the way on the left of the new row that now appears in the Foreign key table.
  5. Under the Generaltab, give your foreign key a name (I follow this conventionof {tablename}_{columnname}_fkey).
  6. Under the Columnstab, select your references.
  7. Click the +icon to the right of the "Local column".
  8. Click the Savebutton at the bottom.
  1. 右键单击该表并选择Properties
  2. 在出现的对话框中,单击Constraints/ Foreign Key
  3. 单击外键表右上角的+图标。
  4. 单击现在出现在外键表中的新行左侧的铅笔图标
  5. 根据常规选项卡,给你的外键的名称(按照本公约{}表名_ {} COLUMNNAME _fkey)。
  6. 在“列”选项卡下,选择您的参考。
  7. 单击“本地列”右侧的+图标。
  8. 点击底部的保存按钮。

Here's a screenshot:

这是一个屏幕截图:

pgAdmin 4 foreign key dialog

pgAdmin 4 外键对话框

回答by Elad

The columns listed under Referencingare columns that belong to the selected table.

下面列出Referencing的列是属于所选表的列。

You need to change the selected table in Referencesas follows: enter image description here

您需要References按如下方式更改所选表: 在此处输入图片说明

Which version of pgAdmin are you using? The References textbox was moved to Columns tab in version 1.16:

您使用的是哪个版本的 pgAdmin?在1.16版中,参考文本框被移到了列选项卡:

On the foreign key properties dialog, the "References" table textbox has been moved to the third tab

在外键属性对话框中,“引用”表文本框已移至第三个选项卡