Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an index in PostgreSQL based on lowercase only?

How would I set up an index based on lower case only?

Even though the actual field contains both upper and lower case letters.

Also, can I run a query and have only the lower case index value returned?

like image 531
Paul Townsend Avatar asked Oct 20 '10 16:10

Paul Townsend


People also ask

How do you create a unique index?

Right-click the table on which you want to create a unique index and select Design. On the Table Designer menu, select Indexes/Keys. In the Indexes/Keys dialog box, click Add. Select the new index in the Selected Primary/Unique Key or Index text box.

Can we alter index in PostgreSQL?

To change the tablespace of an index, you must own the index and have CREATE privilege on the new tablespace. All indexes in the current database in a tablespace can be moved by using the ALL IN TABLESPACE form, which will lock all indexes to be moved and then move each one.


2 Answers

You can create the index and transform the field to upper- or lower-case. Then when you do your queries, you can do the same transform and it'll do the right thing.

So:

CREATE UNIQUE INDEX lower_case_username ON users ((lower(username))); 

Then query for the same thing:

SELECT username FROM users WHERE lower(username) = 'bob'; 
like image 83
Pointy Avatar answered Oct 09 '22 22:10

Pointy


According to the docs you can do this:

CREATE UNIQUE INDEX lower_title_idx ON films ((lower(title))); 
like image 23
mezzie Avatar answered Oct 10 '22 00:10

mezzie