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!