X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 7EB0DD1B860 for ; Thu, 26 Feb 2004 14:39:44 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 48159-07 for ; Thu, 26 Feb 2004 10:39:40 -0400 (AST) Received: from wolff.to (wolff.to [66.93.249.74]) by svr1.postgresql.org (Postfix) with SMTP id 9E6C0D1B4AA for ; Thu, 26 Feb 2004 10:39:35 -0400 (AST) Received: (qmail 7437 invoked by uid 500); 26 Feb 2004 14:41:59 -0000 Date: Thu, 26 Feb 2004 08:41:59 -0600 From: Bruno Wolff III To: Michiel Lange Cc: pgsql-admin@postgresql.org Subject: Re: Help with triggers Message-ID: <20040226144159.GA29397@wolff.to> Mail-Followup-To: Michiel Lange , pgsql-admin@postgresql.org References: <403E012D.9070607@minas.demon.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <403E012D.9070607@minas.demon.nl> User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200402/356 X-Sequence-Number: 12530 On Thu, Feb 26, 2004 at 15:22:37 +0100, Michiel Lange wrote: > But here it is: > I have a database that is about how ocomputers and phones are connected > in a patch panel. > For that I have three tables: > patches(patch_id(serial, pkey), location(varchar, unique)); > computers(computer_id(serial, pkey), name(varchar, unique), patch > (unique,references patches.patch_id), ... and some more); > phones(number(int4, pkey), owner(varchar), patch (unique,references > patches.patch_id), ... and some more); > > (the database is not only about this information, there's quite some > more, but this narrows the problem down) > > The point is: > I can say that computers.patch is unique > I can also say that phones.patch is unique > > but there's also this situation that you cannot connect a computer and a > phone on the same patch_id, so if a patch is used for a computer it is > not possible to use that patch also for the phones. (and vise versa) > As far as I know there is no constraint that enforces this, so I will > have to write a trigger... You can enforce this without having to write a trigger. You add a patch type to all three tables constrained in patches to be one of two types and in the other tables to be the type matching that table. Then you make the foreign keys into patches to be patch_id combined with the patch type. This prevents both phones and computers from having the same patch id.