pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Stephan Szabo <sszabo@megazone.bigpanda.com>
To: Diego <diego@unimedijui.com.br>
Cc: pgsql-admin@postgresql.org
Subject: Re: UNIQUE KEY with null value...
Date: Tue, 1 Nov 2005 09:32:02 -0800 (PST)
Message-ID: <20051101092644.Y59282@megazone.bigpanda.com> (raw)
In-Reply-To: <4367AF54.6010909@unimedijui.com.br>
References: <4367AF54.6010909@unimedijui.com.br>

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.



view thread (5+ messages)  latest in thread

Message-ID: <20051101092644.Y59282@megazone.bigpanda.com>
Permalink:  ../20051101092644.Y59282@megazone.bigpanda.com/
Also on:    postgresql.org/message-id/20051101092644.Y59282@megazone.bigpanda.com

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: sszabo@megazone.bigpanda.com, diego@unimedijui.com.br
  Subject: Re: UNIQUE KEY with null value...
  In-Reply-To: <20051101092644.Y59282@megazone.bigpanda.com>
  References: <4367AF54.6010909@unimedijui.com.br> <20051101092644.Y59282@megazone.bigpanda.com>

* 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