【发布时间】:2012-03-27 22:04:43
【问题描述】:
这是我的问题的后续:
How to implement a many-to-many hierarchical structure in MySQL
在这里:
How to record sequential collections of records in MySQL.
简而言之,我想在 MySQL 中实现一个食谱表和另一个指令表。配方是一系列连续的指令或其他配方。例如,您可以想象一个Peach_preserve 配方和一个使用Peach_preserve 的Peach_tart,以及一系列其他步骤(说明)。 Peach_preserve 可用于许多其他食谱。
我阅读了this blog post by Bill Karwin about closure tables,我认为这个解决方案最能解决我的挑战(我的层次结构是多对多的,并且步骤是连续的)。所以例如我会:
recipe
id name
1 Peach preserve
2 Cubed peeled peaches
3 Fresh peaches
4 Powdered sugar
5 Cook together
6 Peel and cut in chunks
7 Mix
step (or instruction)
id desc
1 Cook together
2 Buy peaches
3 Buy sugar
4 Peel and cut in chunks
5 Mix
recipe_instruction
(Ancestor) (Descendant)
recipe_id step_id depth descendant_is_instruction
3 3 0 0
3 2 1 1
4 4 0 0
4 3 1 1
6 6 0 0
6 4 1 1
2 2 0 0
2 3 1 0
2 2 2 1
2 6 1 0
2 4 2 1
(and so on...)
我不是descendant_is_instruction 标志的粉丝,但我不知道该怎么做。我想我可以用descendant_is_leaf 替换它来识别终端项目...
排序顺序由一个包含深度为 1 的所有关系的表表示:
Depth=1 table
recipe_id step_id order
3 2 1
4 3 1
6 4 1
2 3 1
2 6 2
我在这里进行了简化,因为在实践中我会将成分和说明分开,但你明白了。
那么,这是一种结合分层数据结构和步进顺序概念的好方法吗?我应该做些什么来改进/简化?
【问题讨论】: