MySQL index use

Source: Internet
Author: User

  

1. Start by randomly creating a table with the following SQL statement:

CREATE TABLE IF not EXISTS ' student ' (

' id ' int (one) not NULL auto_increment COMMENT ' School Number ',

' Name ' varchar (+) not NULL DEFAULT ' COMMENT ' name ',

' Sex ' tinyint (1) not NULL COMMENT ' sex ',

' Age ' tinyint (2) is not NULL COMMENT ' ages ',

' class ' varchar (+) not NULL DEFAULT ' COMMENT ' class ',

PRIMARY KEY (' id ')

Engine=myisam DEFAULT charset=utf8 comment= ' student table ';

2. As you can see, in the SQL statement that created the table, a primary key index has been established and the index in the table is viewed: Show index from ' student '

3. Of course, we can also add other indexes on the basis, such as a unique index. Assuming that each student's name cannot be duplicated, a unique index can be added to the Name field:

ALTER TABLE ' student ' ADD UNIQUE ' stu_name ' (' name ');

At this point, look again at the index in the table, show index from ' student '

4. Then add a normal index to the class:

ALTER TABLE ' student ' ADD INDEX ' Stu_class ' (' class ');

View index in table, show index from ' student '

5. Next, delete the index, remove the unique index and the normal index:

ALTER TABLE ' student ' DROP INDEX ' stu_name ';

ALTER TABLE ' student ' DROP INDEX ' stu_class ';

Then look at the index in the table, show index from ' student '

MySQL index use

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.