Received: from localhost.localdomain (postgresql.org [64.49.215.8]) by localhost (Postfix) with ESMTP id E0B82475425 for ; Fri, 24 May 2002 14:09:37 -0400 (EDT) Received: from wildbrain.com (unknown [64.1.15.36]) by postgresql.org (Postfix) with ESMTP id 27D27475DA7 for ; Fri, 24 May 2002 14:08:23 -0400 (EDT) Received: from wildbrain.com (spock.wildbrain.com [10.20.2.38]) by wildbrain.com (8.12.2/8.12.2) with ESMTP id g4OI8Bwe011432 for ; Fri, 24 May 2002 11:08:11 -0700 Message-ID: <3CEE818B.9020702@wildbrain.com> Date: Fri, 24 May 2002 11:08:11 -0700 From: Gregory Brauer User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.0rc2) Gecko/20020510 X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Trees in SQL Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Archive-Number: 200205/266 X-Sequence-Number: 7901 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