1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Show Indexes For A Table as a mysql til

This commit is contained in:
jbranchaud
2017-06-03 21:55:36 -05:00
parent 08d6474008
commit ede6aec9ac
2 changed files with 35 additions and 1 deletions

View File

@@ -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

View File

@@ -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 | | |
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
```