pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Leo Mannhart <Leo.Mannhart@beecom.ch>
To: pgsql-sql@postgresql.org
Subject: Re: uniqueness constraint with NULLs
Date: Mon, 29 Jun 2009 09:23:51 +0200
Message-ID: <1246260231.7296.2.camel@mars.lemaco.ch> (raw)
In-Reply-To: <4A486872.5060906@cs.anu.edu.au>
References: <4A485431.3080707@cs.anu.edu.au>
	<20090629062602.GB24737@a-kretschmer.de>
	<4A486872.5060906@cs.anu.edu.au>

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:

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));
CREATE INDEX
lem=# insert into bobtest (a, b) values (1, 4);
INSERT 0 1
lem=# insert into bobtest (a, b) values (1, 4);
ERROR:  duplicate key value violates unique constraint "idc_bobtest"
lem=# insert into bobtest (a, b, c) values (1, 4, null);
ERROR:  duplicate key value violates unique constraint "idc_bobtest"
lem=# 

> > 
> > Regards, Andreas
> 
> Beautiful!
> 
> Many thanks,
> 
> Bob Edwards.
> 




view thread (10+ messages)  latest in thread

Message-ID: <1246260231.7296.2.camel@mars.lemaco.ch>
Permalink:  ../1246260231.7296.2.camel@mars.lemaco.ch/
Also on:    postgresql.org/message-id/1246260231.7296.2.camel@mars.lemaco.ch

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: Leo.Mannhart@beecom.ch
  Subject: Re: uniqueness constraint with NULLs
  In-Reply-To: <1246260231.7296.2.camel@mars.lemaco.ch>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox