Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id A240EB5D944 for ; Wed, 18 May 2011 17:21:28 -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 61106-09 for ; Wed, 18 May 2011 20:21:21 +0000 (UTC) X-Greylist: delayed 01:10:43.132229 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 E3C1FB5DBEC for ; Wed, 18 May 2011 17:21:20 -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 1QMm8b-0006Lb-Em for pgsql-sql@postgresql.org; Wed, 18 May 2011 20:10:30 +0100 Received: from eddie.ringways.co.uk ([10.1.1.115] helo=eddie.ringways.co.uk) by mail.ringways.co.uk with ESMTP id pKAFAiHW55837; Wed, 18 May 2011 20:10:15 +0100 From: Gary Stainburn Organization: Ringways Garages Ltd To: pgsql-sql@postgresql.org Subject: foreign keys and lots of tables Date: Wed, 18 May 2011 20:10:19 +0100 User-Agent: KMail/1.9.10 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <201105182010.19753.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=-0.51 tagged_above=-5 required=5 tests=BAYES_05=-0.5, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201105/102 X-Sequence-Number: 35386 I have the following tables (individual seat allocation removed to make it simpler) create table coaches ( -- carriages c_id serial primary key, c_name varchar(20) not null ); create table trains ( -- one for each train t_id serial primary key ); create table train_coaches ( -- which carriages are on what trains t_id int4 not null references trains(t_id), c_id int4 not null references coaches(c_id) ); I now want to create bookings and allocate seats, but the seat must exist on the coach_seats table *AND* only for a carriage included in the train, i.e. an entry in train_coaches. create table bookings ( b_id serial primary key, t_id int4 not null references trains(t_id) ); 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. To complicate things, when the initial booking is made, bot c_id and c_seat are NULL. Will this make any difference? Gary -- Gary Stainburn Group I.T. Manager Ringways Garages http://www.ringways.co.uk