Re: unique key issue

From: Alban Hertroys <alban(at)magproductions(dot)nl>
To: Junkone <junkone1(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: unique key issue
Date: 2006-09-19 13:44:25
Message-ID: 450FF439.7000805@magproductions.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Junkone wrote:
> HI
> I have a table with a unique key constraint on col1,col2. The dattabase
> allowed an multiple inserts. here is how
>
> col1 col2
> 'abc' <empty space>
> 'abc' <empty space>

I suppose the values in col2 are NULL values?

You cannot compare NULL values. NULL == NULL evaluates to NULL (not true
or false), you have no way of knowing those two records are equal.
PostgreSQL (among others) assumes NULL values to always be different.

The meaning and interpretation of NULL is a frequent topic of discussion.

> I am not sure how this can happen

You can solve your problem by creating 2 unique constraints:
CREATE UNIQUE INDEX idx1 ON table (col1, col2) WHERE col2 IS NOT NULL;
CREATE UNIQUE INDEX idx2 ON table (col1) WHERE col2 IS NULL;

Regards,
--
Alban Hertroys
alban(at)magproductions(dot)nl

magproductions b.v.

T: ++31(0)534346874
F: ++31(0)534346876
M:
I: www.magproductions.nl
A: Postbus 416
7500 AK Enschede

// Integrate Your World //

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Harald Armin Massa 2006-09-19 13:45:25 Limits of arrays
Previous Message Geoffrey 2006-09-19 13:43:45 Re: vista