Showing posts with label databases. Show all posts
Showing posts with label databases. Show all posts

Wednesday, September 27, 2006

Implementing Trees in SQL

Implementing hierarchical trees in SQL can be slightly tricky. Fortunately some good ideas have been presented by Joe Celko. I came across to ways to implement trees from his books.

One is the Adjacency List model, which in simple words makes use of seperate tables to model the nodes and the edges of the trees. One table could hold the Node data, and one table describing the Edge relations. This approach usually helps in normal situations where complex tree operations are not required.

In case one has to perform some complex calculations on the tree, another model - the Nested Tree model is suggested. The Nested Tree Model is quite interesting and very convenient to implement. The idea here is "Containment represents subordination". Quoting his book " Visualise the nested sets model as a little worm with Bates automatic numbering stamp crawling along the "boxes-and-arrows" version of the tree. The worm starts at the top, the root, and makes a complete trip around the tree. When he comes to a node he puts a number in the cell on the side he is visiting and his numbering stamp increments itself. Each node will get two numbers, one for the right side and one for the left side. Computer science majors will recognize this as a modified preorder tree traversal algorithm."


for further reading check out these pointers ....

A good starter tutorial on Adjacency List Model

Joe Celko’s SQL for Smarties Advanced SQL Programming Second Edition

Joe Celko's Trees and Hierarchies in SQL for Smarties

Friday, August 04, 2006

Bits n Bytes - Databases Programming Practices

In a slightly off-dated but worthwhile article on SQL programming conventions, Joe Celko, a premier SQL Guru writes " .... After more than 25 years of the relational model, people are still writing SQL as if it were a 1950s punch card file system. It takes time for a new tool to find its own voice. Motion pictures were originally shot from a fixed position because that is how people viewed stage plays. The engine in a car is in front because that is where the horse went. ... " for further reading visit

for further references check out -