Re: cascading delete - recursivity

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(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:28:44
Message-ID: Pine.BSF.4.21.0106121025590.53698-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Sat, 9 Jun 2001, 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 !

If you want to use foreign keys, using -1 for no cat is going to mean
you're going to need a -1 category. You're probably better off using
NULL for that.

you probably want something like:
id_category_parent REFERENCES categories ON DELETE CASCADE
[you may want to add ON UPDATE CASCADE]

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Jan Wieck 2001-06-12 17:40:23 Re: cascading delete - recursivity
Previous Message Alex Pilosov 2001-06-12 17:15:27 Re: PL/PGSQL