Any example of a necessary nullable foreign key?

后端 未结 8 1797
感情败类
感情败类 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:01

    There is another situation that I can think of:

    Within one table, you have an ID field that uniquely identifies that record. That same record may (or may not) have a single parent.

    Table with the following columns:

    • id as an integer, auto-increment, not nullable
    • parentid as an integer, nullable.

    With a foreign key constraint, you can ensure that if the field is not null, then it refers to a valid parent. Furthermore, if you attempt to delete the parent record without fixing the children, then the parent cannot be deleted.

提交回复
热议问题