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
Add a Foreign Key in PgAdmin
提问by rotarydial
I have studidtemplates
table below:
我有下studidtemplates
表:
template_id
is the primary_key
template_id
是primary_key
I want to create a new table referencing template_id
as a foreign key
. It is named studidtemplatetextelements
. See image below:
我想创建一个引用template_id
为foreign key
. 它被命名为studidtemplatetextelements
。见下图:
I created a column template_id
in the second table and want to make it a foreign key referencing template_id
in studidtemplates
table. I did it by clicking the button in the Constraints
tab,pointed by an arrow in the image below.
我创建了一个列template_id
在第二个表,并希望把它的外键引用template_id
的studidtemplates
表。我是通过单击Constraints
选项卡中的按钮来完成的,该按钮由下图中的箭头指向。
I notice something different. In 'Referencing' option there's no template_id
option available. See image below:
我注意到一些不同的东西。在“参考”选项中没有template_id
可用选项。见下图:
Where am I missing?
我在哪里失踪?
回答by rotarydial
In pgAdmin 4, here are the steps:
在 pgAdmin 4 中,步骤如下:
- Right-click on the table and select Properties.
- Within the dialog that appears, click Constraints/ Foreign Key.
- Click the +icon on the upper-right of the Foreign key table.
- Click the pencil icon, which is all the way on the left of the new row that now appears in the Foreign key table.
- Under the Generaltab, give your foreign key a name (I follow this conventionof {tablename}_{columnname}_fkey).
- Under the Columnstab, select your references.
- Click the +icon to the right of the "Local column".
- Click the Savebutton at the bottom.
- 右键单击该表并选择Properties。
- 在出现的对话框中,单击Constraints/ Foreign Key。
- 单击外键表右上角的+图标。
- 单击现在出现在外键表中的新行左侧的铅笔图标。
- 根据常规选项卡,给你的外键的名称(按照本公约的{}表名_ {} COLUMNNAME _fkey)。
- 在“列”选项卡下,选择您的参考。
- 单击“本地列”右侧的+图标。
- 点击底部的保存按钮。
Here's a screenshot:
这是一个屏幕截图:
回答by Elad
The columns listed under Referencing
are columns that belong to the selected table.
下面列出Referencing
的列是属于所选表的列。
You need to change the selected table in References
as follows:
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
在外键属性对话框中,“引用”表文本框已移至第三个选项卡