Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are UNIQUE indices case sensitive in MySQL?

Are indices (indexes) defined as UNIQUE case sensitive in MySQL?

like image 209
nickf Avatar asked Jan 21 '09 01:01

nickf


People also ask

Is MySQL index case sensitive?

The default collations used by SQL Server and MySQL do not distinguish between upper and lower case letters—they are case-insensitive by default.

Is unique key case sensitive?

MySQL is case insensitive by default and normally it is more than enough. However one of my recent projects required a case sensitive varchar column with unique index.

Are MySQL data types case sensitive?

Table names are stored in lowercase on disk and name comparisons are not case-sensitive. MySQL converts all table names to lowercase on storage and lookup. This behavior also applies to database names and table aliases.

What is unique index in MySQL?

In MySQL, UNIQUE INDEX is used to define multiple non-duplicate columns at once. While PRIMARY KEY constraint also assures non-duplicate values in a column, only one PRIMARY KEY can be defined per table. So for scenarios where multiple columns are to be made distinct, UNIQUE INDEX is used.


2 Answers

It depends on the collation of the field - if it's ci (case insensitive) or cs (case sensitive). The unique index would apply accordingly.

like image 158
Eran Galperin Avatar answered Oct 05 '22 14:10

Eran Galperin


You can make a column case-sensitive by using this syntaxis. the unique index also will be case-sensitive.

ALTER TABLE tbl_name MODIFY
col_name column_definition
[CHARACTER SET charset_name]
[COLLATE collation_name]

Example:

ALTER TABLE `tablename` MODIFY `column` VARCHAR(100)  CHARACTER SET utf8 COLLATE utf8_bin; 

Note: utf8_bin compares strings by the binary value of each character in the string.

Tested on Msql 5.5.X

like image 29
Hemerson Varela Avatar answered Oct 05 '22 16:10

Hemerson Varela