Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id DAC8E474E61 for ; Fri, 3 Jan 2003 08:09:55 -0500 (EST) Received: from email06.aon.at (WARSL401PIP3.highway.telekom.at [195.3.96.75]) by postgresql.org (Postfix) with SMTP id 825A5476375 for ; Fri, 3 Jan 2003 08:09:54 -0500 (EST) Received: (qmail 455850 invoked from network); 3 Jan 2003 13:09:56 -0000 Received: from m169p001.dipool.highway.telekom.at (HELO cantor) ([62.46.11.1]) (envelope-sender ) by qmail6rs.highway.telekom.at (qmail-ldap-1.03) with SMTP for ; 3 Jan 2003 13:09:56 -0000 From: Manfred Koizar To: brianw@mcsdallas.com Cc: pgsql-sql@postgresql.org Subject: Re: NULLL comparison in multiple column unique index Date: Fri, 03 Jan 2003 14:10:18 +0100 Message-ID: References: <200301021719520868.01EFA8DC@mail.mcsdallas.com> In-Reply-To: <200301021719520868.01EFA8DC@mail.mcsdallas.com> X-Mailer: Forte Agent 1.8/32.548 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/22 X-Sequence-Number: 10887 On Thu, 02 Jan 2003 17:19:52 -0600, "Brian Walker" wrote: >create table test1 (name varchar(64),num1 int,num2 int); >create unique index idx1 on test1(name,num1); >insert into idx1 values ('row3',null,22); >insert into idx1 values ('row3',null,23); > >This is allowed to happen. In Microsoft SQL the second insert will >fail because of the unique index. This looks like in MSSQL for the >unique index checks that NULL is equal to NULL so the unique check >fails. In PostgreSQL NULL != NULL so the unique check passes because >even though the name is the same the "num1" field is different.. This is just one more issue where Postgres is standard compliant and MS is not. Your problem has been discussed before: http://archives.postgresql.org/pgsql-novice/2002-09/msg00062.php >Does anybody have any ideas on how I can work around this difference? Also read the other messages of that thread; thus you should get an idea of possible solutions. You wrote: >In PostgreSQL NULL != NULL While accurate enough for the context you used it in, it is not completely exact. NULL = NULL is neither TRUE nor FALSE, it is UNKNOWN. The same holds for NULL != NULL. Try SELECT * FROM anytable WHERE NULL = NULL; SELECT * FROM anytable WHERE NULL != NULL; to illustrate this; you get 0 rows in both cases, even in MSSQL ;-). What's relevant here is that NULL = NULL doesn't evaluate to TRUE, which explains why rows containing NULL cannot violate a unique constraint. Servus Manfred