Constraint UNIQUE on a column not case sensitive

From: Daniel CAUNE <d(dot)caune(at)free(dot)fr>
To: pgsql-sql(at)postgresql(dot)org
Subject: Constraint UNIQUE on a column not case sensitive
Date: 2006-07-01 13:47:59
Message-ID: 0J1Q008ME8ZX7M90@VL-MO-MR003.ip.videotron.ca
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi,

I would like to find an efficient solution for adding/implementing a constraint UNIQUE on a VARCHAR column not case sensitive:

ALTER TABLE MyTable
ADD CONSTRAINT UNQ_MyTable_MyColumn
UNIQUE (lower(MyColumn)); -- invalid syntax

The idea is to have an index on that column, in a not case sensitive form, i.e. lower(MyColumn).

SELECT *
FROM MyTable
WHERE lower(MyColumn) = lower('...');

I don't know how to add such a constraint on MyTable except by defining a trigger on INSERT clause and checking whether lower(:NEW.MyColumn) has been already inserted in MyTable.

Is there better and more efficient way to do that?

Regards,

--
Daniel

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Michael Glaesemann 2006-07-01 14:01:08 Re: Constraint UNIQUE on a column not case sensitive
Previous Message Erik Jones 2006-07-01 12:54:04 Re: Alternative to Select in table check constraint