Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id C048F9FB6C0 for ; Tue, 23 Oct 2007 23:45:04 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 44414-09 for ; Tue, 23 Oct 2007 23:44:52 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 8B1D09FB5AB for ; Tue, 23 Oct 2007 23:44:58 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.1/8.14.1) with ESMTP id l9O2ieOr021716; Tue, 23 Oct 2007 22:44:40 -0400 (EDT) To: Paul Lambert cc: pgsql-sql Subject: Re: Quick question re foreign keys. In-reply-to: <471EA7D5.9040403@autoledgers.com.au> References: <471EA7D5.9040403@autoledgers.com.au> Comments: In-reply-to Paul Lambert message dated "Wed, 24 Oct 2007 10:03:01 +0800" Date: Tue, 23 Oct 2007 22:44:40 -0400 Message-ID: <21715.1193193880@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200710/95 X-Sequence-Number: 29467 Paul Lambert writes: > I have a table where one column references by foreign key a column from > another table. However, the column in this first table does not always > contain data which results in a not-null constraint violation when I > attempt an insert. > My question therefore is, is it possible to create a foreign key that is > conditional, i.e. only enforce the foreign key where the value in that > table is not null. If I'm understanding you correctly, the problem is not the foreign key, it's that you marked the column NOT NULL. A foreign key constraint by itself will allow a NULL in the referencing column to pass. You choose whether you want to allow that or not by separately applying a NOT NULL constraint or not. regards, tom lane