First, let me thank everyone that commented and read the last blog, “How do you store a tree in a database table?” The feedback was great and helpful for everyone including myself. Probably one of the best interactions between the readers I have seen. I just hope Roustabout and Joe Celko don’t run into each other at a SQL World 2008 and if they do they by each other a beer.

I didn’t expect as much feedback or hits as the posting got, I figured every one shelved their databases for REST/JSON services by now :)

Approaches that came out of the last blog


1.) Nested Sets By Joe Celko

There were lots of comments about the Nested Sets approach. It seems like a valid approach. Don’t see how it works with data that may have more than one parent. The approach is also more complicated to develop from scratch but once the wrapper is completed, it should work pretty efficiently as long as the structure is not changing very often.

http://www.sitepoint.com/article/hierarchical-data-database http://www.intelligententerprise.com/001020/celko.jhtml
http://www.amazon.com/Hierarchies-Smarties- Kaufmann-Management-Systems/dp/1558609202/ref=pd_sim_b_img_1

2.) Roustabout’s Approach

This approach was given in the comments in Part one of the blog. I have tried to decipher this from the comments, but I know this doesn’t do it justice,

Product

ID | Name | Description
————————

ProductCategory

ID | Name | Description
————————
ProductProductCategoryTree

ID | Type | ParentID | ParentType
————————————

ProductProductCategoryPathName

ID | Type | ParentID | ParentType | AncestorID | AncestorType | Sort
————————————————————————

ProductProductCategoryPathNameDesc

ID | Type | ParentID | ParentType | AncestorID | AncestorType | Sort
————————————————————————

http://www.rockstarapps.com/wordpress/?p=88 – His approach is embedded in the comment section of the article.

3.) Oracle Databases developers can use “CONNECT BY”

I am not an Oracle user, but this functionality is really nice and allows developers to select items based on the relationship of an id to parent.

SELECT * FROM tree CONNECT BY PRIOR id = parent_id

http://philip.greenspun.com/sql/trees.html http://www.oracle.com/technology/oramag/oracle/05-may/o35asktom.html

4.) Postgres ltree Module

A couple people brought up the fact that Postgres database allows developers to us their ltree module. I looked at the http://www.postgresql.org website … finally found some documentation, but the information on the ltree module was located on a different site at the link below.

http://www.sai.msu.su/~megera/postgres/gist/ltree/
http://www.postgresql.org/

5.) XML Database

Duke, said the following

“This really isn’t mean to be a facetious answer, but if you have control of which database is used then you might consider using a native XML database instead of MySQL. The free version of DB2 is the best choice.”

Snippet from the IBM website:

“pureXML’s seamless integration of XML with relational data speeds application development, improves search performance with highly optimized XML indexes, and is flexible because both SQL and XQuery can be used to query XML data.”

Click link for more information:
http://www-306.ibm.com/software/data/db2/xml/

6.) My “Pathological Tree” Approach

Won’t go into it here, it is explained in the previous post and I have Sample application and a download to go along with it.

http://www.rockstarapps.com/wordpress/?p=88

The sample application has been upgraded to include the number of SQL calls per page. Located below the product area is the list of all the SQL calls made.

7.) Some people wanted to go Web 2.0 and throwout the structure in favor of the folksonomy. Just Tag It!

MRIQUE,
“who use trees when u can tag ;-)
Probably not the best solution for all cases

That should wrap up all the approaches that were talked about on the previous blog entry. If there are any that were missed or want to comment on the pros and cons of each, enter it in the comment box below. I need to find or develop an in entry post commenting system that allows people to add comments after a particular section.

Summary

There were a couple things I was looking at in the previous post.

  1. Needed to be simple to develop
  2. Needed to work in MySQL. Not many DB2 or Postgres databases provided by hosting providers. Enterprises could use the Oracle, DB2 or Postgres approach.
  3. Have a simple storage structure.

No solution is best for all cases. Look at the above approaches and pick the solution best matches your requirements.

Bob (Buffone)