Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UpZzQ-0002L7-0Q for pgsql-admin@arkaria.postgresql.org; Thu, 20 Jun 2013 08:13:08 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1UpZzP-0008Ov-9I for pgsql-admin@arkaria.postgresql.org; Thu, 20 Jun 2013 08:13:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UpZzO-0008Op-IB for pgsql-admin@postgresql.org; Thu, 20 Jun 2013 08:13:06 +0000 Received: from plane.gmane.org ([80.91.229.3]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UpZzA-0000Un-OG for pgsql-admin@postgresql.org; Thu, 20 Jun 2013 08:13:06 +0000 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1UpZz8-0003xS-EO for pgsql-admin@postgresql.org; Thu, 20 Jun 2013 10:12:50 +0200 Received: from ppp-88-217-24-232.dynamic.mnet-online.de ([88.217.24.232]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 20 Jun 2013 10:12:50 +0200 Received: from spam_eater by ppp-88-217-24-232.dynamic.mnet-online.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 20 Jun 2013 10:12:50 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-admin@postgresql.org From: Thomas Kellerer Subject: Re: Composite Unique Key - Doubt Date: Thu, 20 Jun 2013 10:12:47 +0200 Lines: 45 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: ppp-88-217-24-232.dynamic.mnet-online.de User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.21) Gecko/20090302 Thunderbird/2.0.0.21 Mnenhy/0.7.5.666 In-Reply-To: X-Pg-Spam-Score: -3.2 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-admin Precedence: bulk Sender: pgsql-admin-owner@postgresql.org Technical Doubts wrote on 20.06.2013 09:42: > technologies > ( > technologyid bigint, > status character(1), > implementeddate date > *CONSTRAINT technologies_uq UNIQUE (technologyid, status, implementeddate)* > ) > > entering data as > > insert into technologies (technologyid,status,implementeddate) > values > (123,'P',null), > (123,'P',null); > > 2 rows affected. > > table accepting duplicate values in spite of composite unique constraint.. > where I am doing wrong? > That's because of the null values. Any comparison with NULL yields "unknown" and in case of a constraint this means the constraint is not violated. Apparently you can not make that column NOT NULL (which would prevent this situation). But you could create a unique index on an expression that treats NULL as "some value", e.g: create table technologies ( technologyid bigint, status character(1), implementeddate date ); create unique index technologies_uq on technologies (technologyid, status, coalesce(implementeddate, date '1900-01-01')); A unique constraint is slightly different to a unique index (e.g. it cannot be the target of a foreign key) but it would server your purpose in this case - unlesse you have requirements you did not mention. -- Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-admin