pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedcascading delete - recursivity
3+ messages / 3 participants
[nested] [flat]
* cascading delete - recursivity
@ 2001-06-09 16:22 Postgresql <frederes@free.fr>
2001-06-12 17:28 ` Re: cascading delete - recursivity Stephan Szabo <sszabo@megazone23.bigpanda.com>
2001-06-12 17:40 ` Re: cascading delete - recursivity Jan Wieck <JanWieck@Yahoo.com>
0 siblings, 2 replies; 3+ messages in thread
From: Postgresql @ 2001-06-09 16:22 UTC (permalink / raw)
To: pgsql-sql
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 !
Thanks !
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: cascading delete - recursivity
2001-06-09 16:22 cascading delete - recursivity Postgresql <frederes@free.fr>
@ 2001-06-12 17:28 ` Stephan Szabo <sszabo@megazone23.bigpanda.com>
1 sibling, 0 replies; 3+ messages in thread
From: Stephan Szabo @ 2001-06-12 17:28 UTC (permalink / raw)
To: Postgresql <frederes@free.fr>; +Cc: 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]
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: cascading delete - recursivity
2001-06-09 16:22 cascading delete - recursivity Postgresql <frederes@free.fr>
@ 2001-06-12 17:40 ` Jan Wieck <JanWieck@Yahoo.com>
1 sibling, 0 replies; 3+ messages in thread
From: Jan Wieck @ 2001-06-12 17:40 UTC (permalink / raw)
To: Postgresql <frederes@free.fr>; +Cc: 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@Yahoo.com #
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2001-06-12 17:40 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-06-09 16:22 cascading delete - recursivity Postgresql <frederes@free.fr>
2001-06-12 17:28 ` Stephan Szabo <sszabo@megazone23.bigpanda.com>
2001-06-12 17:40 ` Jan Wieck <JanWieck@Yahoo.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox