4

我有这个层次来分类汽车:

                             Color
                           /       \
                      Light        Dark
                    /   |   \       /   |   \
                  Red Green Blue  Red Green Blue
                   |    |     |    |    |     |
                 Car1  Car2  Car3 Car4 Car5  Car6

但我也可以有这个层次结构:

                             Color
                      /       |       \
                   Red      Green      Blue
                  /   \     /    \     /   \
               Light Dark Light Dark Light Dark
                  |    |     |    |    |     |
                Car1  Car4  Car2 Car5 Car3  Car6

如何在表格中创建这些?

我的想法是这样的:

Id | ParentId | ParentId2 | Name
--------------------------------
1    Null         Null       Color
2      1                     Light
3      1                     Dark

但如果我想添加更多层次结构,添加许多 ParentId 列似乎并不好。而且我想我也会遇到多个父母的问题。

目标是将这些层次结构放在树视图控件中,用户只需单击一个按钮即可调用他想要查看的任何层次结构。

谢谢 !

4

2 回答 2

2

是否应将其视为不同的层次结构类型...

Id | ParentId | Type | Name
--------------------------------
1    Null         1      Color
2      1          1       Light
3      1          1       Dark
4    Null         2      Color
5      4          2       Red
6      4          2       Green
7      4          2       Blue
8      5          2       Light
...

或者只是规范化为 3 个表格:汽车、颜色、颜色色调

因此,您可以查询原始数据,然后在代码中生成树结构循环。

于 2013-01-10T08:42:26.320 回答
2

Personally I prefer the nested sets when it comes to hierarchies stored in sql. There are many variations to the technique depending on your constraints and there are many resources over the net about CRUD sql routines.

I have to say that when you have large trees you will have a small performance penalty at Create/Update/Delete top parents but you have excellent performance when you Read and I think this is preferred over a recursive model where you have the other way around.

Edit:

I see the multiple parent issue. I would suggest a change in your approach by merging the 2nd and 3rd level. You would have something like

**Node**      |       **Left**      |     **Right**      |     **Level**
Color                 1                   26                   1
LightRed              2                   5                    2
LightGreen            6                   9                    2
LightBlue             10                  13                   2
DarkRed               14                  17                   2
DarkGreen             18                  21                   2
DarkBlue              22                  25                   2
Car1                  3                   4                    3
Car2                  7                   8                    3
Car3                  11                  12                   3
Car4                  15                  16                   3
Car5                  19                  20                   3
Car6                  23                  24                   3

The query to get all the cars that have color would be something like

select * 
from hierarchy
where left > 1 and right < 26 and level = 3

Edit2:

a-horse-with-no-name correctly pointed that your DBMS may support recursive models. If so then it may be a better solution for you. Here is some documentation for Sql Server.

于 2013-01-10T08:44:19.913 回答