13

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"
Stephen Swensen
  • 22,107
  • 9
  • 81
  • 136

2 Answers2

12

I've managed to get it working by doing:

CREATE UNIQUE INDEX person_name_upper ON person(
    UPPER(first_name), UPPER(last_name));
priomsrb
  • 2,602
  • 3
  • 26
  • 34
  • using postgresql 9.4, this is the solution that worked for me.michael's accepted solution above threw a syntax exception, not liking the `lower` keyword inside the `unique` declaration. – liltitus27 Dec 20 '16 at 15:19
7

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
  • 71,784
  • 24
  • 131
  • 181