简体   繁体   English

数据库设计:分层数据

[英]Database Design: Hierarchical Data

I am having trouble arriving at a normalized relational database design to describe a small hierarchy which deviates enough from the typical hierarchy examples such that I am unsure how to proceed my first time tackling such a problem. 我在进行规范化的关系数据库设计时很难描述一个很小的层次结构,该层次结构与典型的层次结构示例有很大的偏差,因此我不确定如何继续进行第一次处理此类问题。

My problem is as follows: 我的问题如下:

Each branch in the hierarchy is guaranteed to be either 2, 4, or 6 levels deep. 保证层次结构中的每个分支深度为2、4或6级。 If it is 2 levels deep, the hierarchy looks like this: 如果深度为2层,则层次结构如下所示:

Category / Group / Component

If it is 4 levels deep, it looks like this: 如果深度为4级,则如下所示:

Category / Group / Component / Group / Component

If it is 6 levels deep, it looks like this: 如果深度为6级,则如下所示:

Category / Group / Component / Group / Component / Group / Component

Categories, Groups, and Components each have their own set of attributes. 类别,组和组件各自具有自己的属性集。 To further complicate matters, a relationship exists between a Component and entity A, a Component and entity B, and a Component and entity C. 使问题更加复杂的是,组件和实体A,组件和实体B以及组件和实体C之间存在关系。

My original thought was to strive to keep the Components in one table, however, I have been unable to come up with a normalized solution that satisfies this goal. 我最初的想法是努力将组件保持在一个表中,但是,我一直无法提出一种可以满足此目标的标准化解决方案。

Instead, I came up with a normalized solution where there is a separate table for Components at each of the three possible component levels. 相反,我想出了一个规范化的解决方案,其中在三个可能的组件级别的每个级别都有一个单独的组件表。 However, I am not really comfortable with this because it triples the number of tables capturing links between components and entitites A, B, and C (9 total link tables rather than 3 if all components were in one table). 但是,我对此并不满意,因为它将捕获组件和实体A,B和C之间的链接的表的数量增加了两倍(总共9个链接表,如果所有组件都在一个表中,则为3个)。

Here is what the design I came up with looks like: 这是我想到的设计的样子:

TABLE: Group_1_Components

ATTRIBUTES: Row_ID, Category, Component

RELATES-TO: Group_1_Components_A_Links, Group_1_Components_B_Links, Group_1_Components_C_Links, Group_2_Components

TABLE: Group_2_Components

ATTRIBUTES: Row_ID, Group, Component, Group_1_Component_Row_ID

RELATES-TO: Group_2_Components_A_Links, Group_2_Components_B_Links, Group_2_Components_C_Links, Group_1_Components, Group_3_Components

TABLE: Group_3_Components

ATTRIBUTES: Row_ID, Group, Component, Group_2_Component_Row_ID

RELATES-TO: Group_3_Components_A_Links, Group_3_Components_B_Links, Group_3_Components_C_Links, Group_2_Components

Each of the 9 links tables contain two Row IDs to address a many-to-many relationship with either table A, B, or C. 9个链接表中的每个表都包含两个行ID,以解决与表A,B或C的多对多关系。

Is this a reasonable design or am I overlooking a simpler, more typical solution? 这是一个合理的设计,还是我忽略了一个更简单,更典型的解决方案? I looked at a few design techniques specific to capturing hierarchies in a relational database, notably the adjacency list, but I am not sure they fit here, nor do they appear to be normalized solutions. 我研究了一些特定的设计技术,这些技术专门用于捕获关系数据库中的层次结构,尤其是邻接表,但是我不确定它们是否适合此处,也不确定它们是否是标准化的解决方案。

It should be noted that the hierarchy will be seldomly modified; 应该注意的是,很少会修改层次结构。 it will frequently be read where reads retrieve either all of the components or components at a specific level for a selected group. 它会经常被读取,其中读取将检索所选组的所有组件或特定级别的组件。 The link tables to entities A, B, and C will be written to regularly. 到实体A,B和C的链接表将定期写入。

Any and all suggestions are welcome. 任何和所有建议都欢迎。 Thanks in advance for your help. 在此先感谢您的帮助。 Brian 布赖恩

I suggest that you de-normalize your data so that your hierarchy is based on component/group entities, so that you match "regular" hierarchies. 我建议您对数据进行非规范化,以便您的层次结构基于组件/组实体,以便与“常规”层次结构匹配。 In this case you can have the following tables: 在这种情况下,您可以具有下表:

a) Components a)组件

b) Groups b)组

c) Component_Groups - with a unique key on component_id and group_id to ensure that you only have one combination for each component and group c)Component_Groups-在component_id和group_id上具有唯一键,以确保每个组件和组只有一个组合

In this case then your hierarchy will be: Category -> Component_Group -> Component_Group -> Component_Group 在这种情况下,您的层次结构将是:类别-> Component_Group-> Component_Group-> Component_Group

Another option for this kind of problem is using a self-referencing table. 此类问题的另一种选择是使用自引用表。 Just one table. 一张桌子

Single table with ID, PARENT_ID and a TYPE so you can distinguish CATEGORY, GROUP and COMPONENT. 具有ID,PARENT_ID和TYPE的单个表,因此您可以区分CATEGORY,GROUP和COMPONENT。

All categories would have no PARENT_ID and then you could search for all child objects where the parent id is equal to the id of the category you want to dive deeper into. 所有类别都没有PARENT_ID,然后您可以搜索其父ID等于您要深入研究的类别ID的所有子对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM