问题描述
Customers
customer_id
Orders
order_id
customer_id fk
如果我有两个表并在 Orders 表中的 customer_id 上定义一个外键,允许它为空,我是说我可以有一个没有客户关联的订单.因此,可空外键的概念似乎与外键的目的不一致,即强制执行此约束.
If I have two tables and define a foreign key on customer_id in the Orders table, by allowing it to be null I am saying that I can have an order that does not have a customer associated with it. As such, the notion of a nullable foreign key seems at odds with the purpose of a foreign key, which is to enforce this constraint.
是否有一个简单的例子说明需要可空外键的情况?还是赞成允许他们的论据?
Is there a simple example of a situation in which a nullable foreign key would be necessary? Or an argument in favor of permitting them?
推荐答案
想象一个包含团队 TODO 的表格.如果 TODO 尚未分配给团队成员,则其 user_id
为 NULL
.如果它不是 NULL
它是 users
表的外键.
Imagine a table that holds the TODOs of a team. If a TODO is not yet assigned to a member of the team, its user_id
is NULL
. If it is not NULL
it is a foreign key to the users
table.
这篇关于任何必要的可空外键示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!