使用特殊中心节点建模有向图
我正在寻找有关如何对包含一个特殊节点的有向图进行建模的意见。
特殊节点:
- 不能有任何边通向它。
- 无法删除。
当前设计:
表:节点、边。 边包含两列; from_node_id 和 to_node_id,每个都引用 Nodes 表中的一条记录。
我没有将特殊节点存储为 Nodes 表中的第一条记录,而是决定根本不保留它的记录,而是将其与任何数据库查询分开构建。 在 Edges 表中,from_node_id 列中的 NULL 具有特殊含义,指的是中心节点。
我使用此设计的动机是,我不必担心保护中心节点记录不被删除/修改或在 Edges 表的 to_node_id 列中被引用。 这也会自动阻止边缘往返于同一节点。 我意识到这种设计有一些缺点,例如无法使 from_node_id 和 to_node_id 成为复合主键,可能还有更多。
我目前倾向于使中心节点成为实际记录,并在相关数据库方法中创建对该节点的检查。 进行此设计的最佳方法是什么?
I'm looking for opinions on how to model a directed graph that contains one special node.
Special node:
- Cannot have any edges leading to it.
- Cannot be removed.
Current design:
Tables: Nodes, Edges. Edges contains two columns; from_node_id and to_node_id, each referencing a record in the Nodes table.
Rather than storing the special node as the first record in the Nodes table, I decided not to keep a record for it at all, constructing it separately from any database queries. In the Edges table, NULL takes on a special meaning in from_node_id column, referring to the center node.
My motivation for using this design was that I wouldn't have to worry about protecting a center node record from deletion/modification or being referenced in the to_node_id column of the Edges table. This would also automatically prevent an edge from going from and to the same node. I realize there are some drawbacks to this design, such as not being able to make from_node_id and to_node_id a composite primary key, and probably many more.
I'm currently leaning towards making the center node an actual record and creating checks for that node in the relevant database methods. What's the best way to go about this design?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看到一些反对在这种情况下使用 NULL 的论点。
换句话说,我更愿意在数据库中为中心节点保留行。
I see some arguments against using NULL in this case.
In other words I would prefer to have row in the database for the central node.