Is there anyway that we can set a constraint in database table level to have upper or lower case values for certain columns? When we create a table, we can set NOT NULL to avoid having null values on a column. Same way, can we do that for either uppercase or lower case?
You can do that using a check constraint:
create table foo
(
only_lower varchar(20) not null check (lower(only_lower) = only_lower),
only_upper varchar(20) not null check (upper(only_upper) = only_upper)
);
-
It worked. Do we need NOT NULL statement if we accept either null or upper only? – user3123690 Oct 27 '15 at 18:39
-
@user3123690 If you want to allow
null
values, then of course you don't need thenot null
constraint. – a_horse_with_no_name Oct 27 '15 at 19:13