Subject Re: [firebird-support] Self-referencing table SQL question
Author Sam Hunt
diwic2 wrote:
> I have a self-referencing table, something like:
>
> id integer
> name varchar(98)
> parentid integer (references another row in the same table)
>
> say I have these rows:
> ID ParentID Name
> 1 <null> World
> 2 1 Europe
> 3 2 Sweden
> 4 2 Germany
> 5 3 Stockholm
> 6 3 Gothenburg
> 7 4 Berlin
>
You could define your tbl struct as: Data_Type 1 = Parent and 2 = child
ID* Date_Type* ParentID Name
1 1 <null> New World
2 1 <null> Old World
1 2 2 Sweden
2 2 2 Germany
3 2 2 Stockholm
4 2 1 America

(In the above, the parent recs and child recs were inserted in any order
but "ordered by" Data_Type,ID ASC.)

then
select Name where ParentID=2
would yield Sweden,Germany, and Stockholm

and
select Name where ParentID=1
would yield America.

The Data_Type column isn't required, but helps to conceptualize the data
relationships.

My 2 cents.
Sam D