Are UNIQUE indices case sensitive in MySQL?

MysqlIndexingKeyUniqueCase Sensitive

Mysql Problem Overview


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

Mysql Solutions


Solution 1 - Mysql

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

Solution 2 - Mysql

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnickfView Question on Stackoverflow
Solution 1 - MysqlEran GalperinView Answer on Stackoverflow
Solution 2 - MysqlHemerson VarelaView Answer on Stackoverflow