57

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

Hemerson Varela
  • 24,034
  • 16
  • 68
  • 69
nickf
  • 537,072
  • 198
  • 649
  • 721

2 Answers2

94

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

Yves M.
  • 29,855
  • 23
  • 108
  • 144
Eran Galperin
  • 86,251
  • 24
  • 115
  • 132
27

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

Hemerson Varela
  • 24,034
  • 16
  • 68
  • 69