Re: cascading delete - recursivity

From: Jan Wieck <JanWieck(at)Yahoo(dot)com>
To: Postgresql <frederes(at)free(dot)fr>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: cascading delete - recursivity
Date: 2001-06-12 17:40:23
Message-ID: 200106121740.f5CHeNC02301@jupiter.us.greatbridge.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Postgresql wrote:
> Hi,
>
> I have a table of categories (it's pseudo-sql...) :
>
> CATEGORIES
> (
> id_category PRIMARY KEY,
> id_category_parent (FOREIGN KEY ? CONSTRAINT ??) ,
> cat_text
> )
>
> There is recursivity : id_category_parent is -1 (= no parent cat) or the
> id_category of a category in this table.
> What is the SQL statements to create such a table so that when you delete a
> category, all sub-categories
> are automatically deleted ?
> I've tried many things like CONSTRAINT... but the query fails !

You can only use the special NULL value to suppress the
foreign key check on those rows that don't have a parent.
Then it'd be

CREATE TABLE categories (
id_category serial PRIMARY KEY,
id_category_parent integer,

FOREIGN KEY (id_category_parent)
REFERENCES categories (id_category)
ON DELETE CASCADE
);

Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me. #
#================================================== JanWieck(at)Yahoo(dot)com #

_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Alex Pilosov 2001-06-12 18:06:19 Re: audit trail and system catalogs
Previous Message Stephan Szabo 2001-06-12 17:28:44 Re: cascading delete - recursivity