Re: delete on cascade

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: Luca Ferrari <fluca1978(at)infinito(dot)it>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: delete on cascade
Date: 2006-10-23 15:48:15
Message-ID: 20061023084359.V59438@megazone.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


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.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Andrew Sullivan 2006-10-23 17:16:49 Re: conversion of numeric field from MSSQL to postgres
Previous Message chester c young 2006-10-23 15:41:25 Re: delete on cascade