A simple learning tutorial for a unique index in Mysql _mysql

Source: Internet
Author: User

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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.