SQL - How to store and navigate hierarchies?

What are the ways that you use to model and retrieve hierarchical info in a database?


I like the Modified Preorder Tree Traversal Algorithm. This technique makes it very easy to query the tree.

But here is a list of links about the topic which I copied from the Zend Framework (PHP) contributors webpage (posted there by Posted by Laurent Melmoux at Jun 05, 2007 15:52).

Many of the links are language agnostic:

There is 2 main representations and algorithms to represent hierarchical structures with databases :

  • nested set also known as modified preorder tree traversal algorithm
  • adjacency list model

It's well explained here:

  • http://www.sitepoint.com/article/hierarchical-data-database
  • Managing Hierarchical Data in MySQL
  • http://www.evolt.org/article/Four_ways_to_work_with_hierarchical_data/17/4047/index.html

Here are some more links that I've collected:

  • http://en.wikipedia.org/wiki/Tree_%28data_structure%29
  • http://en.wikipedia.org/wiki/Category:Trees_%28structure%29

adjacency list model

  • http://www.sqlteam.com/item.asp?ItemID=8866

nested set

  • http://www.sqlsummit.com/AdjacencyList.htm
  • http://www.edutech.ch/contribution/nstrees/index.php
  • http://www.phpriot.com/d/articles/php/application-design/nested-trees-1/
  • http://www.dbmsmag.com/9604d06.html
  • http://en.wikipedia.org/wiki/Tree_traversal
  • http://www.cosc.canterbury.ac.nz/mukundan/dsal/BTree.html (applet java montrant le fonctionnement )

Graphes

  • http://www.artfulsoftware.com/mysqlbook/sampler/mysqled1ch20.html

Classes :

Nested Sets DB Tree Adodb

  • http://www.phpclasses.org/browse/package/2547.html

Visitation Model ADOdb

  • http://www.phpclasses.org/browse/package/2919.html

PEAR::DB_NestedSet

  • http://pear.php.net/package/DB_NestedSet
  • utilisation : https://www.entwickler.com/itr/kolumnen/psecom,id,26,nodeid,207.html

PEAR::Tree

  • http://pear.php.net/package/Tree/download/0.3.0/
  • http://www.phpkitchen.com/index.php?/archives/337-PEARTree-Tutorial.html

nstrees

  • http://www.edutech.ch/contribution/nstrees/index.php

The definitive pieces on this subject have been written by Joe Celko, and he has worked a number of them into a book called Joe Celko's Trees and Hierarchies in SQL for Smarties.

He favours a technique called directed graphs. An introduction to his work on this subject can be found here


What's the best way to represent a hierachy in a SQL database? A generic, portable technique?

Let's assume the hierachy is mostly read, but isn't completely static. Let's say it's a family tree.

Here's how not to do it:

create table person (
person_id integer autoincrement primary key,
name      varchar(255) not null,
dob       date,
mother    integer,
father    integer
);

And inserting data like this:

person_id   name      dob       mother father  
1           Pops      1900/1/1   null   null  
2           Grandma   1903/2/4   null   null  
3           Dad       1925/4/2   2      1  
4           Uncle Kev 1927/3/3   2      1
5           Cuz Dave  1953/7/8   null   4
6           Billy     1954/8/1   null   3

Instead, split your nodes and your relationships into two tables.

create table person (
person_id integer autoincrement primary key,
name      varchar(255) not null,
dob       date
);

create table ancestor (
ancestor_id   integer,
descendant_id integer,
distance      integer
);

Data is created like this:

person_id   name      dob       
1           Pops      1900/1/1  
2           Grandma   1903/2/4   
3           Dad       1925/4/2   
4           Uncle Kev 1927/3/3
5           Cuz Dave  1953/7/8   
6           Billy     1954/8/1   

ancestor_id  descendant_id  distance
1            1              0
2            2              0
3            3              0
4            4              0
5            5              0
6            6              0
1            3              1
2            3              1
1            4              1
2            4              1
1            5              2
2            5              2
4            5              1
1            6              2
2            6              2
3            6              1

you can now run arbitary queries that don't involve joining the table back on itself, which would happen if you have the heirachy relationship in the same row as the node.

Who has grandparents?

select * from person where person_id in 
    (select descendant_id from ancestor where distance=2);

All your descendants:

select * from person where person_id in 
    (select descendant_id from ancestor 
    where ancestor_id=1 and distance>0);

Who are uncles?

select decendant_id uncle from ancestor 
    where distance=1 and ancestor_id in 
    (select ancestor_id from ancestor 
        where distance=2 and not exists
        (select ancestor_id from ancestor 
        where distance=1 and ancestor_id=uncle)
    )

You avoid all the problems of joining a table to itself via subqueries, a common limitation is 16 subsuqeries.

Trouble is, maintaining the ancestor table is kind of hard - best done with a stored procedure.


I've got to disagree with Josh. What happens if you're using a huge hierarchical structure like a company organization. People can join/leave the company, change reporting lines, etc... Maintaining the "distance" would be a big problem and you would have to maintain two tables of data.

This query (SQL Server 2005 and above) would let you see the complete line of any person AND calculates their place in the hierarchy and it only requires a single table of user information. It can be modified to find any child relationship.

--Create table of dummy data
create table #person (
personID integer IDENTITY(1,1) NOT NULL,
name      varchar(255) not null,
dob       date,
father    integer
);

INSERT INTO #person(name,dob,father)Values('Pops','1900/1/1',NULL);  
INSERT INTO #person(name,dob,father)Values('Grandma','1903/2/4',null);
INSERT INTO #person(name,dob,father)Values('Dad','1925/4/2',1);
INSERT INTO #person(name,dob,father)Values('Uncle Kev','1927/3/3',1);
INSERT INTO #person(name,dob,father)Values('Cuz Dave','1953/7/8',4);
INSERT INTO #person(name,dob,father)Values('Billy','1954/8/1',3);

DECLARE @OldestPerson INT; 
SET @OldestPerson = 1; -- Set this value to the ID of the oldest person in the family

WITH PersonHierarchy (personID,Name,dob,father, HierarchyLevel) AS
(
   SELECT
      personID
      ,Name
      ,dob
      ,father,
      1 as HierarchyLevel
   FROM #person
   WHERE personID = @OldestPerson

   UNION ALL

   SELECT
    e.personID,
      e.Name,
      e.dob,
      e.father,
      eh.HierarchyLevel + 1 AS HierarchyLevel
   FROM #person e
      INNER JOIN PersonHierarchy eh ON
         e.father = eh.personID
)

SELECT *
FROM PersonHierarchy
ORDER BY HierarchyLevel, father;

DROP TABLE #person;

FYI: SQL Server 2008 introduces a new HierarchyID data type for this sort of situation. Gives you control over where in the "tree" your row sits, horizontally as well as vertically.