views:

21

answers:

1

In Oracle 10g, how do I add a unique case-insensitive constraint on two varchar fields? For example, given the following records already in the table:

"Stephen", "Swensen"
"John", "Smith"

The following inserts would be invalid:

"stephen", "Swensen"
"John", "smith"
"stephen", "swensen"

But the following inserts would be valid:

"Stephen", "Smith"
"John", "Swensen"
+1  A: 

Assuming your table is called person, and the first and last name columns are called first_name and last_name, add this unique constraint:

ALTER TABLE person ADD CONSTRAINT person_name_unique
    UNIQUE(LOWER(first_name),LOWER(last_name));

Let me know if I understood your question correctly and made the correct assumptions about your table layout.

Michael Goldshteyn
I don't think `LOWER` belongs in a constraint like that. It's better to specify a collation.
Gabe
You got it, thanks! I also found this, which works as well: create unique index UK_U_FIRSTNAME_LASTNAME on PERSON (LOWER(FIRST_NAME), LOWER(LAST_NAME));
Stephen Swensen