Re: uniqueness constraint with NULLs

From: Robert Edwards <bob(at)cs(dot)anu(dot)edu(dot)au>
To: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: uniqueness constraint with NULLs
Date: 2009-06-29 07:08:34
Message-ID: 4A486872.5060906@cs.anu.edu.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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=!#
>
>
> Regards, Andreas

Beautiful!

Many thanks,

Bob Edwards.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Leo Mannhart 2009-06-29 07:23:51 Re: uniqueness constraint with NULLs
Previous Message A. Kretschmer 2009-06-29 06:26:02 Re: uniqueness constraint with NULLs