任何必需的可空外键的示例? [英] Any example of a necessary nullable foreign key?

查看:206
本文介绍了任何必需的可空外键的示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Customers
 customer_id

Orders
 order_id
 customer_id fk

如果我有两个表,并在Orders表中的customer_id上定义一个外键,通过允许它为null我说我可以有一个顺序,没有与之相关的客户。因此,可空外键的概念似乎与外键的目的不一致,这是为了强制实施这个约束。

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.

这篇关于任何必需的可空外键的示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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