Mysql index!

Source: Internet
Author: User
Tags mysql index

1. ALTER TABLE
Alter table is used to create a common index, a UNIQUE index, or a primary key index.

Alter table table_name add index index_name (column_list)
Alter table table_name add unique (column_list)
Alter table table_name add primary key (column_list)

Table_name is the name of the table to be indexed, and column_list indicates which columns are indexed. When multiple columns are indexed, they are separated by commas. The index name index_name is optional and is time-saving. MySQL assigns a name based on the first index column. In addition, alter table allows you to change multiple tables in a single statement, so you can create multiple indexes at the same time.
2. CREATE INDEX
Create index can add normal or UNIQUE indexes to a table.

Create index index_name ON table_name (column_list)
Create unique index index_name ON table_name (column_list)

Table_name, index_name, and column_list have the same meaning as the alter table statement, and the index name is not optional. In addition, you cannot use the create index statement to CREATE a primary key index.
3. Index type
When creating an index, you can specify whether the index can contain duplicate values. If not, the index should be created as a primary key or UNIQUE index. For single-column uniqueness indexes, this ensures that a single column does not contain duplicate values. For multi-column uniqueness indexes, the combination of multiple values is not repeated.
The primary key index is very similar to the unique index. In fact, the primary key index is only a unique index with the name primary. This indicates that a table can only contain one primary key, because a table cannot have two indexes with the same name.
The following SQL statement adds the primary key index to the students table on the SID.

Alter table students add primary key (SID)

4. delete an index
You can use the alter table or drop index statement to delete an index. Similar to the create index statement, drop index can be processed as a statement in alter table. The syntax is as follows.

Drop index index_name on talbe_name
Alter table table_name drop index index_name
Alter table table_name drop primary key

The first two statements are equivalent. The index index_name in table_name is deleted.
The first statement is only used to delete the primary key index. Because a table only has one primary key index, you do not need to specify the index name. If the primary key index is not created, but the table has one or more unique indexes, MySQL deletes the first unique index.
If a column is deleted from the table, the index is affected. If you delete a column in an index with multiple columns, the column is also deleted from the index. If you delete all the columns that make up the index, the entire index will be deleted.

5. View Indexes
Mysql> show index from tblname;
Mysql> show keys from tblname;
· Table
Table name.
· Non_unique
If the index cannot contain duplicate words, the value is 0. If yes, it is 1.
· Key_name
The name of the index.
· Seq_in_index
The column serial number in the index, starting from 1.
· Column_name
Column name.
· Collation
How the column is stored in the index. In MySQL, there are values 'A' (ascending) or null (unclassified ).
· Cardinality
The estimated number of unique values in the index. You can update analyze table or myisamchk-A by running analyze table. The base number is counted based on the statistical data stored as an integer. Therefore, this value is not required to be accurate even for small tables. The larger the base, the larger the chance for MySQL to use the index for union.
· Sub_part
If a column is partially indexed, it is the number of indexed characters. If the entire column is indexed, the value is null.
· Packed
Indicates how keywords are compressed. If it is not compressed, It is null.
· Null
If the column contains null, yes is included. If no, the column contains no.
· Index_type
Used Index methods (btree, Fulltext, hash, rtree ).
· Comment

Related Article

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.