Thread: help on creating table

help on creating table

From
"pgsql-sql"
Date:
Hi All,

I'm planning to have data in a tree structure when fetched.
e.g.

NODE1  + --- NODE1_1  + --- NODE1_2  |        + --- NODE1_2_1  + --- NODE1_3

Is this possible? How?

I would really appreciate any help.

Sherwin



Re: help on creating table

From
"Albert REINER"
Date:
Saluton,

this is easy:

On Fri, Oct 20, 2000 at 06:48:54PM +0800, pgsql-sql wrote:
...
> NODE1
>    + --- NODE1_1
>    + --- NODE1_2
>    |        + --- NODE1_2_1
>    + --- NODE1_3

create table n (id int4, parent int4, data text);

insert into n (id, data) values (1, 'node 1');
insert into n (id, parent, data) values (2, 1, 'node 1.1');
insert into n (id, parent, data) values (3, 1, 'node 1.2');
insert into n (id, parent, data) values (4, 3, 'node 1.2.1');
insert into n (id, parent, data) values (5, 1, 'node 1.3');

(you will probably want to use a serial for id, etc.)

The idea is to store each node in a row, and to store both the row's
id and the id of the parent node in it. When there is no parent node
(your node NODE_1), parent is NULLL.

HTH,

Albert.


-- 

--------------------------------------------------------------------------
Albert Reiner                                   <areiner@tph.tuwien.ac.at>
Deutsch       *       English       *       Esperanto       *       Latine
--------------------------------------------------------------------------