Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id B762C6339AC for ; Mon, 29 Jun 2009 03:18:10 -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 91832-09 for ; Mon, 29 Jun 2009 03:18:05 -0300 (ADT) X-Greylist: delayed 00:35:48.318041 by SQLgrey-1.7.6 Received: from anumail8.anu.edu.au (anumail8.anu.edu.au [130.56.64.134]) by mail.postgresql.org (Postfix) with ESMTP id 2C9F363396E for ; Mon, 29 Jun 2009 03:18:06 -0300 (ADT) Received: from mailout1.anu.edu.au (mailout1.anu.edu.au [130.56.64.232]) by anumail8.anu.edu.au (8.14.3/8.14.3) with ESMTP id n5T5gEWr012194 for ; Mon, 29 Jun 2009 15:42:15 +1000 (EST) Received: from mailout1.anu.edu.au (localhost.localdomain [127.0.0.1]) by localhost (Postfix) with SMTP id A421914A09C0 for ; Mon, 29 Jun 2009 15:42:14 +1000 (EST) Received: from smtphost.anu.edu.au (ds1.anu.edu.au [130.56.64.53]) by mailout1.anu.edu.au (Postfix) with ESMTP id 75E3A14A09AD for ; Mon, 29 Jun 2009 15:42:14 +1000 (EST) Received: from dcsmail.anu.edu.au (dcsmail.anu.edu.au [150.203.164.16]) by smtphost.anu.edu.au (8.14.3/8.14.3) with ESMTP id n5T5gE3C003268 for ; Mon, 29 Jun 2009 15:42:14 +1000 (EST) Received: from littleblue.anu.edu.au ([150.203.160.52]) by dcsmail.anu.edu.au with esmtp (Exim 4.63) (envelope-from ) id 1ML9d3-0003YL-9a for pgsql-sql@postgresql.org; Mon, 29 Jun 2009 15:42:14 +1000 Message-ID: <4A485431.3080707@cs.anu.edu.au> Date: Mon, 29 Jun 2009 15:42:09 +1000 From: Robert Edwards User-Agent: Thunderbird 2.0.0.18 (X11/20081125) MIME-Version: 1.0 To: pgsql-sql@postgresql.org Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-SA-Exim-Connect-IP: 150.203.160.52 X-SA-Exim-Mail-From: bob@cs.anu.edu.au Subject: uniqueness constraint with NULLs X-SA-Exim-Version: 4.2.1 (built Tue, 09 Jan 2007 17:23:22 +0000) X-SA-Exim-Scanned: Yes (on dcsmail.anu.edu.au) X-PMX-Version: 5.5.6.374947, Antispam-Engine: 2.7.2.376379, Antispam-Data: 2009.6.29.53055 internal X-PMX-Outbound: anu.edu.au X-PMX-Info: from-anu X-PerlMx-Spam: Gauge=IIIIIIII, Probability=8%, Report=' BODY_SIZE_1500_1599 0, BODY_SIZE_2000_LESS 0, BODY_SIZE_5000_LESS 0, BODY_SIZE_7000_LESS 0, TO_NO_NAME 0, __CT 0, __CTE 0, __CT_TEXT_PLAIN 0, __HAS_MSGID 0, __MIME_TEXT_ONLY 0, __MIME_VERSION 0, __MOZILLA_MSGID 0, __SANE_MSGID 0, __TO_MALFORMED_2 0, __USER_AGENT 0' X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200906/112 X-Sequence-Number: 32717 I have a table with a uniqueness constraint on three columns: # \d bobtest Table "public.bobtest" Column | Type | Modifiers --------+---------+------------------------------------------------------ id | integer | not null default nextval('bobtest_id_seq'::regclass) a | integer | b | integer | c | integer | Indexes: "bobtest_id_key" UNIQUE, btree (id) "bobtest_unique" UNIQUE, btree (a, b, c) I can insert multiple rows with identical a and b when c is NULL: ... # insert into bobtest (a, b) values (1, 4); INSERT 0 1 # insert into bobtest (a, b, c) values (1, 4, NULL); INSERT 0 1 # select * from bobtest; id | a | b | c ----+---+---+--- 1 | 1 | 2 | 1 2 | 1 | 3 | 1 3 | 1 | 4 | 1 4 | 1 | 4 | 5 | 1 | 4 | 6 | 1 | 4 | (6 rows) Can anyone suggest a way that I can impose uniqueness on a and b when c is NULL? In the real app., c is a date field and I require it to be NULL for some rows. In these cases, I only want at most one row with identical a and b, but I can have identical a and b when c is a real date as long as that date is also unique for a given a and b. I'm guessing I'm going to need to use a function and that someone will yell at me for using NULLs to represent real data, but I thought I'd be brave and ask anyway, in case I am missing some other solution that doesn't involve the use of triggers etc. Cheers, Bob Edwards.