Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id C4546633138 for ; Mon, 29 Jun 2009 05:21:02 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 15182-04 for ; Mon, 29 Jun 2009 05:21:01 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.186]) by mail.postgresql.org (Postfix) with ESMTP id 7449A632E50 for ; Mon, 29 Jun 2009 05:21:01 -0300 (ADT) Received: from webserv.wug.de ([194.25.154.138]) by mrelayeu.kundenserver.de (node=mreu2) with ESMTP (Nemesis) id 0MKv5w-1MLC6k3MEi-000KJ6; Mon, 29 Jun 2009 10:20:59 +0200 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1MLC6k-000228-00 for pgsql-sql@postgresql.org; Mon, 29 Jun 2009 10:20:58 +0200 Date: Mon, 29 Jun 2009 10:20:58 +0200 From: "A. Kretschmer" To: pgsql-sql@postgresql.org Subject: Re: uniqueness constraint with NULLs Message-ID: <20090629082058.GG24737@a-kretschmer.de> References: <4A485431.3080707@cs.anu.edu.au> <20090629062602.GB24737@a-kretschmer.de> <4A486872.5060906@cs.anu.edu.au> <1246260231.7296.2.camel@mars.lemaco.ch> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1246260231.7296.2.camel@mars.lemaco.ch> X-OS: Debian/GNU Linux - weil ich es mir Wert bin! User-Agent: Mutt/1.5.9i X-Provags-ID: V01U2FsdGVkX1+V57R351FaNnKInhRZC7BfHkc4G/3BoNvsS2f 4Hb0sSt0jLyusQD54vW45/CBgvQnOpR+fD5n6Cn0XVzI2sZ69m 6zyQGFYjSRRLkeQyl2oRg== X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200906/118 X-Sequence-Number: 32723 In response to Leo Mannhart : > On Mon, 2009-06-29 at 17:08 +1000, Robert Edwards wrote: > > A. Kretschmer wrote: > > > In response to Robert Edwards : > > >> Can anyone suggest a way that I can impose uniqueness on a and b when > > >> c is NULL? > > > > > > Sure, use a functional index: > > > > > > test=# create table bobtest (a int, b int, c int); > > > CREATE TABLE > > > test=*# create unique index idx_bobtest on > > > bobtest(a,b,coalesce(c::text,'NULL')); > > > CREATE INDEX > > > test=*# insert into bobtest (a, b) values (1, 4); > > > INSERT 0 1 > > > test=*# insert into bobtest (a, b, c) values (1, 4, NULL); > > > ERROR: duplicate key value violates unique constraint "idx_bobtest" > > > test=!# > > > > Why changing the datatype of coulumn "c"? Will this not probably disable > the use of the index? Why not only: Maybe, depends on the query. > > lem=# create table bobtest (a int, b int, c int); > CREATE TABLE > lem=# create unique index idc_bobtest on bobtest (a, b, coalesce(c, 1)); bad idea if 1 is a valid value for this column. Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net