mysql是怎么实现多对多的,如何在MySQL中实现多对多层次结构

In short, I want to implement in MySQL a table of recipes and another of instructions. A recipe is a sequential series of instructions or other recipes. So for example you could imagine a Peach_preserve recipe, and a Peach_tart that uses the Peach_preserve, plus a series of other steps (instructions). Peach_preserve could be used for many other recipes.

In my previous question I was recommended a design to allow for a specific order for each instruction within a recipe:

recipe

id name

1 Recipe1

2 Recipe2

recipe_instruction

recipe_id instruction_id sortorder

1 5 1

1 3 2

1 4 3

2 6 1

2 7 2

2 3 3

2 6 4

What I would like to do now is to incorporate the idea that within a recipe a sub-component might be another recipe rather than an atomic / discrete instruction. So my idea is to do it this way:

recipe

id name

1 Recipe1

2 Recipe2

recipe_instruction

recipe_id step_id isRecipe sortorder

1 5 F 1

1 3 F 2

1 4 F 3

2 1 F 1

2 1 T 2

2 2 F 3

2 1 F 4

Here you can see that Recipe1 is made of 3 instructions, and Recipe2 is made of one instruction, then Recipe1, then another 2 instructions, one of which is a repeat of step 1. I thought of other ways to capture this idea but they involve a bunch of null entries. What I don't like in the above is that the key is made of the 4 attributes...

My questions are:

Is it a good idea to incorporate a recursive idea inside a db?

If so, is this the way to do it, or can it be improved upon?

QUICK EDIT: I'm starting to read on hierarchies. In my example, each one recipe can have several parents.

解决方案

This is a very common technique. It is used to store hierarchical data (you call it recursive) within a db.

However you will have to manage the integrity in your application, because a foreign key cannot be used, since the relation is conditional to the isRecipe flag.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 实现 MySQL 多表树形结构递归可以采用以下步骤: 1. 创建一个包含父级ID的表,用于存储树形结构数据。 2. 在表插入数据,并且每个数据项都有一个父级ID。 3. 使用递归查询获取树形结构数据。 以下是一个示例 SQL 代码,用于实现 MySQL 多表树形结构递归: ``` WITH RECURSIVE cte AS ( SELECT id, name, parent_id, 0 AS depth FROM category WHERE parent_id IS NULL UNION ALL SELECT c.id, c.name, c.parent_id, depth + 1 FROM category c JOIN cte ON c.parent_id = cte.id ) SELECT id, name, parent_id, depth FROM cte; ``` 在上面的 SQL 代码,我们使用了递归查询 (WITH RECURSIVE) 和联结 (JOIN) 的方式获取树形结构数据。其,第一条 SELECT 语句用于获取根节点,即 parent_id 为 NULL 的数据项。随后,我们使用 UNION ALL 和 JOIN 语句来获取树形结构的各个节点。 最终,我们可以获取到一个包含 id、name、parent_id 和 depth 的树形结构数据。其,id 表示节点的唯一标识符,name 表示节点的名称,parent_id 表示节点的父级ID,depth 表示节点的深度。 ### 回答2: MySQL多表树形结构递归实现是指使用多个表来构建一个树形结构,并通过递归方式来查询和操作这个树形结构。 在MySQL,我们可以通过以下步骤来实现多表树形结构的递归查询: 1. 创建表格:首先,我们需要创建一个或多个表格来存储树形结构的数据。一般来说,这些表格包含一个主键字段和一个外键字段,外键字段用于建立表格之间的关系。 2. 插入数据:接下来,我们需要向表格插入数据,以构建树形结构。通常,每个数据行都包含一个唯一的主键值和一个外键值,外键值指向其父节点的主键值。 3. 查询数据:使用递归查询的方法,我们可以从根节点开始,逐级向下遍历整个树形结构,找到所需的数据。在MySQL,可以使用WITH RECURSIVE语句来实现递归查询。 4. 更新数据:如果需要修改树形结构的数据,我们可以通过更新表格的外键值来实现。例如,如果需要将一个节点从一个父节点移动到另一个父节点,可以通过更新该节点的外键值来实现。 总结而言,MySQL多表树形结构递归实现是一种使用多个表格和递归查询方法来构建和操作树形结构的方法。这种方法可以方便地查询和操作树形结构的数据,在很多场景下都具有较高的实用性。 ### 回答3: MySQL是一种广泛使用的关系型数据库管理系统,可以用于存储和管理大量的数据。在MySQL,可以使用多个表结构来实现树形结构的递归。 在实现树形结构的递归时,首先需要创建一个包含树节点信息的表。该表至少应包含一个节点ID、节点名称、父节点ID等列。可以使用主键和外键约束来确保节点间的关系正确性。 接下来,可以通过使用递归查询来获取树形结构的数据。在MySQL,可以使用WITH RECURSIVE子句来实现递归查询。该子句可以将表自身连接多次,从而实现递归查询。 具体步骤如下: 1. 创建一个包含树节点信息的表,例如节点表; 2. 在节点表,定义一个主键列node_id和一个外键列parent_id,用于建立节点间的层次关系; 3. 使用WITH RECURSIVE子句编写递归查询语句,例如: WITH RECURSIVE cte (node_id, node_name, parent_id, level) AS ( SELECT node_id, node_name, parent_id, 0 FROM node_table WHERE node_id = 1 -- 将根节点的ID作为起点 UNION ALL SELECT n.node_id, n.node_name, n.parent_id, cte.level + 1 FROM node_table n INNER JOIN cte ON n.parent_id = cte.node_id ) SELECT * FROM cte; 4. 执行上述递归查询语句,即可获得树形结构的数据。 通过以上步骤,可以在MySQL实现多表树形结构的递归查询。递归查询可以帮助我们快速获取树形结构的所有节点和层次关系,非常方便实用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值