Its been a while since I wrote this and its kinda fuzzy at this hour, but
this will give you a general direction to go and you can work out the
If you know the depth of the tree then you can create a field of a specified
length and store something like:
This allows you to select all records that are in level "B" fairly easily
with a "like" clause. Note that everything has a root of "A".
Or you can create two fields:
This one is confusing and there is no easy way to show the tree in text.
You cannot use the same number for a node. eg. if your first level is "A"
then none of your sub levels can be "A". So use the sequence generator to
generate a unique key for each node. Then your select would look something
Select * from hierachy_table where parent_key = child_key and parent_key
This should give you all branches under the "A" node. To get the
indention...hmmm try ordering the records by parent/child and writing code
that loops thru the records? That one stumps me...gotta sleep on it :)
DISCLAIMER: This might not be 100% accurate...its a seat of the pants
memory. Anyone who knows this please feel free to correct my errors :)
From: Kaare Rasmussen <kar(at)webline(dot)dk>
To: pgsql-general(at)postgreSQL(dot)org <pgsql-general(at)postgreSQL(dot)org>
Date: Friday, February 26, 1999 3:02 AM
Subject: [GENERAL] Tree structure
>I can't figure this one out. I need a tree structure like this
>This should somehow show up like this
>The whole excercise is because I'd like to show a tree structure:
>- - 4
>Is this possible with PostgreSQL?
pgsql-general by date
|Next:||From: Roderick A. Anderson||Date: 1999-02-26 13:40:27|
|Subject: SQL/Relational Design Text Book recommendations|
|Previous:||From: Peter T Mount||Date: 1999-02-26 11:00:30|
|Subject: Re: [GENERAL] Tree structure|