Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 84D6E635CC7 for ; Thu, 30 Jul 2009 12:35:11 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 01903-02-6 for ; Thu, 30 Jul 2009 15:35:00 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from QMTA07.westchester.pa.mail.comcast.net (qmta07.westchester.pa.mail.comcast.net [76.96.62.64]) by mail.postgresql.org (Postfix) with ESMTP id 497A563586F for ; Thu, 30 Jul 2009 12:34:01 -0300 (ADT) Received: from OMTA22.westchester.pa.mail.comcast.net ([76.96.62.73]) by QMTA07.westchester.pa.mail.comcast.net with comcast id NFRy1c0061ap0As57Fa1N3; Thu, 30 Jul 2009 15:34:01 +0000 Received: from [10.0.1.2] ([71.225.82.239]) by OMTA22.westchester.pa.mail.comcast.net with comcast id NFbv1c00F59opWn3iFbvYD; Thu, 30 Jul 2009 15:35:55 +0000 Message-Id: <5E69BD4A-C7CC-4821-A51C-4590449F7E10@comcast.net> From: Daryl Richter To: PgSQL-sql Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Mime-Version: 1.0 (Apple Message framework v935.3) Subject: Re: Foreign keys and fixed values... Date: Thu, 30 Jul 2009 11:33:59 -0400 References: X-Mailer: Apple Mail (2.935.3) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=-10 required=5 tests=none X-Spam-Level: X-Archive-Number: 200907/154 X-Sequence-Number: 32895 Hi- On Jul 29, 2009, at 4:16 PM, Kjell Rune Skaaraas wrote: > > Hello, > > I'm having a problem using foreign keys, or at least in a way i find > good. Basicly I have a table I wish to use as a foreign key with > lookup "id" and "code", like: > > 1,a > 1,b > 1,c > 2,a > 2,d > > I have of course an UNIQUE on (id,code). However, in the table I > want the key to apply to, I only have one column that I want to > restrict to one lookup. Basicly I want to do something like: > > ALTER TABLE data ADD FOREIGN KEY ( 2, col ) REFERENCES lookups ( id, > code ) > > In this case 2 is a literal integer, not a column reference and the > result should be that a and d is legal - but this command isn't legal. > > Options: > 1) Add another column to my data table like: > ALTER TABLE data ADD COLUMN dummy INTEGER DEFAULT 2 > ALTER TABLE data ADD FOREIGN KEY ( dummy, col ) REFERENCES lookups > ( id, code ) > This works, but creates a useless column in my data table that feels > very unclean. > > 2) Refactor the lookup table to have one lookup per table. However, > this also means I'll probably need to duplicate translation tables, > value mapping tables etc. and a bunch of queries would have to > dynamically alter table names. It seems more than a little messy. > #2 is the proper solution. A foreign key is a binding constraint between all the rows in the 2 tables. You are essentially trying to create a FK that applies to some rows and not to others. Or, alternately, a table which has conditional FKs to other tables based upon the data it contains. So, you have created a dependency between the data in the database and your schema which is a bad idea. Imagine, for example, after you have set this all up, the user comes to you and says, "Sorry, when I told you 2 maps to either a or d, I should have said 3". Now, instead of just issuing an update, you have to change your schema. Your other concerns regarding this option can almost certainly be addressed through proper schema design too. -- Daryl http://itsallsemantics.com "Everyone thinks of changing the world, but no one thinks of changing himself." - Leo Tolstoy