Which database system? Oracle (and PostgreSQL, kinda) handle tree structures without a problem. For example, in Oracle you could do the following (all on the SQL*Plus commandline):
CREATE TABLE FOO ( id NUMBER NOT NULL ,name VARCHAR2(20) ,parent NUMBER ); INSERT INTO FOO VALUES (1, 'First', NULL); INSERT INTO FOO VALUES (2, 'Second 1', 1); INSERT INTO FOO VALUES (3, 'Second 2', 1); INSERT INTO FOO VALUES (4, 'Third', 2); -- This is a formatting command COLUMN id a40; SELECT parent ,name ,LPAD(' ', 6*(Level - 1)) || id AS id FROM foo START WITH id = 1 CONNECT BY parent = PRIOR id;

I've tested the above, so you can actually just cut'n'paste it. The PG syntax is similar, but not identical, and you have to patch the latest release. (I don't remember where the patch can be downloaded from - google for it.)

Most other RDBMS do not have this kind of functionality built in. There are PL/SQL packages out there to handle this kind of structure, but they're very kludgy to use.

------
We are the carpenters and bricklayers of the Information Age.

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.


In reply to Re: (OT?) Recursive sql queries? by dragonchild
in thread (OT?) Recursive sql queries? by BUU

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.