问题
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?
回答1:
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';
回答2:
According to the docs you can do this:
CREATE UNIQUE INDEX lower_title_idx ON films ((lower(title)));
回答3:
CREATE UNIQUE INDEX my_index_name ON my_table (LOWER(my_field));
回答4:
You can also use this for wildcard searches:
CREATE INDEX IF NOT EXISTS foo_table_bar_field ON foo_table(lower(username))
Query like so:
SELECT * FROM foo_table WHERE lower(username) like 'bob%'
来源:https://stackoverflow.com/questions/3980050/how-do-i-create-an-index-in-postgresql-based-on-lowercase-only