Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id B90339FB266 for ; Mon, 23 Oct 2006 12:48:34 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 18628-09 for ; Mon, 23 Oct 2006 15:48:17 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey- Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by postgresql.org (Postfix) with ESMTP id 2D4689FB2FC for ; Mon, 23 Oct 2006 12:48:16 -0300 (ADT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id DD3EF3526B; Mon, 23 Oct 2006 08:48:15 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id DB3F93525A; Mon, 23 Oct 2006 08:48:15 -0700 (PDT) Date: Mon, 23 Oct 2006 08:48:15 -0700 (PDT) From: Stephan Szabo To: Luca Ferrari Cc: pgsql-sql@postgresql.org Subject: Re: delete on cascade In-Reply-To: <200610231049.16507.fluca1978@infinito.it> Message-ID: <20061023084359.V59438@megazone.bigpanda.com> References: <200610231049.16507.fluca1978@infinito.it> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200610/205 X-Sequence-Number: 26594 On Mon, 23 Oct 2006, Luca Ferrari wrote: > Hi all, > I guess this is an already asked question, but I didn't found an answer, so > apologize me. Imagine I've got two tables: > skill(id,description) // primary key => id > family(id,description) // primary key => id > and I want to associate skills to families: > ass_sf(id_skill,id_family) // appropriate foreign keys Using the type information from the original and assuming it's the same for family, without referential actions that'd look something like: Create table skill(id varchar(20) primary key, description varchar(50)); Create table family(id varchar(20) primary key, description varchar(50)); Create table ass_sf(id_skill varchar(20) references skill, id_family varchar(20) referenced family); > Tables are already created and the database is running. Now I'd like to > implement a delete cascade, thus when I delete a skill also its association > with the family must be deleted. In this case, you'd need to remove the constraint on ass_sf.id_skill and replace it with one like foreign key(id_skill) references skill on delete cascade which you can do with alter table in two steps. The on delete information is associated with the foreign key and deletes matching rows from the referencing table (ass_sf) when rows in the referenced tablen (skill) are deleted. You may also want to think about what the on update behavior should be.