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

杀马特。学长 韩版系。学妹 提交于 2019-12-18 12:45:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!