Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 6E50E8AE547 for ; Fri, 1 Jun 2012 00:28:28 -0300 (ADT) Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SaIXJ-0001Tm-Ol for pgsql-bugs@postgresql.org; Fri, 01 Jun 2012 03:28:27 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id q513SBts002871; Thu, 31 May 2012 23:28:11 -0400 (EDT) To: jose.soares@sferacarta.com cc: pgsql-bugs@postgresql.org Subject: Re: BUG #6669: unique index w/ multiple columns and NULLs In-reply-to: References: Comments: In-reply-to jose.soares@sferacarta.com message dated "Thu, 31 May 2012 05:25:24 -0000" Date: Thu, 31 May 2012 23:28:11 -0400 Message-ID: <2870.1338521291@sss.pgh.pa.us> From: Tom Lane X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201206/1 X-Sequence-Number: 33228 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, "", then the result of the is true; otherwise, the result of the is false. (SQL92 section 8.9 ) 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