Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a lowercase constraint to a column in sql

I've searched the web but found only how to update columns to lowercase. Is there a way to put a constraint in a column so that it accepts only lowercase and gets an error when we try to insert something in uppercase.

The table is the following:

create table Student
(
  ID int,
  Name varchar(50),
  Email varchar(50)
);
like image 571
Aeonify Avatar asked Aug 19 '15 12:08

Aeonify


People also ask

Is check constraint case sensitive in SQL?

SQL keywords are by default set to case insensitive that means that the keywords are allowed to be used in lower or upper case. The names of the tables and columns specification are set to case insensitive on the SQL database server, however, it can be enabled and disabled by configuring the settings in SQL.

How do I get just the lowercase letters in SQL?

Whenever you want some text data from your SQL database to be displayed in lowercase, use the LOWER() function. This function takes as an argument a string or the name of a column whose text values are to be displayed in lowercase.

How do you add constraints to an existing column?

Use the ADD CONSTRAINT clause to specify a primary key, foreign key, referential, unique, or check constraint on a new or existing column or on a set of columns. This syntax fragment is part of the ALTER TABLE statement. Notes: For NULL and NOT NULL constraints, use instead the MODIFY Clause.

How do you lowercase a column in SQL?

Use the SQL LOWER() function if you want to convert a string column to lowercase. This function takes only one argument: the column whose values you want to lowercase. This function is a good choice if your database is case sensitive and you want to select only records matching a particular string.


2 Answers

You can use a check constraint with the binary_checksum function for this:

alter table student add check (binary_checksum(email) = binary_checksum(lower(email)));

You can also use multiple conditions in the check, so you could also check that the email-address contains a @ character and so on, although processing demand would increase:

check (binary_checksum(email) = binary_checksum(lower(email)) and charindex('@', email) > 0);

It would probably be better to check the input in the client side application where you insert it, or use a trigger or stored procedure to handle inserts and force data to be lower case.

To quote the SQL Server manual:

CHECKSUM and BINARY_CHECKSUM return different values for the string data types, where locale can cause strings with different representation to compare equal. The string data types are char, varchar, nchar, nvarchar, or sql_variant (if the base type of sql_variant is a string data type). For example, the BINARY_CHECKSUM values for the strings "McCavity" and "Mccavity" are different. In contrast, in a case-insensitive server, CHECKSUM returns the same checksum values for those strings. CHECKSUM values should not be compared with BINARY_CHECKSUM values.

like image 64
jpw Avatar answered Oct 11 '22 10:10

jpw


Create a check constraint that compares the actual value with the lower value:

create table Student
(
  ID int,
  Name varchar(50),
  Email varchar(50),
  constraint check_lowercase_email check (lower(email) = email)
);

The above however will not work if your default collation is case insensitive in that case you need to do something like this:

create table Student
(
  ID int,
  Name varchar(50),
  Email varchar(50),
  constraint check_lowercase_email check (lower(email) = email COLLATE Latin1_General_CS_AI )
);

Which collation you use depends on what the default collation of your database is, but usually there is a CS version available which stands for "case insensitive". Latin1_General_BIN would probably work just as well for emails.

like image 7
a_horse_with_no_name Avatar answered Oct 11 '22 11:10

a_horse_with_no_name