KBD

Keith Devens .com

Thursday, December 4, 2008 Flag waving
Any sufficiently complicated C or Fortran program contains an ad hoc, informally specified, bug-ridden, slow implementation of half of... – Philip Greenspun (Greenspun's Tenth Rule)
← Unintentionally ironic quote of the dayConcepts, Techniques, and Models of Computer Programming →

Daily link icon Friday, June 20, 2003

Trees in SQL

It turns out there's another way to store hierarchical data in SQL that I didn't know existed. Basically, rather than storing the "parent" for each record (with the root having a NULL parent), it turns out you can store a tree by storing "left" and "right" values for each element of the tree, where left and right are numbers that would be assigned by a preorder traversal of the tree.

Via Simon, check out this article from SitePoint.com by Gijs Van Tulder: Storing Hierarchical Data in a Database, and via a comment on Simon's blog, check out database guru Joe Celko's explanation of the same thing.

The first article has more of a tutorial flavor, and has nice diagrams, but Celko's article goes a little more in depth. Also, Gijs only used a binary tree in his example, so I wasn't sure if it'd work for more-than-binary trees, but Celko's example shows that it can.

The only downside to this technique is that it's more of a pain to insert new rows or delete existing ones, but the benefits seem to outweigh the downsides, especially for large or heavily nested recordsets. This is really a great technique, and I'm glad that it's now in my repetoire. I didn't realize this was possible. Thanks for finding this, Simon Smiley

← Unintentionally ironic quote of the dayConcepts, Techniques, and Models of Computer Programming →

Comments XML gif

Jay Fienberg (http://icite.net/blog/) wrote:

There is a really excellent booklet with some unique info, called "Tree Processing in SQL", by Rozenshtein, Abramovich, and Alexandrova. It is available through http://sqlforum.com/.

This booklet has some unique techniques for optimized processing of tress in SQL, using the parent/child storage arrangement.

Clecko's info on trees and graphs (which are also covered in two chapters in his SQL for Smarties book) is great for understanding different storage arrangements, each of which has its own advantages for different uses.

∴ Jay Fienberg | 20-Jun-2003 4:18pm est | http://icite.net/blog/ | #2232

Keith (http://www.keithdevens.com/) wrote:

Thanks Jay. I actually have Celko's Smarties book, so I should have already been aware of this technique. Smiley I'll have to look at those chapters when I get a moment of free time.

Keith | 20-Jun-2003 4:23pm est | http://www.keithdevens.com/ | #2234

dev@ wrote:

If you still interesting in this have a look at
http://www.codeproject.com/cs/database/Trees_in_SQL_databases.asp

∴ dev@ | 10-Mar-2005 4:05am est | #7177

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

December 2008
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 4 posts

Recent comments XML

Girls, please don't get breast implants

I have 34 A breast but at 22 years​old they seem to be growing again​which ...

76.64.120.153: Dec 3, 10:00am

Perl 6 1.0 in March?

Doh, my mistake. I'm aware of the​relation between Parrot and Rakudo​but I'...

Keith: Dec 2, 1:03am

Free image hosting sites

Well, TinyPic has this in its​FAQ:

> Images and videos is in​your accoun...

Keith: Dec 1, 1:13am

Join a NameValueCollection into a querystring in C#

Well with a lamba expression, this​is what I came up​with:

?!code:csharp...

Gustaf Lindqvist: Nov 30, 4:38pm

Generated in about 1.541s.

(Used 8 db queries)

mobile phone