初级&pgAdmin 中的外键 [英] Primary & Foreign Keys in pgAdmin

查看:22
本文介绍了初级&pgAdmin 中的外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道有人能给我解释一下如何在 pgAdmin 中分配主键和外键吗?

I was wondering can some give me an explanation on how to assign primary and foreign keys in pgAdmin?

我在网上找不到任何信息.

I can't find any information online.

例如...我有一个学生表,其中包含他们的所有详细信息(地址、d.o.b. 等).我将在表中添加一个 student_number 并将其设为主键.

For example...I've got a Student table with all their details (address, d.o.b. and etc.). I'm going to add a student_number to the table and make it a primary key.

我只想知道如何使用 pgAdmin 来做到这一点?如果您愿意解释,请给我有关在 postgreSQL(和 pgAdmin)中使用主键的更多信息.外键也是同样的情况.

I just want to know how do I do that using pgAdmin? And if you may be kind to explain give me further information on using Primary Keys in postgreSQL (and pgAdmin). The same case with the foreign keys.

推荐答案

pgAdmin 中没有选项可以将列添加到现有表并同时使其成为主键,因为这几乎是不可能的.

There is no option in pgAdmin to add a column to an existing table and make it the primary key at the same time, because this is hardly possible.

主键列需要保存唯一的非空值.将列添加到现有表后,它会保存 NULL 值.因此,您必须先输入唯一值,然后才能添加 UNIQUEPRIMARY KEY 约束.

A primary key column needs to hold unique non-null values. Upon adding a column to an existing table, it holds NULL values. So you have to enter unique values before you can add a UNIQUE or PRIMARY KEY constraint.

该规则有一个例外:如果您添加 serial 列,自动插入唯一值.在这种情况下,您也可以立即定义 PRIMARY KEY:

There is an exception to that rule, though: If you add a serial column, unique values are inserted automatically. In this case, you can also define it PRIMARY KEY right away:

ALTER TABLE student ADD COLUMN student_number serial PRIMARY KEY;

这适用于 PostgreSQL 9.1.我不确定它是否在旧版本中也是如此.

This works in PostgreSQL 9.1. I am not sure it does in older versions, too.

pgAdmin 目前没有在新列..."对话框中为 serial 列合并这种特殊情况(版本 1.14).

pgAdmin does not incorporate this special case for serial columns in the "New column..." dialog at this time (version 1.14).

这篇关于初级&pgAdmin 中的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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