You can create an index when you execute the CREATE TABLE statement, or you can add indexes to the table by using the CREATE INDEX or ALTER TABLE alone.
1. ALTER TABLE
ALTER table is used to create a normal 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)
Where table_name is the name of the table to increase the index, column_list indicates which columns to index, and columns are separated by commas. Index name index_name optional, by default, MySQL assigns a name based on the first indexed 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 to add a normal or unique index 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 in the ALTER TABLE statement, and the index name is not selectable. In addition, the primary key index cannot be created with the CREATE INDEX statement.
3. Index type
When you create an index, you can specify whether the index can contain duplicate values. If not included, the index should be created as a primary key or a unique index. For single-column uniqueness indexes, this guarantees that a single column does not contain duplicate values. For multi-column uniqueness indexes, the combination of multiple values is guaranteed to be distinct.
The PRIMARY key index is very similar to a unique index. In fact, the PRIMARY key index is only a unique index with the name PRIMARY. This means that a table can contain only one primary KEY because it is not possible to have two indexes with the same name in a table.
The following SQL statement adds a primary key index to the students table on the SID.
ALTER TABLE Students ADD PRIMARY KEY (SID)
4. Deleting 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 handled as a statement inside ALTER TABLE, with the following syntax.
DROP INDEX index_name on Talbe_name
ALTER TABLE table_name DROP INDEX index_name
ALTER TABLE table_name DROP PRIMARY KEY
Where the first two statements are equivalent, delete the index index_name in table_name.
The 3rd statement is only used when deleting the primary key index, because a table may have only one primary key index, so 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 removed from the table, the index is affected. For multiple-column combinations of indexes, if one of the columns is deleted, the column is also removed from the index. If you delete all the columns that make up the index, the entire index is deleted.
5. View Index
Mysql> Show index from table name;
Mysql> Show keys from table name;
The role of the columns in the Index table:
· The name of the table sheet.
· Non_unique 0 If the index cannot include a repeating word. 1 if it is possible.
· The name of the Key_name index.
· Seq_in_index the column sequence number in the index, starting at 1.
· COLUMN_NAME column name.
· How the Collation column is stored in the index. In MySQL, there is a value of ' A ' (ascending) or null (no classification).
· An estimate of the number of unique values in the cardinality index. You can update by running analyze table or myisamchk-a. The cardinality is counted according to the statistics stored as integers, so even for small tables, this value is not necessarily accurate. The larger the cardinality, the greater the chance that MySQL will use the index when it is federated.
· Sub_part If the column is only partially indexed, the number of characters that are indexed. Null if the entire column is indexed.
· Packed indicates how the keyword is compressed. Null if it is not compressed.
· NULL if the column contains null, it contains Yes. If not, the column contains No.
· Index_type used index Method (BTREE, Fulltext, HASH, RTREE).
· Comment
MySQL index creation, view, delete