How can unique columns being case-insensitive be accomplished?

From: "Jrg" <mail2holly(at)gmx(dot)de>
To: pgsql-sql(at)postgresql(dot)org
Subject: How can unique columns being case-insensitive be accomplished?
Date: 2002-09-13 09:07:57
Message-ID: alsac7$30gc$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi folks,

I got an problem that's being on my mind for a couple of days. I've thought
about different solution methods, but everything seems to be worse than the
problem itself.

I got a very simple lookup table with only one column 'name'. Its data type
is VarChar(50) and it's unique, so there can't be any duplicate entries.
Problems is that unique is case-sensitive. Germans tend to capitalize the
first letter of every word. But if anone doesn't do that, or if he
accidentially acps any letter, then this forms a new valid entry. So there
could be two entries representing the same thing, e.g 'Phenytoin' and
'phenytoin'.

How can this problem be overcome? Any ideas?

One solution I found is a before-trigger that checks whether the table
already contains a similar entry:

CREATE FUNCTION test_drug () RETURNS OPAQUE AS '
BEGIN
IF (SELECT name from drug WHERE lower(name) = lower(NEW.name)) ISNULL
THEN
RETURN NEW;
ELSE
RAISE EXCEPTION ''Lookup table cannot have duplicate entries'';
END IF;
END;
' LANGUAGE 'plpgsql';

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

This solution seems pretty complicated to me. Worst thing is that I have to
write a test function for every lookup table, since I don't know how to
place the lookup table's name in the SELECT-statement. Any ideas for that?

Greetings,
Joerg

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Albrecht Berger 2002-09-13 10:29:21 sql group by statement
Previous Message Rudi Starcevic 2002-09-13 02:49:39 DISTINCT ON