Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL unique indexes and string case

If I create a PostgreSQL unique index on a field, is the comparison case-insensitive by default?

If not, is it possible to ask PostgreSQL to ignore string case?

like image 744
Simone Carletti Avatar asked Nov 08 '10 13:11

Simone Carletti


People also ask

Are unique indexes faster?

A unique index guarantees that the table won't have more than one row with the same value. It's advantageous to create unique indexes for two reasons: data integrity and performance. Lookups on a unique index are generally very fast.

Does unique constraint create index?

Yes, absolutely. A unique constraint creates a unique index.

What is the difference between unique index and unique constraint?

A unique index ensures that the values in the index key columns are unique. A unique constraint also guarantees that no duplicate values can be inserted into the column(s) on which the constraint is created. When a unique constraint is created a corresponding unique index is automatically created on the column(s).


1 Answers

PostgreSQL is case sensitive. To do what you want create a function index. So say

CREATE UNIQUE INDEX test_upper_idx ON mytable (UPPER(myfield)); 

That way when you use UPPER(myfield) in your query the index will be used.

See this link

like image 134
Kuberchaun Avatar answered Sep 22 '22 18:09

Kuberchaun