Trees in SQL - Mailing list pgsql-sql

From Gregory Brauer
Subject Trees in SQL
Date
Msg-id 3CEE818B.9020702@wildbrain.com
Whole thread Raw
Responses Re: Trees in SQL  (Oleg Bartunov <oleg@sai.msu.su>)
Re: Trees in SQL  (Achilleus Mantzios <achill@matrix.gatewaynet.com>)
Re: Trees in SQL  (Josh Berkus <josh@agliodbs.com>)
contrib/tree issues  (achill@matrix.gatewaynet.com)
List pgsql-sql
I hope this isn't an overly broad topic that ends up diverging into graph
theory, but I have a tree structure of identical items (analogous to a
filesystem directory tree) that I need to store in Postgres.  The
"obvious" design is to give the table that will represent these objects
a field identifying its "parent" that is a relation to the same table.
However, this seems to make many common SQL queries rather difficult.

What sort of strategies are best for storing tree structures in a
relational database, and how would one structure SQL queries to find,
say, "all of the children anywhere under this node", or to represent
the condition "if this node is a child at any depth under this other
node"?  Are there good strategies for preventing cycles?

I'd appreciate any insights anyone can give.

Thanks.

Greg Brauer
greg@wildbrain.com



pgsql-sql by date:

Previous
From: Jeff Eckermann
Date:
Subject: Re: Functions with dynamic queries
Next
From: Gregory Brauer
Date:
Subject: Re: Trees in SQL