Any example of a necessary nullable foreign key?

后端 未结 8 1789
感情败类
感情败类 2020-12-03 01:42
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 allow

8条回答
  •  清歌不尽
    2020-12-03 02:14

    A common design scenario for setting a column to null is if you have a one to many parent child relationship, but the children do not have to be present.

    When a parent record (like an account) has children that can be deleted (like help requests) this is a good usage.

    You may have a column for MostRecentRequest that includes the ID of the most recent help request. When the request is removed from the system the MostRecentRequest column is set to NULL meaning there are none present.

    Another example is when you want NULL to mean that a row has not been assigned a parent yet. Perhaps you have help requests that come in and a NULL on the technician field means that no tech is assigned to the request. When you delete that technician you want all his uncompleted work to go back to the pool by resetting the value to NULL.

提交回复
热议问题