Re: How can unique columns being case-insensitive be accomplished?

From: "Ross J(dot) Reedstrom" <reedstrm(at)rice(dot)edu>
To: pgsql-sql(at)postgresql(dot)org, J366rg <mail2holly(at)gmx(dot)de>
Subject: Re: How can unique columns being case-insensitive be accomplished?
Date: 2002-09-16 15:09:48
Message-ID: 20020916150948.GA797@rice.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Mon, Sep 16, 2002 at 09:38:46AM -0400, Tom Lane wrote:
> "Jrg" <mail2holly(at)gmx(dot)de> writes:
> > How can this problem be overcome? Any ideas?
>
> Create a unique index on lower(name).

I'm sure Jrg 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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Josh Berkus 2002-09-16 15:52:27 Re: Formatting zeroes
Previous Message Richard Huxton 2002-09-16 15:08:27 Re: Dublicates pairs in a table.