Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - How to store and navigate hierarchies?

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

like image 952
realcals Avatar asked Sep 02 '08 04:09

realcals


People also ask

What is the best database to store hierarchical data?

Document based database like MongoDB, and Redis are great for small scale, hierarchical data with a relatively small amount of children for each entry.

How do you query hierarchical data?

A database evaluates hierarchical queries in the following order: A join, if present, is evaluated first, whether the join is specified in the FROM clause or with WHERE clause predicates. The CONNECT BY condition is evaluated. Any remaining WHERE clause predicates are evaluated.

What type of SQL query handles hierarchical data?

A hierarchical query is a type of SQL query that handles hierarchical model data. They are special cases of more general recursive fixpoint queries, which compute transitive closures. In standard SQL:1999 hierarchical queries are implemented by way of recursive common table expressions (CTEs).


1 Answers

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
like image 160
markus Avatar answered Oct 05 '22 17:10

markus