MySQL add index MySQL How to create an index

Source: Internet
Author: User
Tags mul

1. Add primary key (primary key index)
Mysql>alter TABLE ' table_name ' ADD PRIMARY KEY (' column ')
2. Add unique (unique index)
Mysql>alter TABLE ' table_name ' ADD UNIQUE (
' Column '
)
3. Add index (normal index)
Mysql>alter TABLE ' table_name ' ADD INDEX index_name (' column ')
4. Add fulltext (full-text index)
Mysql>alter TABLE ' table_name ' ADD fulltext (' column ')
5. Adding Multi-column indexes
Mysql>alter TABLE ' table_name ' ADD INDEX index_name (' column1 ', ' column2 ', ' column3 ')

Here's a more detailed approach

In MySQL, you can use the ALTER TABLE SQL statement to add an index to a field in a table.

The basic syntax for adding an index to a field in a table by using the ALTER TABLE statement is:
ALTER table < table name > ADD INDEX (< field >);

Let's try adding an index to the T_name field in test.

Mysql> ALTER TABLE test add index (T_NAME);
Query OK, 0 rows affected (0.17 sec)
records:0 duplicates:0 warnings:0

After successful execution, let's take a look at the results.

mysql> describe test;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| t_id | Int (11) |     YES | |       NULL | |
| T_name | varchar (50) | NO | MUL |       NULL | |
| T_password | char (32) |     YES | |       NULL | |
| T_birth | Date |     YES | |       NULL | |
+------------+-------------+------+-----+---------+-------+
4 rows in Set (0.00 sec)

As you can see, the key column of the T_name field is changed from the original blank to the Mul. What does this mul mean? A simple explanation: If key is Mul, then the value of the column can be repeated, which is a leading column of a non-unique index (the first column) or a component of a uniqueness index but can contain null values.

MySQL add index MySQL How to create an index

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.