I’m brand new to SQL. I need to build a database to hold data in the following tree:
tree
========
stringA
/ | \
groupA groupB groupC
| | | \
groupW groupX groupY groupZ
I have ~20,000 different ‘stringA’ values. Each one will be organized into 1 or more sub-groups, and each sub-group will be organized into 1 or more parent group. When my program launches, I need to make a list of all the Parent groups. If a user selects one of these groups, I need to make a list of all the sub-groups that are children to this parent. And if a user selects the sub-group, then I need to make a list of all leaf nodes contained in the sub-group.
The number of sub-groups that each “stringA” has varies, as does the number of parent parent-groups for each sub-group.
Can anyone provide suggestions for how to organize this data into tables? Like I said, I’m a complete noobie to SQL so any help is greatly appreciated!
Thanks!
Sunny
I would recommend to use Closure Tables ‘pattern’. That should meet your requirements.
Just google the name or (better) go and buy/steal the SQL Antipatterns book. It has a chapter on creating tree-like structures and one of the explained approaches is closure table. It will explain how/why use them and what are the alternatives.