Re: How can unique columns being case-insensitive be accomplished? - Mailing list pgsql-sql

From Ross J. Reedstrom
Subject Re: How can unique columns being case-insensitive be accomplished?
Date
Msg-id 20020916150948.GA797@rice.edu
Whole thread Raw
In response to Re: How can unique columns being case-insensitive be accomplished?  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-sql
On Mon, Sep 16, 2002 at 09:38:46AM -0400, Tom Lane wrote:
> "J�rg" <mail2holly@gmx.de> writes:
> > How can this problem be overcome? Any ideas?
> 
> Create a unique index on lower(name).

I'm sure J�rg can follow this tip, but for the archives, I'll include
the full syntax:

CREATE UNIQUE INDEX drug_lwr_idx ON drug ( lower(name) )

Another trick would be to use your trigger to lower() the inserted/updated
names, rather than only as a test, combined with a regular unique index:

CREATE FUNCTION lower_drug () RETURNS OPAQUE AS '   BEGIN       NEW.name = lower(NEW.name);RETURN NEW;   END;
' LANGUAGE 'plpgsql';

CREATE TRIGGER drug_insert BEFORE UPDATE OR INSERT ON drug FOR EACH ROW EXECUTE PROCEDURE lower_drug();

That keeps your data nice and clean and consistent, otherwise
the first time a MixedCaseDrugName goes in, it'll keep the correct
'mixedcasedrugname' from being inserted, later.  Be very sure that lower()
gives you exactly the output you want, however.

Ross


pgsql-sql by date:

Previous
From: Stephan Szabo
Date:
Subject: Re: does table names have a format and size
Next
From: Richard Huxton
Date:
Subject: Re: Dublicates pairs in a table.