0

I'm working with PostgreSQL 9.3.

I have a table with a varchar column that I always want to be filled with lowercase strings.

I could use the Postgres lower function before saving my values, but is there instead a way to define this column as a lowercase column?

8
  • 1
    You could define the field as case-insensitive text (CITEXT) which isn't what you asked for but might give you what you want. Commented Mar 29, 2015 at 15:33
  • If I do so, add a unique constraint and try to save 'AaaA' then 'aaaa', will I get a unique exception? Commented Mar 29, 2015 at 15:37
  • Yes you will if you create the field as UNIQUE e.g. create table t (f citext unique); Commented Mar 29, 2015 at 15:50
  • Ok thanks, it fits my needs. Commented Mar 29, 2015 at 16:20
  • You can create a trigger that converts any input to lowercase Commented Mar 29, 2015 at 16:53

1 Answer 1

4

You can accomplish this with a simple check in the column:

create table lower_field ( field1 varchar check (field1 = lower(field1)) ); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.