Show indexes for a table with MySQL
Posted April 15th, 2009 in MySql
MySQL has a SQL query "SHOW INDEX FROM" which returns the indexes from a table. This post looks at some example usage of this query to get a list of indexes and a list of primary keys for a table with MySQL.
Example tables
The examples in this post have two tables which are structued as follows:
CREATE TABLE `products` ( `product_id` int(10) unsigned NOT NULL auto_increment, `url` varchar(100) NOT NULL, `name` varchar(50) NOT NULL, `description` varchar(255) NOT NULL, `price` decimal(10,2) NOT NULL, `visible` tinyint(1) unsigned NOT NULL default '1', PRIMARY KEY (`product_id`), UNIQUE KEY `url` (`url`), KEY `visible` (`visible`) )
CREATE TABLE `products_to_categories` ( `product_id` int(10) unsigned NOT NULL, `category_id` int(10) unsigned NOT NULL, PRIMARY KEY (`product_id`,`category_id`) )
Show the indexes
Running the following query will show all the indexes for the products table:
SHOW INDEXES FROM products;
This will return the following:
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | products | 0 | PRIMARY | 1 | product_id | A | 0 | NULL | NULL | | BTREE | | | products | 0 | url | 1 | url | A | 0 | NULL | NULL | | BTREE | | | products | 1 | visible | 1 | visible | A | 0 | NULL | NULL | | BTREE | | +----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
Show the primary keys
To just show the primary key(s) for the products table run this query:
SHOW INDEXES FROM products WHERE Key_name = "PRIMARY";
This will return the following:
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | products | 0 | PRIMARY | 1 | product_id | A | 0 | NULL | NULL | | BTREE | | +----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
Show the primary keys - multiple primary keys
The final example uses the products_to_categories table which has a multi-part primary key, so two rows are returned. The SQL:
SHOW INDEXES FROM products_to_categories WHERE Key_name = "PRIMARY";
And the result:
+------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | products_to_categories | 0 | PRIMARY | 1 | product_id | A | NULL | NULL | NULL | | BTREE | | | products_to_categories | 0 | PRIMARY | 2 | category_id | A | 0 | NULL | NULL | | BTREE | | +------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
Summary
It's easy to show the indexes and primary keys for a table with MySQL. You can read more about the columns returned on the SHOW INDEX Syntax manual page in the MySQL manual.
Related posts:
- Copy a MySQL table with phpMyAdmin (Wednesday, February 18th 2009)
- Copy a table in MySQL with CREATE TABLE LIKE (Saturday, February 14th 2009)
- Copy a table in MySQL (Wednesday, February 11th 2009)
- How to tell which storage engine a MySQL table uses (Tuesday, January 13th 2009)
- Check if a MySQL table exists (Friday, June 13th 2008)
- Listing tables and their structure with the MySQL Command Line Client (Sunday, May 18th 2008)
Share or Bookmark
Share or Bookmark this page using the following services. You will need to have an account with the selected service in order to post links or bookmark this page.
Subscribe or Follow
Subscribe via RSS or email, or follow me on Facebook or Twitter below. The RSS icon takes you through to Feedburner where you can select the service or application to use.
