MySQL 中的 SHOW INDEX、SHOW INDEXES 和 SHOW KEYS 的區別?


show index、show indexes 和 show keys 沒有區別。它們有相似的含義。

我們首先建立一個表 −

mysql> create table DemoTable1549
   -> (
   -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> EmployeeName varchar(20)
   -> );
Query OK, 0 rows affected (0.82 sec)

以下是建立索引的查詢 −

mysql> create index name_index1 on DemoTable1549(EmployeeName);
Query OK, 0 rows affected (0.41 sec)
Records: 0  Duplicates: 0  Warnings: 0

以下是 SHOW INDEX 的查詢 −

mysql> show index from DemoTable1549;

這將產生以下輸出 −

+---------------+------------+-------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| Table         | Non_unique | Key_name    | Seq_in_index | Column_name  | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible |
+---------------+------------+-------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| demotable1549 |          0 | PRIMARY     |            1 | EmployeeId   |         A |           0 |     NULL |   NULL |      | BTREE      |         |               |     YES |
| demotable1549 |          1 | name_index1 |            1 | EmployeeName |         A |           0 |     NULL |   NULL |  YES | BTREE      |         |               |     YES |
+---------------+------------+-------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
2 rows in set (0.17 sec)

以下是 SHOW INDEXES 的查詢 −

mysql> show indexes from DemoTable1549;

這將產生以下輸出 −

+---------------+------------+-------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| Table         | Non_unique | Key_name    | Seq_in_index | Column_name  | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible |
+---------------+------------+-------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| demotable1549 |          0 | PRIMARY     |            1 | EmployeeId   |         A |           0 |     NULL |   NULL |      | BTREE      |         |               |     YES |
| demotable1549 |          1 | name_index1 |            1 | EmployeeName |         A |           0 |     NULL |   NULL | YES  | BTREE      |         |               |     YES |
+---------------+------------+-------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
2 rows in set (0.00 sec)

以下是實施 SHOW KEYS 的查詢 −

mysql> show keys from DemoTable1549;

這將產生以下輸出 −

+---------------+------------+-------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| Table         | Non_unique | Key_name    | Seq_in_index | Column_name  | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible |
+---------------+------------+-------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| demotable1549 |          0 | PRIMARY     |            1 | EmployeeId   |         A |           0 |     NULL | NULL   |      | BTREE      |         |               |     YES |
| demotable1549 |          1 | name_index1 |            1 | EmployeeName |         A |           0 |     NULL | NULL   | YES  | BTREE      |         |               |     YES |
+---------------+------------+-------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
2 rows in set (0.00 sec)

更新於: 12-12-2019

223 次瀏覽

為你的 事業助力

完成課程獲得認證

開始吧
廣告