Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 63A62B5D95B for ; Thu, 19 May 2011 05:39:03 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 90840-09 for ; Thu, 19 May 2011 08:38:56 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.ringways.co.uk (hub.ringways.co.uk [77.86.27.30]) by mail.postgresql.org (Postfix) with ESMTP id EF1D9B5D832 for ; Thu, 19 May 2011 05:38:55 -0300 (ADT) Received: from localhost ([127.0.0.1] helo=mail.ringways.co.uk) by mail.ringways.co.uk with esmtp (Exim 4.69) (envelope-from ) id 1QMykr-0004KA-3e for pgsql-sql@postgresql.org; Thu, 19 May 2011 09:38:50 +0100 Received: from eddie.ringways.co.uk ([10.1.1.115] helo=eddie.ringways.co.uk) by mail.ringways.co.uk with ESMTP id p9cjAi0u78916; Thu, 19 May 2011 09:38:45 +0100 From: Gary Stainburn Organization: Ringways Garages Ltd To: pgsql-sql@postgresql.org Subject: Re: SOLVED - foreign keys and lots of tables Date: Thu, 19 May 2011 09:38:47 +0100 User-Agent: KMail/1.9.10 References: <201105182010.19753.gary.stainburn@ringways.co.uk> <20110519010907.770c8c48@karnak.local> In-Reply-To: <20110519010907.770c8c48@karnak.local> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <201105190938.47857.gary.stainburn@ringways.co.uk> X-SpamTest-Envelope-From: gary.stainburn@ringways.co.uk X-SpamTest-Info: Profiles 20401 [Mar 31 2011] X-SpamTest-Method: none X-SpamTest-Rate: 0 X-SpamTest-Status: Not detected X-SpamTest-Status-Extended: not_detected X-SpamTest-Version: SMTP-Filter Version 3.0.0 [0285], KAS30/SDK/Release X-Anti-Virus: Kaspersky Mail Gateway, version: 5.6.28/RELEASE, bases: 20110331T110535 #5151353, check: 20110515 clean X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201105/115 X-Sequence-Number: 35399 Thanks Dave, I came up with the same answer after much googling. I managed to write a trigger to do this and it worked first time :D Then I realised I was going to have to write more triggers to handle the reverse side, i.e. deleting a coach_train record after a booking has been made. Thanks anyway. Gary On Thursday 19 May 2011 01:09:07 David W Noon wrote: > On Wed, 18 May 2011 20:10:19 +0100, Gary Stainburn wrote about [SQL] > > foreign keys and lots of tables: > >I have the following tables (individual seat allocation removed to > >make it simpler) > > Omitting details makes the problem more difficult to comprehend. > > [snip] > > >create table booking_seats ( > > b_id int4 not null references bookings(b_id), > > c_id int4, -- carriage ID > > c_seat varchar(10) -- seat label > >); > > > >The following ensures the seat exists on the coach. (not shown) > > > >alter table booking_seats add constraint seat_exists > > foreign key (c_id, c_seat) references coach_seats (c_id,c_seat); > > > >How would I ensure that the coach exists on the train. I would need to > >convert the b_id to a t_id using the bookings table and I don't know > >how. > > I think you will need to write a trigger procedure with something like > the following query inside it: > > IF NOT EXISTS(SELECT * FROM train_coaches AS tc > INNER JOIN bookings AS b ON b.t_id = tc.t_id > WHERE b.b_id = NEW.b_id AND tc.c_id = NEW.c_id)) THEN > -- Something is wrong. > > You cannot use a subquery in a CHECK constraint, so I think a trigger > will be the go. > > >To complicate things, when the initial booking is made, bot c_id and > >c_seat are NULL. Will this make any difference? > > If c_id is NULL you cannot check anything against it, so your data > integrity has just gone for a Burton. -- Gary Stainburn Group I.T. Manager Ringways Garages http://www.ringways.co.uk