Any example of a necessary nullable foreign key?

2020-01-29 05:36发布

Customers
 customer_id

Orders
 order_id
 customer_id fk

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?

8条回答
淡お忘
2楼-- · 2020-01-29 06:02

To set a foreign key nullable or null use below sql script

ALTER TABLE Return_COMMENTS MODIFY order_ID Number NULL;
查看更多
唯我独甜
3楼-- · 2020-01-29 06:04

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.

The purpose of a foreign key is the make explicit the concept the random integer in the Orders table actually refers to an item in the Customers table. Actually enforcing that as a constraint is incidental.

查看更多
登录 后发表回答