Re: Case insensitive primary keys - Mailing list pgsql-general

From Richard Huxton
Subject Re: Case insensitive primary keys
Date
Msg-id 3AADD732.FF59D59F@archonet.com
Whole thread Raw
In response to Case insensitive primary keys  ("Jeff Schnitzer" <jeff@infohazard.org>)
List pgsql-general
Jeff Schnitzer wrote:
>
> Hello!  I've got a question:
>
> I'm trying to create a system with a string (varchar) primary key which
> is *not* case sensitive.  Is there any way I can create a primary key
> column which automatically takes care of this?

you can try something like:

CREATE UNIQUE INDEX foo_idx ON tbl (lower(foo));

That will prevent entries duplicates that differ only in case.

> I'm coming at this problem from a java app server, so my hands are
> somewhat bound regarding the queries used to insert and search the
> table.  There are solutions, but the ideal (for me) would be for the
> database to ignore the case of my key column.  Is this possible?

If you can't change LIKE to ILIKE or foo='x' to lower(foo) = lower('x')
you'll have problems with comparisons.

The only other thing(s) I can suggest are:

1. add a rule/trigger to inserts/updates/selects to do the lower() stuff
for you.
2. you _could_ write your own type, but this is last resort really.

- Richard Huxton

pgsql-general by date:

Previous
From: Richard Huxton
Date:
Subject: Re: help with simple rule
Next
From:
Date:
Subject: Re: Data type for storing images?