pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Owen Jacobson <ojacobson@osl.com>
To: Bryce Nesbitt <bryce1@obviously.com>
To: pgsql-sql@postgresql.org
Subject: Re: Delete with join -- deleting related table entries?
Date: Wed, 8 Feb 2006 09:32:27 -0800
Message-ID: <144D12D7DD4EC04F99241498BB4EEDCC20CC56@nelson.osl.com> (raw)

Bryce Nesbitt wrote:

> When I delete a record from a certain table, I need to delete a
> (possibly) attached note as well.  How can I do this with 
> postgres?  The
> tables are like this:
> 
> reservation
>     reservation_id
>     stuff...
> 
> isuse
>     issue_id
>     reservation_id
        references reservation (reservation_id) -- ADD
>     stuff..
> 
> note
>     issue_id
        references isuse (issue_id) -- ADD (kept typo in example)
>     text comments...
> 
> A select that pulls out what I want to delete is:
> 
>     SELECT reservation_id,issue_id,note_id,eg_note.comments FROM
> eg_reservation
>        LEFT JOIN eg_issue USING (reservation_id)
>        LEFT JOIN eg_note USING (issue_id)
>        WHERE reservation_id > condition;
> 
> Can anyone help me turn this into a DELETE statement?

1.  Add foreign key references between the tables to ensure that there are only notes and issues (isuses? :) for existing issues and reservations respectively.  You can make those references 'ON DELETE CASCADE' so that a delete of the original reservation cascades down to related entries in the issue table, which in turn cascade down to the related entries in the note table.

2.  Or...

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;
END;

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.

Get yourself a good, non-MySQL-specific database book, which should explain how referential integrity is handled in databases.

-Owen



view thread (10+ messages)  latest in thread

Message-ID: <144D12D7DD4EC04F99241498BB4EEDCC20CC56@nelson.osl.com>
Permalink:  ../144D12D7DD4EC04F99241498BB4EEDCC20CC56@nelson.osl.com/
Also on:    postgresql.org/message-id/144D12D7DD4EC04F99241498BB4EEDCC20CC56@nelson.osl.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: ojacobson@osl.com, bryce1@obviously.com
  Subject: Re: Delete with join -- deleting related table entries?
  In-Reply-To: <144D12D7DD4EC04F99241498BB4EEDCC20CC56@nelson.osl.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox