MySQL unique index unique for not repeating data fields we often set the ID in the datasheet to a unique index unique, let me explain how to use a unique index in MySQL unique bar.
The purpose of creating a unique index is not to improve access speed, but to avoid duplication of data. A unique index can have more than one but the value of the indexed column must be unique, and the value of the indexed column allows null values. If you can determine that a data column will contain only values that are different from each other, you should use the keyword unique when you create an index for that data column.
Define it as a unique index.
Set directly when creating tables:
DROP TABLE IF EXISTS ' student ';
CREATE TABLE ' student ' (
' stu_id ' int (one) not null auto_increment,
' name ' varchar (255) DEFAULT null,
PRIMARY Key (' stu_id '),
UNIQUE key ' Uk_student_name ' (' name ')
engine=innodb auto_increment=12 DEFAULT Charset=utf8;
To create a unique index:
Create unique index uk_student_name on student (name);
To add a constraint after a table has been built:
ALTER TABLE student add constraint uk_student_name unique (name);
If you do not need a unique index, you can delete the
mysql> ALTER TABLE student DROP INDEX name;
Query OK, 0 rows affected (0.85 sec)
If you want to add an index
ALTER TABLE user add unique index (USER_ID,USER_NAME);