Re: Constraints/On Delete...

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: "Boget, Chris" <chris(at)wild(dot)net>
Cc: "'pgsql-general(at)postgresql(dot)org'" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Constraints/On Delete...
Date: 2003-01-04 16:21:07
Message-ID: 20030104081957.D20990-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sat, 4 Jan 2003, Boget, Chris wrote:

> Newbie alert!! :p
>
> I have 2 tables already created
>
> CREATE TABLE "first" (
> "name" varchar (100) NOT NULL,
> "record_num" SERIAL,
> PRIMARY KEY ("record_num"));
>
> CREATE TABLE "second" (
> "text" varchar (10) NOT NULL,
> "name" int4 REFERENCES "first"("record_num") NOT NULL,
> "record_num" SERIAL ,
> PRIMARY KEY ("record_num"));
>
> and have records in each. The "name" field in the "second"
> table contains data that matches data in the "first". Now,
> when I go to delete a row from "first", I'm getting the error:
>
> ERROR: $1 referential integrity violation - key in first still referenced
> from second
>
> which I can understand. Now, how can I alter the "first" table
> so that when I delete rows from it, the corresponding rows from
> the "second" table are deleted as well? I was looking at the
> ALTER TABLE syntax but the only thing I saw there was adding a
> foreign key. But I already have a foreign key set up by the
> REFERENCES in the create definition for the "second" table, yes?
> So what do I need to do?

You don't alter first. You need to drop the constraint on second and add
a new constraint on second with on delete cascade.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Boget, Chris 2003-01-04 16:41:05 Re: Constraints/On Delete...
Previous Message Tom Lane 2003-01-04 16:09:18 Re: select for update with left outer joins?