From ede6aec9ac877c070bf7e828d984cf8e2df94a46 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sat, 3 Jun 2017 21:55:36 -0500 Subject: [PATCH] Add Show Indexes For A Table as a mysql til --- README.md | 3 ++- mysql/show-indexes-for-a-table.md | 33 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 mysql/show-indexes-for-a-table.md diff --git a/README.md b/README.md index 1ef3d9b..775cd3b 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/). For a steady stream of TILs from a variety of rocketeers, checkout [til.hashrocket.com](https://til.hashrocket.com/). -_535 TILs and counting..._ +_536 TILs and counting..._ --- @@ -232,6 +232,7 @@ _535 TILs and counting..._ ### MySQL - [List Databases And Tables](mysql/list-databases-and-tables.md) +- [Show Indexes For A Table](mysql/show-indexes-for-a-table.md) ### Phoenix diff --git a/mysql/show-indexes-for-a-table.md b/mysql/show-indexes-for-a-table.md new file mode 100644 index 0000000..2e61e46 --- /dev/null +++ b/mysql/show-indexes-for-a-table.md @@ -0,0 +1,33 @@ +# Show Indexes For A Table + +When describing a table, such as the table `users`: + +```sql +> describe users; ++------------+-----------------------+------+-----+---------+----------------+ +| Field | Type | Null | Key | Default | Extra | ++------------+-----------------------+------+-----+---------+----------------+ +| id | mediumint(8) unsigned | NO | PRI | NULL | auto_increment | +| first_name | varchar(80) | NO | | NULL | | +| last_name | varchar(80) | NO | | NULL | | +| email | varchar(80) | NO | UNI | NULL | | ++------------+-----------------------+------+-----+---------+----------------+ +``` + +I can see in the `Key` column that there is a primary key and a unique key +for this table on `id` and `email`, respectively. + +These keys are indexes. To get more details about each of the indexes on +this table, we can use the +[`show indexes`](https://dev.mysql.com/doc/refman/5.7/en/show-index.html) +command. + +```sql +> show indexes in users; ++-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ +| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | ++-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ +| users | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | | +| users | 0 | unique_email | 1 | email | A | 0 | NULL | NULL | | BTREE | | | ++-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ +```