Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 1AC463405C8 for ; Wed, 22 Feb 2012 08:19:27 -0400 (AST) Received: from hub.ringways.co.uk ([77.86.27.30] helo=mail.ringways.co.uk) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1S0BAM-0005yO-Re for pgsql-sql@postgresql.org; Wed, 22 Feb 2012 12:19:27 +0000 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 1S0BA5-0006tz-CP for pgsql-sql@postgresql.org; Wed, 22 Feb 2012 12:19:10 +0000 Received: from eddie.ringways.co.uk ([10.1.1.115] helo=eddie.ringways.co.uk) by mail.ringways.co.uk with ESMTP id qCJ61uCm89970; Wed, 22 Feb 2012 12:19:06 +0000 From: Gary Stainburn Organization: Ringways Garages Ltd To: pgsql-sql@postgresql.org Subject: Re: Another constant in foreign key problem. Date: Wed, 22 Feb 2012 12:19:09 +0000 User-Agent: KMail/1.9.10 References: <201202221035.11963.gary.stainburn@ringways.co.uk> <4F44D9D9.3060201@pedal.me.uk> In-Reply-To: <4F44D9D9.3060201@pedal.me.uk> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <201202221219.09214.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: 20120222 clean X-Spam-Score: -51.6 (---------------------------------------------------) X-Spam-Report: Spam detection software, running on the system "ollie.ringways.co.uk", has identified this incoming email as possible spam. The original message has been attached to this so you can view it (if it isn't spam) or label similar future email. If you have any questions, see Gary Stainburn for details. Content preview: Thank you to you both. I was thinking that I wasn't going to be able to do this. As the use of these tables is purely to control the web interface to this database I have decided control this via the application rather than within the database. It will require less coding and is simpler. [...] Content analysis details: (-51.6 points, 15.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -50 ALL_TRUSTED Passed through trusted hosts only via SMTP -2.6 BAYES_00 BODY: Bayesian spam probability is 0 to 1% [score: 0.0000] 1.0 RING_SAFE RING_SAFE X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201202/71 X-Sequence-Number: 36364 Thank you to you both. I was thinking that I wasn't going to be able to do this. As the use of these tables is purely to control the web interface to this database I have decided control this via the application rather than within the database. It will require less coding and is simpler. On Wednesday 22 February 2012 12:04:41 Philip Couling wrote: > Hi Gary > > In short you can't have a foreign key here because foreign keys are just > that: a *key* from another table. fl_level is not a key, it is not > unique and requires fl_f_id to be unique. > > If you want a foreign key between these two tables then you must add the > facility id to the document library and use a composite key (fl_f_id , > fl_level) as the foreign key. This may be advantageous as the same > structure could be re-used across other facilities (even though it would > not be applicable to all). > > > > There are a couple of alternatives to this. One is to add a separate > column to facility_levels. The sole purpose of this column would be to > proved a unique key on every row in the table. Foreign keys (such as on > library_document_user_level) would reference this and not fl_level. > There would be no constraint here to prevent a row in > library_document_user_level from referencing the wrong facility. > > > Another possible alternative which is very PostgreSQL specific is to use > inheritance. Create a child table storing only facility_levels for > fl_f_id 22. The foreign key would reference the child table and not > facility_levels. Everything in the child table would also exist in > facility_levels. As this is so specific to PostgreSQL and is not hugely > common as a technique, read this as a *possible* solution not a > recommended one. > > Hope this helps > -- Gary Stainburn Group I.T. Manager Ringways Garages http://www.ringways.co.uk