关系数据库问题:Parent 引用 child

Relational Database Question: Parent that references child

让 parent table 引用 child table 是否有效?

比如一个tableParents有一个childtableChildren。 Children 有一个外键 Parent_ID 链接回其 parent。但是 Parent 有另一列(比如 favorite_child_id),它是返回 Child table 的外键。这是 acceptable 还是两个 table 本质上相互引用是糟糕的数据库设计?

这并不罕见,也是一个合理的解决方案。例如,常见的情况是公司的联系人。其中一个联系人可能被指定为 "primary contact".

唯一的问题是您不能同时声明 favorite_child_id 为 nNOT NULLchild.parent_idNOT NULL。如果这样做,您将处于无法将值添加到 table.

的情况

另一种方法是在 child table 中有一个 "is favorite" 标志。确保 恰好 一个 child 设置了该标志可能很棘手。