建立零或一对多的关系 [英] Model a Zero or One to Many Relationship

查看:154
本文介绍了建立零或一对多的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在数据库中建立零或一对多的关系?例如,用户记录可能有也可能没有父级。我的用户表应该有一个t_user.parent_id,还是应该有一个名为t_user_hierarchy的关联表,其中列有t_user_hierarchy.parent_id和t_user_hierarchy.user_id?

How should I model a zero or one to a many relationship in the database? For example, a user record may or may not have a parent. So should my user table have a t_user.parent_id or should I have an associative table called t_user_hierarchy with the columns t_user_hierarchy.parent_id and t_user_hierarchy.user_id?

推荐答案

看这个线程。类似的问题:必需的可空的外键的任何示例?

Look at this thread. Similar problem : Any example of a necessary nullable foreign key?

是否有另一个关联表或可空的外键取决于您的用例。它的主观和取决于你的设计。

Whether to have another association table or nullable foreign key depends on your use case. Its subjective and depends on your design.

这篇关于建立零或一对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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