MySQL add index MySQL How to create an index _mysql

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 indexing)
Mysql>alter TABLE ' table_name ' ADD fulltext (' column ')
5. Adding multiple-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 ALTER TABLE, an 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 using the ALTER TABLE statement is:
ALTER table < table name > ADD INDEX (< field >);

Let's try to add 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 to the T_name field is changed from the original blank to the Mul. What does this mul mean? To explain briefly: if the key is Mul, then the value of the column can be repeated, the column is a leading column of a non unique index (the first column) or part of a unique index but can contain null values.

About MySQL using SQL statements to add indexes to fields in the table, this article on the introduction of so many, I hope to help you, thank you!

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.