A brief introduction to the use of indexes in Mysql _mysql

Source: Internet
Author: User

A database index is a data structure that increases the speed of an operation, in which one or more columns can be used, providing a basis for two quick random lookups and efficient sequential access records to create the index.

When you create an index, it should be considered a column that creates one or more indexes on those columns of the SQL query.

In fact, the exponent also retains the table type for each record in the primary key or index field and the pointer's actual table.

Users are not able to see the index, they are only used to speed up the query speed, will be used for the database search engine to find the record very fast.

The indexes on the INSERT and UPDATE statement tables require more time and become a quick SELECT statement for those tables. The reason is that when making inserts or updates, the database and the need for lazy or updated index values.
Simple Unique index:

A table that can create a unique index. A unique index means that two rows cannot have the same index value. Here is the syntax to create the index table

CREATE UNIQUE INDEX index_name
on table_name (column1, Column2,...);

You can use one or more columns to create an index. For example, we can create an index using the Tutorial_author column on the table tutorials_tbl

CREATE UNIQUE INDEX author_index on
tutorials_tbl (Tutorial_author)

You can create a table with a simple index. Create a simple index as long as you omit a query that omits the unique keyword. A simple index allows a value in a repeating table.

If you want to index values in descending order in a column, you can add the reserved word desc after the column name:

mysql> CREATE UNIQUE INDEX author_index on
tutorials_tbl (Tutorial_author DESC)

Alter command to add and delete indexes:

There are four types of statement tables that add indexes:

    • ALTER TABLE tbl_name Add PRIMARY key (column_list): This statement adds a primary key, which means that the index value must be unique and cannot be null.
    • ALTER TABLE tbl_name ADD UNIQUE index_name (column_list): This statement creates an index whose value must be unique (except for null values, which may occur multiple times).
    • ALTER TABLE tbl_name Add index index_name (column_list): This adds a normal index in which any value may appear more than once.
    • ALTER TABLE tbl_name ADD fulltext index_name (column_list): This creates a special Fulltext index for text search purposes.

The following is an example of adding an index to an existing table.

mysql> ALTER TABLE testalter_tbl ADD INDEX (c);

You can delete any index by using the DROP clause and using the ALTER command. Try the following example to create an index drop above.

mysql> ALTER TABLE testalter_tbl DROP INDEX (c);

You can delete any index by using the DROP clause T and using the ALTER command. Try the following example of the index x created above.
alter command to add and remove primary KEY:

You can add a primary key, as well as in the same way. But make sure that the primary key is not NULL on the column.

This is an example of adding a primary key to an existing table. This will not be a null column and then add it as a primary key.

mysql> ALTER TABLE testalter_tbl MODIFY i INT not NULL;
mysql> ALTER TABLE testalter_tbl ADD PRIMARY KEY (i);

You can use the ALTER command to remove the primary key as follows:

mysql> ALTER TABLE testalter_tbl DROP PRIMARY KEY;

To delete an index, this is not a primary KEY, so you must specify the name of the index.
Display index information:

You can use the show index command to list all the associated index tables. Vertical format output (specified by \ G) is often useful in this sentence to avoid long time line overlap:

Try the following example:

Mysql> show INDEX from Table_name\g ...



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.