Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 3C0ADB5DC0B for ; Wed, 18 May 2011 21:09:43 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 35345-01-6 for ; Thu, 19 May 2011 00:09:36 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from karnak.local (cpc2-lutn10-2-0-cust603.9-3.cable.virginmedia.com [81.97.90.92]) by mail.postgresql.org (Postfix) with ESMTP id 5283EB5DD3B for ; Wed, 18 May 2011 21:09:21 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by karnak.local (Postfix) with ESMTP id C655C3003 for ; Thu, 19 May 2011 01:09:19 +0100 (BST) X-Virus-Scanned: by amavisd-new using ClamAV at karnak.local Received: from karnak.local ([127.0.0.1]) by localhost (karnak.local [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id t8SUMCKS0j3H for ; Thu, 19 May 2011 01:09:18 +0100 (BST) Received: from karnak.local (localhost [127.0.0.1]) by karnak.local (Postfix) with ESMTP id 0A3633002 for ; Thu, 19 May 2011 01:09:18 +0100 (BST) Date: Thu, 19 May 2011 01:09:07 +0100 From: David W Noon To: pgsql-sql@postgresql.org Subject: Re: foreign keys and lots of tables Message-ID: <20110519010907.770c8c48@karnak.local> In-Reply-To: <201105182010.19753.gary.stainburn@ringways.co.uk> References: <201105182010.19753.gary.stainburn@ringways.co.uk> Organization: Luton Operatic Society X-Mailer: Claws Mail 3.7.8 (GTK+ 2.22.1; i686-pc-linux-gnu) Mime-Version: 1.0 Content-Type: multipart/signed; micalg=PGP-SHA1; boundary="Sig_/lk0mwarzN8MczEQte8+7rDx"; protocol="application/pgp-signature" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=2.418 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RCVD_IN_PBL=3.335, RCVD_IN_SORBS_DUL=0.001, RDNS_DYNAMIC=0.982 X-Spam-Level: ** X-Archive-Number: 201105/111 X-Sequence-Number: 35395 --Sig_/lk0mwarzN8MczEQte8+7rDx Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable 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 =3D tc.t_id WHERE b.b_id =3D NEW.b_id AND tc.c_id =3D 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. --=20 Regards, Dave [RLU #314465] *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* dwnoon@ntlworld.com (David W Noon) *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* --Sig_/lk0mwarzN8MczEQte8+7rDx Content-Type: application/pgp-signature; name=signature.asc Content-Disposition: attachment; filename=signature.asc -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.17 (GNU/Linux) iEYEARECAAYFAk3UX60ACgkQc9/LpQ70v4+uRgCfdKuNKV5emFhUY6S/yETnqo9w tZQAmwdpr+eZa5u4PVU1EvM1zRsvnmrh =aPq2 -----END PGP SIGNATURE----- --Sig_/lk0mwarzN8MczEQte8+7rDx--