Re: How to represent a tree-structure in a relational database

Поиск
Список
Период
Сортировка
От Ron Peterson
Тема Re: How to represent a tree-structure in a relational database
Дата
Msg-id 3A4B4D8C.9E542934@yellowbank.com
обсуждение исходный текст
Ответ на RE: How to represent a tree-structure in a relational database  ("Stuart Statman" <stu@slammedia.com>)
Список pgsql-sql
Stuart Statman wrote:
> 
> I would suggest, instead, to create a table that represents your hierarchy
> without adding columns. For example :
> 
> create table Category (
> CategoryID       int4  not null  primary key,
> ParentCategoryID int4  not null  REFERENCES Category (CategoryID),
> CategoryName     varchar(100)
> );

Another possibility would be to use two tables to represent the data
structure.

CREATE SEQUENCE category_node_id_seq;
CREATE TABLE category_node (name    TEXT    NOT NULL,
id    INTEGER    DEFAULT NEXTVAL('category_node_id_seq')    PRIMARY KEY
);

CREATE TABLE category_edge (parent    INTEGER    NOT NULL    REFERENCES category_node(id),
child    INTEGER    NOT NULL    REFERENCES category_node(id)
);

This structure is more 'normal' in the sense that nodes without children
(in a tree, the leaf nodes) don't have records in the edge table.

What either of these structures allow to do is create directed graph
structures.  If you'd like to constrain this structure to be a tree, you
have to enforce that restriction with procedural code.

-Ron-


В списке pgsql-sql по дате отправления:

Предыдущее
От: Ron Peterson
Дата:
Сообщение: Re: How to represent a tree-structure in a relational database
Следующее
От: John Reid
Дата:
Сообщение: Re: system catalog info