Re: unique constraint with a null column?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: CSN <cool_screen_name90001(at)yahoo(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: unique constraint with a null column?
Date: 2005-12-30 22:30:19
Message-ID: 15776.1135981819@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

CSN <cool_screen_name90001(at)yahoo(dot)com> writes:
> I have three columns, and one of them can be null. I'd
> like to create a unique constraint across all three
> columns and allow only one null value. e.g.

UNIQUE won't do that for you, but you could brute force it with a
unique constraint plus a check constraint along the lines of
(a notnull AND b notnull) OR
(a notnull AND c notnull) OR
(b notnull AND c notnull)

However, the real question is whether you think that "unique"
means "no more than one instance of x,y,NULL". If that's what
you mean then UNIQUE isn't going to enforce it ...

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message CSN 2005-12-30 22:38:48 Re: Delete / F/K error
Previous Message Jim Buttafuoco 2005-12-30 22:06:07 Re: unique constraint with a null column?