如何获取MySQL索引列表?

首先让我们看看如何显示MySQL的索引。为此,请使用SHOW命令。

显示索引的查询如下-

mysql> SHOW INDEX FROM indexingdemo;

这是输出。

+--------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| Table        | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible |
+--------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| indexingdemo |          1 | indexName |            1 | Name        | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | YES     |
+--------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
1 row in set (0.17 sec)

您可以在以下查询的帮助下获取MySQL索引列表。

在此之前,我们首先来看一下语法。

SELECT DISTINCT
TABLE_NAME,
INDEX_NAME
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = 'yourDatabaseName';

现在,让我们应用以上语法来获取MySQL索引列表。

mysql> SELECT DISTINCT
   ->     TABLE_NAME,
   ->     INDEX_NAME
   -> FROM INFORMATION_SCHEMA.STATISTICS
   -> WHERE TABLE_SCHEMA = 'business';

以下是显示索引列表的输出。

+--------------------------+------------------+
| TABLE_NAME               | INDEX_NAME       |
+--------------------------+------------------+
| addcolumntable           | Name_Index       |
| addingautoincrement      | PRIMARY          |
| addingunique             | name             |
| autoincrement            | PRIMARY          |
| autoincrementtable       | PRIMARY          |
| bookindexes              | BookName         |
| childdemo                | ConstChild       |
| clonestudent             | idIndex          |
| clonestudent             | NameStuIndex     |
| college                  | PRIMARY          |
| compositeprimarykey      | PRIMARY          |
| demoauto                 | PRIMARY          |
| demoindex                | PRIMARY          |
| demoschema               | idDemoIndex      |
| duplicatebookindexes     | BookName         |
| employeeinformation      | PRIMARY          |
| foreigntable             | constFKPK        |
| foreigntabledemo         | FKConst          |
| functionindexdemo        | indFirstName     |
| indexingdemo             | indexName        |
| keydemo                  | PRIMARY          |
| lastinsertrecordiddemo   | PRIMARY          |
| multipleindexdemo        | id               |
| nextiddemo               | PRIMARY          |
| parentdemo               | PRIMARY          |
| primarytable             | PRIMARY          |
| primarytable1            | PRIMARY          |
| primarytabledemo         | PRIMARY          |
| schemadatabasemethoddemo | PRIMARY          |
| sequencedemo             | PRIMARY          |
| student                  | idIndex          |
| student                  | NameStuIndex     |
| studentenrollment        | StudCollegeConst |
| tabledemo2               | ConstFK          |
| tabledemo3               | ConstFK          |
| tablepri                 | PRIMARY          |
| tblf                     | ConstFK          |
| tblp                     | PRIMARY          |
| transcationdemo          | PRIMARY          |
| triggedemo               | PRIMARY          |
| uniqueautoid             | id               |
| uniqueconstdemo          | name             |
| uniquedemo               | name             |
| uniquedemo1              | id               |
| updtable                 | PRIMARY          |
| usernameandpassworddemo  | PRIMARY          |
| usernameandpassworddemo  | UserId           |
+--------------------------+------------------+
47 rows in set (0.07 sec)