Re: UNIQUE KEY with null value...

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: Diego <diego(at)unimedijui(dot)com(dot)br>
Cc: pgsql-admin(at)postgresql(dot)org
Subject: Re: UNIQUE KEY with null value...
Date: 2005-11-01 17:32:02
Message-ID: 20051101092644.Y59282@megazone.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

On Tue, 1 Nov 2005, Diego wrote:

> Hi,
> I´m a brazilian programmer!
>
> I have a question about Postgres UNIQUE KEY. Look:
>
> (Step 1)
> CREATE TABLE test (
> id integer not null primary key,
> id_uni integer not null,
> cod integer,
> name varchar(40),
> constraint test_uk unique(id_uni, cod)
> )
>
> (Step 2)
> insert into test (id, id_uni, cod, name)
> values (1, 1, null, 'A')
>
> (Step 3)
> insert into test (id, id_uni, cod, name)
> values (2, 1, null, 'B') =========> [Have the same UNIQUE KEY]
>
> Why postgresql don´t break insertion in this case????

AFAICS, SQL (at least 92) says that those do not violate UNIQUE.

The unique predicate on which the UNIQUE constraint is defined says:

2) If there are no two rows in T such that the value of each column
in one row is non-null and is equal to the value of the cor-
responding column in the other row according to Subclause 8.2,
"<comparison predicate>", then the result of the <unique predi-
cate> is true; otherwise, the result of the <unique predicate>
is false.

The unique constraint for the above is effectively
UNIQUE (SELECT id_uni, col FROM test)
The two rows in question look like
(1,null) and (1,null).

There are no rows in that would make the predicate return false AFAICS.

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Tomeh, Husam 2005-11-01 17:38:48 Re: Pre-allocate space in advance
Previous Message Eric Faulhaber 2005-11-01 17:28:18 Re: UNIQUE KEY with null value...