first at all, I have NO IDEA how to title my question. If anyone has a better title I'll be greatfull.
首先,我没有IDEA如何标题我的问题。如果有人有更好的头衔,我会很高兴。
My real question now, is about what is the best approach to make records referencing other records in the same table. I'm thinking about something like
我现在的真正问题是,在同一个表中创建引用其他记录的记录的最佳方法是什么。我在想类似的东西
- Person
- 人
- A person must have a father (logical thinking, let's forget about orphan people, please)
- 一个人必须有一个父亲(逻辑思维,让我们忘记孤儿)
- A person can be parent to many childs
- 一个人可以成为许多孩子的父母
The table could be somehting like PersonId, Name, PersonParentId
该表可能类似于PersonId,Name,PersonParentId
Is it right? There is a better way to do it? I heard about Hierarchical Data but I'm not sure.
这样对吗?还有更好的方法吗?我听说过Hierarchical Data,但我不确定。
Thanks
谢谢
1 个解决方案
#1
3
This is almost always the best way to do it. You will see discussions of better ways but in general, this is almost always the right way to do it.
这几乎总是最好的方法。您将看到更好的方法的讨论,但总的来说,这几乎总是正确的方法。
A few pointers.
几点指示。
- Don't forget the foreign key
- 别忘了外键
- Make sure the referential logic is solid
- 确保参照逻辑是可靠的
- Look into
WITH RECURSIVE
for tree-based queries - 查看WITH RECURSIVE以查找基于树的查询
- In your Recursive CTE's code defensively and check for cycles.
- 在您的Recursive CTE代码中进行防御并检查周期。
#1
3
This is almost always the best way to do it. You will see discussions of better ways but in general, this is almost always the right way to do it.
这几乎总是最好的方法。您将看到更好的方法的讨论,但总的来说,这几乎总是正确的方法。
A few pointers.
几点指示。
- Don't forget the foreign key
- 别忘了外键
- Make sure the referential logic is solid
- 确保参照逻辑是可靠的
- Look into
WITH RECURSIVE
for tree-based queries - 查看WITH RECURSIVE以查找基于树的查询
- In your Recursive CTE's code defensively and check for cycles.
- 在您的Recursive CTE代码中进行防御并检查周期。