Is there any way to make a UNIQUE index case insensitive in Mysql 5.1.x ?
Asked Answered
B

3

9

If so - What must change in this table ?

CREATE TABLE  contestants 
( 
  idContestants  int(10) unsigned NOT NULL AUTO_INCREMENT,
  idEvent        int(10) unsigned NOT NULL,
  ContestantName  varchar(50) DEFAULT NULL,
  PRIMARY KEY (idContestants),
  UNIQUE KEY Index_UniqueName (idEvent,ContestantName),
)
ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
Barnes answered 31/7, 2011 at 8:10 Comment(2)
What does 'case instinctive' mean?Gautier
Do you mean case insensitive?Blanka
G
23

If you mean case sensitive then:

ALTER TABLE `contestants` CHANGE `ContestantName` `ContestantName` VARCHAR( 50 )
CHARACTER SET latin1 COLLATE latin1_bin NULL DEFAULT NULL 

If you mean case insensitive then:

ALTER TABLE `contestants` CHANGE `ContestantName` `ContestantName` VARCHAR( 50 )
CHARACTER SET latin1 COLLATE latin1_general_ci NULL DEFAULT NULL 

For table level do (for case insensitive):

ALTER TABLE `contestants` DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci

Note that table level only affects new columns.

For database level do (for case insensitive):

ALTER DATABASE `database_name` CHARACTER SET latin1 COLLATE latin1_general_ci

Note that database level only affect new tables.

Gautier answered 31/7, 2011 at 8:16 Comment(2)
It is important to note that this is a COLUMN level collation, initially I read this too quickly and thought it was a TABLE level collation which didn't work for me.Choral
@ScottJungwirth I added info about changing it at table and database level.Gautier
B
4

Yes, use a case-insensitive collation on the columns involved.

MySQL Manual :: Column Character Set and Collation

Bacchanalia answered 31/7, 2011 at 8:13 Comment(0)
C
0

This worked for me in Mysql 5.5

ALTER TABLE `contestants` MODIFY
`ContestantName` VARCHAR(50) 
CHARACTER SET latin1
COLLATE latin1_bin;
Cinder answered 28/6, 2013 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.