database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
ㆍPostgreSQL
Firebird
Oracle
Informix
Sybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
PostgreSQL Q&A 2990 게시물 읽기
No. 2990
Tree-structure functions (3)
작성자
정재익
작성일
2001-06-21 23:27
조회수
1,681

원본출처 : http://www.brasileiro.net/postgres/cookbook/view-one-recipe.adp?recipe_id=297

 

 

Submitted on: 05-11-2001

Description:

Another category tree. This one allows a category to have multiple parents.

Code:

 

Alan Young (alany@subdimension.com)

 

This code is freely available ... you don't have to make

your stuff open if you don't want to but you probably won't

get the benefit of this community if you do.

 

The setup goes like so:

 

create table category (

id serial not null primary key,

shortname not null text,

longname text,

description text

);

 

create unique index category_shortname on category ( shortname );

 

insert into table ( id, shortname, longname, description )

values ( 0, 'ROOT', 'ROOT', 'ROOT' );

 

create table subcategory (

category_id int4 references category ( id ),

parent_id int4 references category ( id )

);

 

create function validate_subcategory_parent_id () returns opaque as '

begin

if new.parent_id = new.category_id then

raise exception ''Parent ID (parent_id) and Category ID (category_id) cannot be the same.'';

end if;

 

return new;

end;

' language 'plpgsql';

 

create trigger validate_subcategory

before insert or update on subcategory

for each row execute procedure validate_subcategory_parent_id ();

 

I'm not sure how (or if I even need) to check for circular

references (other than the immediate case as above) because

I want to allow the following possibility:

 

CatA

CatB

CatA

CatC

CatA

 

I've got four different types of results that I need and

I'm stumped on two of them ... ideas would be appreciated.

 

1. A category's parent:

SELECT shortname, longname, description

FROM category c, subcategory s

WHERE s.category_id = [category]

AND c.id = s.parent_id

 

2. All ancestor's of a category:

 

3. All immediate children of a category:

SELECT shortname, longname, description

FROM category c, subcategory s

WHERE s.parent_id = [category]

AND c.id = s.category_id

 

4. All descendants of a category:

 

These would all be created as views. I need to go home so

I must close this, but I will add more as I learn more.

[Top]
No.
제목
작성자
작성일
조회
2993두 datetime 사이의 overlap interval 구하기
정재익
2001-06-21
2041
2992다른 time zone 으로 시간 변경하기
정재익
2001-06-21
1840
2991날짜 계산함수 -- DATE_ADD(), DATE_SUB()
정재익
2001-06-21
2481
2990Tree-structure functions (3)
정재익
2001-06-21
1681
2989Tree-structure functions (2)
정재익
2001-06-21
1969
2988Tree-structure functions (1)
정재익
2001-06-21
2082
2987Writing Large Objects to Postgresql via ODBC using VB
정재익
2001-06-21
1849
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.018초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다