Re: UNIQUE KEY with null value...

From: Eric Faulhaber <ecf(at)goldencode(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:28:18
Message-ID: 4367A5B2.9000508@goldencode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

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????
>
> Thanks
> Diego Ziquinatti
> Brazil

Diego,

In PostgreSQL, NULL is not considered a unique value, as it represents
an "unknown" value. From the docs regarding unique constraints:

"...null values are not considered equal in this comparison. That means
even in the presence of a unique constraint it is possible to store an
unlimited number of rows that contain a null value in at least one of
the constrained columns."

See:
http://www.postgresql.org/docs/8.0/interactive/ddl-constraints.html#AEN1996

Regards,
Eric Faulhaber

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Stephan Szabo 2005-11-01 17:32:02 Re: UNIQUE KEY with null value...
Previous Message Scott Marlowe 2005-11-01 17:25:06 Re: Pre-allocate space in advance