Received: from smtp017.mail.yahoo.com (smtp017.mail.yahoo.com [216.136.174.114]) by postgresql.org (8.11.3/8.11.1) with SMTP id f5CHaGE71770 for ; Tue, 12 Jun 2001 13:36:16 -0400 (EDT) (envelope-from janwieck@yahoo.com) Received: from jupiter.us.greatbridge.com (65.196.69.55) by smtp.mail.vip.sc5.yahoo.com with SMTP; 12 Jun 2001 17:36:10 -0000 X-Apparently-From: Received: (from wieck@localhost) by jupiter.us.greatbridge.com (8.11.2/8.11.2) id f5CHeNC02301; Tue, 12 Jun 2001 13:40:23 -0400 From: Jan Wieck Message-Id: <200106121740.f5CHeNC02301@jupiter.us.greatbridge.com> Subject: Re: cascading delete - recursivity In-Reply-To: <9fti5q$2ccg$1@news.tht.net> from Postgresql at "Jun 9, 2001 06:22:51 pm" To: Postgresql Date: Tue, 12 Jun 2001 13:40:23 -0400 (EDT) CC: pgsql-sql@postgresql.org X-Mailer: ELM [version 2.4ME+ PL68 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Archive-Number: 200106/185 X-Sequence-Number: 3441 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