1

I'm new to database design and just wanted a few opinions as to whether or not I'm going about this in a logical way. I'm building a simple MySQL database through which users will upload items to a previously existing (unchanging) hierarchical tree.

For a simple example:

Section 1

   Division 1.1
      Subdivision 1.1.1
      Subdivision 1.1.2

   Division 1.2
      Subdivision 1.2.1
      Subdivision 1.2.2

Section 2

   Division 2.1
      Subdivision 2.1.1
      Subdivision 2.1.2

   Division 2.2
      Subdivision 2.2.1
      Subdivision 2.2.2

The structure of the tree will not change, the users will simply upload products which will fall under the subdivisions (an industry-specific way to organize a large number of products). I've done research on adjacency lists and nested sets, but am leaning towards 3 separate tables each referencing its parents primary key (seeing as the top levels of the tree will virtually never change). When a new product is uploaded, it will reference all three of its parents (if it is filed under subdivision 1.1.2, it is necessarily part of section 1, division 1). The final tree will have 4 sections, with 10 divisions in each section and 10 subdivisions in each division. Does this make sense as a starting strategy?

Interaction with the database is more-or-less limited to inputting information and categorizing it accurately, and then being able to show how many products one has filed in either section, division, or subdivision. The library will be displayed in a series of drop-down lists, and clicking a list item will bring up the stored info.

Any recommendations or references to literature/tutorials would be appreciated!

3
  • 1
    If you never will have more than 3 levels, then you could go 'cheap' and simply have (section_id, div_id, subdiv_id) and keep it all in a single table. it'd be vulnerable to hierarchy mistakes like sec #1, div 2.1, subsec 1.1.1 however.
    – Marc B
    Mar 22, 2012 at 19:57
  • I agree with Marc B your structure is technically hierarchial, but it's structure is not dynamic enough to warrant an advanced data structure. you could avoid category mistakes with simple constraints
    – David Chan
    Mar 22, 2012 at 20:11
  • Thanks so much Marc and David! David, could you expand on what you mean by simple constraints? Marc B, if all products are required to be assigned to a subsection (like "Bill's Macintosh Apple Orchard" being filed under AmericanFruit>Apples>Macintosh), am I right then in saying I would have one table which describes the unchanging tree, and then a second table which holds specific info and id for "Bill's Macintosh Apple Orchard" and a reference to its parent subdiv_id? I hope that's clear :)
    – NallyRoll
    Mar 23, 2012 at 1:21

4 Answers 4

2

Because "the structure of the tree will not change" you need section, division and subdivision tables (also products).

create table section (
  id int primary key,
  name varchar(100) 
);

create table division (
  id int primary key,
  name varchar(100) ,
  section_id int references section
);

create table subdivision (
  id int primary key,
  name varchar(100) ,
  division_id int references division
);

create table product (
  id int primary key,
  name varchar(100) ,
  subdivision_id int references subdivision
);

For others requerimets, for example:

  • Unknow depth tree.
  • Assign products to several tree levels.

You will look for a parent-child soluction, for example:

create table tree (
  id int primary key,
  parent_id int null references tree,
  name varchar(100) 
);

create table product (
  id int primary key,
  name varchar(100) ,
  subdivision_id int references tree
);
1
  • Thanks alot for this! This is exactly what I was trying to express. I began this process but was unsure if it was regarded as an acceptable solution.
    – NallyRoll
    Mar 23, 2012 at 2:34
2

Since the categories (sections) are more or less static, what you can do is assign each category a 'high' and a 'low' number, such as

catid  name        low      high
1      Section1     1       20
2      Div1.1       2       10
3      Div1.2       11      19
4      Section2     21      40
5      Div2.1       22      29
6      Div2.2       30      39

Then have a separate table for the content:

id   catid   content
1    2       fileA 
2    2       fileB
3    5       fileC

To then query ALL items under section 1, you simply query for all items in a category that have a high and low between 1 and 20. To get all items in Div2.1 (and under), you query for all items that have a category who's high and low are between 22 and 29. This makes it very easy to track the number of items under sub categories.

I forget the name of this actual approach (if there is a definitive one), but I've used it a number of times. For structures that dont change much, its a lot easier to work with than the traditional parent_id-child_id type structure.

4
  • I think it's called Nested Sets: mikehillyer.com/articles/managing-hierarchical-data-in-mysql
    – Nathan
    Mar 22, 2012 at 20:12
  • GrandmasterB and others, thanks for the input. I've actually previously drawn out the schema in a nested set diagram (it took a loong time, with about 400 eventual subdivisions). My questions start with the second table you described. Items MUST be categorized under a subdivision, so all inputted information will always be at the same level. What would the code snippet look like to input fileC into Section 2, Division 2, Subdivision 1 (from your example)? Also (shot in the dark) is there any easy way to assign the low/high numbers to a large number of tree nodes?
    – NallyRoll
    Mar 23, 2012 at 1:04
  • You can convert a recursive table into a nested set. Check this post: sqlblog.com/blogs/adam_machanic/archive/2006/07/12/… Also this: jsimonbi.wordpress.com/2011/02/14/nested-set-hierarchy
    – Nathan
    Mar 24, 2012 at 14:23
  • @TheNally, add a 'bottom' column to denote the categories that are at the bottom of the tree. Then when adding files, only allow files to be added to those categories. Mar 24, 2012 at 21:50
2

A good solution would to have a recursive table.

Check this post on StackOverflow: Hierarchical Data in MySQL

This way, your design will support having more levels down the tree.

Other interesting articles about this topic:

Managing Hierarchical Data in MySQL

Hierarchical data in MySQL: parents and children in one query

Hierarchical data in MySQL: easy and fast

Recursion-less storage of hierarchical data in a relational database

1
  • thanks for the references, a few of these I've checked out, but I'll be sure to look further into recursive tables. Thanks again!
    – NallyRoll
    Mar 23, 2012 at 2:32
0

Have a look at nested sets and nested intervals:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.