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

假如想象 提交于 2019-11-30 04:46:57

问题


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;

回答1:


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.




回答2:


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

MySQL Manual :: Column Character Set and Collation




回答3:


This worked for me in Mysql 5.5

ALTER TABLE `contestants` MODIFY
`ContestantName` VARCHAR(50) 
CHARACTER SET latin1
COLLATE latin1_bin;


来源:https://stackoverflow.com/questions/6888279/is-there-any-way-to-make-a-unique-index-case-insensitive-in-mysql-5-1-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!