X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id C41F452A67 for ; Mon, 28 Feb 2005 20:25:26 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 43008-01 for ; Mon, 28 Feb 2005 20:25:16 +0000 (GMT) Received: from mm02snlnto.son.sandia.gov (mm02snlnto.sandia.gov [132.175.109.21]) by svr1.postgresql.org (Postfix) with ESMTP id 395CD56616 for ; Mon, 28 Feb 2005 20:25:15 +0000 (GMT) Received: from 132.175.109.4 by MM01SNLNTO.son.sandia.gov with ESMTP ( Tumbleweed MMS SMTP Relay 01 (MMS v5.6.3)); Mon, 28 Feb 2005 13:25:03 -0700 X-Server-Uuid: 2C1074A8-2B28-4DE3-9F7D-FF40AE090BA2 Received: from ES22SNLNT.srn.sandia.gov (ec03snlnt.sandia.gov [134.253.164.158] (may be forged)) by mailgate2.sandia.gov ( 8.12.11/8.12.11) with ESMTP id j1SKP22W007704 for ; Mon, 28 Feb 2005 13:25:02 -0700 (MST) Received: from 134.252.13.164 ([134.252.13.164]) by ES22SNLNT.srn.sandia.gov ([134.253.164.160]) with Microsoft Exchange Server HTTP-DAV ; Mon, 28 Feb 2005 20:25:02 +0000 Received: from boomerang by ES22SNLNT.srn.sandia.gov; 28 Feb 2005 20:23: 35 +0000 Subject: Re: table constraints From: "Casey T. Deccio" To: pgsql-sql@postgresql.org In-Reply-To: <20050228202016.GA32034@wolff.to> References: <1109618910.32166.0.camel@boomerang.ran.sandia.gov> <20050228202016.GA32034@wolff.to> Organization: Sandia National Laboratories Date: Mon, 28 Feb 2005 12:23:35 -0800 Message-ID: <1109622215.32226.11.camel@boomerang.ran.sandia.gov> MIME-Version: 1.0 X-Mailer: Evolution 2.0.3 X-PMX-Version: 4.7.0.111621, Antispam-Engine: 2.0.1.0, Antispam-Data: 2005.2.28.10 X-WSS-ID: 6E3DA19518W1226907-01-01 Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.214 tagged_above=0 required=5 tests=AWL, RCVD_NUMERIC_HELO X-Spam-Level: * X-Archive-Number: 200502/368 X-Sequence-Number: 20478 On Mon, 2005-02-28 at 13:20 -0700, Bruno Wolff III wrote: > On Mon, Feb 28, 2005 at 11:28:30 -0800, > "Casey T. Deccio" wrote: > > > > In this case each bldg has an owner associated to it, and each > animal > > lives in some bldg. Each owner has exactly one own favorite animal > out > > of all the bldgs owned by him. So the constraint added to each zoo > row > > is that the boolean field 'favorite' is true for exactly once for > each > > group of animals in the zoo that have a common owner. > > One way to do this kind of thing is to add a UNIQUE key to the table > that holds the animal - table relation of the animal and table and add > a UNIQUE key for the building table of the building and owner. > Then in the owner table add favorite animal building and favorite > animal columns with the property NOT NULL. Then add two deferable > foreign keys (owner, favorite animal building) to the building table > and (favorite animal building, favorite animal) to the animal - > building > table. When modifying data you want to change favorites before > changing > the underlying tables (where a building or animal change affects > someones > favorites). > Thanks for the input. This may work in the example I've given. However, the example I provided was contrived and was used merely to show the discrepancy that I'm finding with using the function as a constraint. In the larger example, things are a bit more complex, and I've found using such a constraint a better fit for now for the problem I'm working with. That said, I'd like to know why the constraint I provided isn't working with the corresponding example. > P.S. in your example you used varchar(50) for the text strings. Using > 'text' > is better unless there is a business rule limiting the name lengths to > 50 > or you expect to port the application to another rdbms. Thanks for the tip. Good point. I've never really known when to use text over varchar, so though I began using varchar for potential later porting to other rdbms, and since then it's just been tradition...:) Casey