这是一个后续行动,我的问题在这里:
How to implement a many-to-many hierarchical structure in MySQL
这里:
How to record sequential collections of records in MySQL。如何将排序顺序添加到表示多对多关系的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
我简化这里因为在实践中我会分开成分和指示,但你明白了。
所以,是一个很好的方式既分层数据结构和工序顺序的概念结合起来?我应该做什么来改进/简化?
从这个意义上说,'description'/'name'字段是相互冗余的。如何让一张桌子拥有食谱树,另一张桌子只是名字? – Yuval 2012-04-06 12:05:49
@Yuval,我没有看到配方名称和使用配方冗余的说明描述。前者描述了由此产生的成分是什么(例如,“融化的巧克力”),后来的“如何”使用了结果(例如,“用融化的巧克力在平板上绘画”)。 Yumm。 – 2012-04-06 12:46:36