如何查看在MySQL中设置的索引?

要查看索引,可以使用SHOW命令。

以下是语法-

show index from yourTableName;

让我们首先创建一个表-

mysql> create table DemoTable
   (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   FirstName varchar(20),
   LastName varchar(20)
   );

以下是创建索引的查询-

mysql> create index id_FirstName on DemoTable(Id,FirstName);
Records : 0 Duplicates : 0 Warnings : 0

以下是查询以查看我在MySQL中设置的索引的查询-

mysql> show index from DemoTable;

这将产生以下显示索引的输出-

+--------------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| Table        | Non_unique | Key_name     | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible |
+--------------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| DemoTable    | 0          | PRIMARY      | 1            | Id          | A         | 0           | NULL     | NULL   |      | BTREE      |         |               | YES     |
| DemoTable    | 1          | id_FirstName | 1            | Id          | A         | 0           | NULL     | NULL   |      | BTREE      |         |               | YES     |
| DemoTable    | 1          | id_FirstName | 2            | FirstName   | A         | 0           | NULL     | NULL   | YES  | BTREE      |         |               | YES     |
+--------------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
3 rows in set (0.26 sec)