pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: jose.soares@sferacarta.com
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #6669: unique index w/ multiple columns and NULLs
Date: Thu, 31 May 2012 23:28:11 -0400
Message-ID: <2870.1338521291@sss.pgh.pa.us> (raw)
In-Reply-To: <E1SZxsy-0001GO-JW@wrigleys.postgresql.org>
References: <E1SZxsy-0001GO-JW@wrigleys.postgresql.org>

jose.soares@sferacarta.com writes:
> I think I have found an error in pg or at least inconsistency, take a look
> at this.
> I created an unique index on two columns and pg let me enter repeated values
> as NULLs (unknown value),

This is entirely correct per SQL standard: unique constraints do not
reject duplicated rows that include nulls.  If you read the standard,
unique constraints are defined in terms of UNIQUE predicates, and a
UNIQUE predicate for a table T is defined thus:

         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.

(SQL92 section 8.9 <unique predicate>)

This is why a primary key constraint is defined as requiring both UNIQUE
and NOT NULL; you need that to ensure that there are indeed no two
indistinguishable rows.

(Mind you, I'm not here to defend *why* the standard is written that
way.  But that is what it says.)

> Oracle don't allows to insert two NULLs in such column.

Oracle is not exactly the most standards-compliant implementation
around.  They are well-known to be particularly wrong with respect to
NULLs behavior.

			regards, tom lane



view thread (11+ messages)  latest in thread

Message-ID: <2870.1338521291@sss.pgh.pa.us>
Permalink:  ../2870.1338521291@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/2870.1338521291@sss.pgh.pa.us

 · 

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-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, jose.soares@sferacarta.com
  Subject: Re: BUG #6669: unique index w/ multiple columns and NULLs
  In-Reply-To: <2870.1338521291@sss.pgh.pa.us>
  References: <E1SZxsy-0001GO-JW@wrigleys.postgresql.org> <2870.1338521291@sss.pgh.pa.us>

* 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