Re: Delete with join -- deleting related table entries?

From: Bryce Nesbitt <bryce1(at)obviously(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Delete with join -- deleting related table entries?
Date: 2006-02-08 17:41:49
Message-ID: 43EA2D5D.6000304@obviously.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Owen Jacobson wrote:
> BEGIN;
> DELETE FROM note WHERE issue_id IN (SELECT issue_id FROM isuse
> WHERE reservation_id = reservation_to_delete);
> DELETE FROM isuse WHERE reservation_id = reservation_to_delete;
> DELETE FROM reservations WHERE reservation_id = reservation_to_delete;
> COMMIT;
>
> With an appropriate value or expression substituted into reservation_to_delete. This would be the "hard way", but (as it's in a single transaction) will still protect other clients from seeing a partial delete.
Yup, that's exactly how I delete reservations one a time. But here I
need to select a few thousand reservations, and I don't think this will
work:
BEGIN;
DELETE FROM note WHERE issue_id IN (SELECT issue_id FROM isuse
WHERE reservation_id IN
(select reservation_id from reservations where date > magic);
DELETE FROM isuse WHERE reservation_id IN
(select reservation_id from reservations where date > magic)
DELETE FROM reservations WHERE reservation_id IN
(select reservation_id from reservations where date > magic)
COMMIT;

I suppose I can do the subselect as a perl wrapper, but I was thinking
that maybe SQL could do it all for me....

-Bryce

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Markus Schaber 2006-02-08 17:55:14 Re: Delete with join -- deleting related table entries?
Previous Message BigSmoke 2006-02-08 17:36:36 Re: regarding debugging?