邻接表树

编程入门 行业动态 更新时间:2024-10-26 13:25:23
本文介绍了邻接表树 - 如何防止循环引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在数据库中有一个邻接表,用 ID 和 ParentID 来表示树结构:

I have an adjacency list in a database with ID and ParentID to represent a tree structure:

-a --b ---c -d --e

当然,在记录中,ParentID 永远不应该与 ID 相同,但我还必须防止循环引用以防止无限循环.这些循环引用理论上可能涉及 2 个以上的记录.( a->b, b->c, c->a 等)

Of course in a record the ParentID should never be the same as ID, but I also have to prevent circular references to prevent an endless loop. These circular references could in theory involve more than 2 records. ( a->b, b->c, c->a , etc.)

对于每条记录,我将路径存储在这样的字符串列中:

For each record I store the paths in a string column like this :

a a b a/b c a/b/c d d e d/e

我现在的问题是:插入/更新时,有没有办法检查是否会发生循环引用?

My question is now : when inserting/updating, is there a way to check if a circular reference would occur?

我应该补充一点,我对嵌套集模型等都了如指掌.我选择了带有存储路径的邻接方法,因为我发现它更直观.我让它与触发器和一个单独的路径表一起工作,它就像一个魅力,除了可能的循环引用.

推荐答案

如果您像这样存储路径,您可以检查该路径是否不包含 id.

If you're storing the path like that, you could put in a check that the path does not contain the id.

更多推荐

邻接表树

本文发布于:2023-10-19 15:02:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1507877.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!