引用PK的外键需要NOT NULL约束吗? [英] Does a Foreign Key referencing PK need the NOT NULL constraint?

查看:453
本文介绍了引用PK的外键需要NOT NULL约束吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

引用主键的外键是否需要PostgreSQL数据库中的NOT NULL约束?

数据库高度标准化,并且将非常大。我不希望添加额外的约束,如果不需要这些查询,那么会减慢查询的速度。

Does a Foreign Key referencing a Primary Key need the NOT NULL constraint in a PostgreSQL database?
The database is highly normalized and will be very large. I do not wish to add extra constraints that will slow down the queries even more if said queries are unneeded.

推荐答案

能够在该表的FK列中表示未知,然后使其为空,如果它必须有一个值,则使其不为空。

If you want to be able to represent unknown in the FK column of that table, then make it nullable, if it has to have a value, make it Not Null.

您可以在引用表中具有空FK值所需的记录数量。唯一的约束是引用表(PK在哪里)不在引用表(FK在哪里)的行上的行。

You can have as many records as you want in the referencing table with null FK values. The unique constraint is on the rows in the referenced table (Where the PK is) not on the rows in the referencing table (where the FK is).

这篇关于引用PK的外键需要NOT NULL约束吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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