Informationen über eine Tabelle in MySQL ermitteln

Ab und zu benötigt man mal in einem Programm doch etwas mehr Angaben zu einer Tabelle. Diese kann man sich durch ein paar einfache SQL Anweisungen alle besorgen, do wie waren die Anweisungen noch mal? Anbei eine kurze Übersicht, um umfangreiche Informationen zu einer Tabelle in MySQL zu ermitteln.

Spalten einer Tabelle

Den Aufbau einer Tabelle ermittelt man einfach mit „describe tablename„.

mysql> describe partners;
+------------+---------------------+------+-----+---------+----------------+
| Field      | Type                | Null | Key | Default | Extra          |
+------------+---------------------+------+-----+---------+----------------+
| id         | int(10) unsigned    | NO   | PRI | NULL    | auto_increment |
| salutation | tinyint(3) unsigned | NO   |     | NULL    |                |
| title      | varchar(64)         | NO   |     | NULL    |                |
| firstname  | varchar(64)         | NO   |     | NULL    |                |
| lastname   | varchar(64)         | NO   |     | NULL    |                |
+------------+---------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

Man erhält dadurch zwar wunderschön die Informationen über die einzelnen Spalten, aber keine Information über die Storage Engine, die Indizes sowie die Spaltenkommentare.

Indizes einer Tabelle

Genaue Angaben zu den Inizes erhält man einfach mit „show index from tablename„.

mysql> show index from users;
+-------+------------+---------------------+--------------+-------------+-----------+
| Table | Non_unique | Key_name            | Seq_in_index | Column_name | Collation |
+-------+------------+---------------------+--------------+-------------+-----------+
| users |          0 | PRIMARY             |            1 | id          | A         |
| users |          0 | login_UNIQUE        |            1 | login       | A         |
| users |          1 | fk_users_customers1 |            1 | customer_id | A         |
| users |          1 | fk_users_partner1   |            1 | partner_id  | A         |
+-------+------------+---------------------+--------------+-------------+-----------+
+-------------+----------+--------+------+------------+---------+
| Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------------+----------+--------+------+------------+---------+
|           0 |     NULL | NULL   |      | BTREE      |         |
|           0 |     NULL | NULL   |      | BTREE      |         |
|           0 |     NULL | NULL   | YES  | BTREE      |         |
|           0 |     NULL | NULL   | YES  | BTREE      |         |
+-------------+----------+--------+------+------------+---------+
1 row in set (0.00 sec)

Aber auch hier gibt es 

Statistik zu einer Tabelle

mysql> show table status from shop like 'partners';
+----------+--------+---------+------------+------+----------------+-------------+
| Name     | Engine | Version | Row_format | Rows | Avg_row_length | Data_length |
+----------+--------+---------+------------+------+----------------+-------------+
| partners | InnoDB | 10      | Compact    | 0    | 0              | 16384       |
+----------+--------+---------+------------+------+----------------+-------------+
+-----------------+--------------+-----------+----------------+---------------------+
| Max_data_length | Index_length | Data_free | Auto_increment | Create_time         |
+-----------------+--------------+-----------+----------------+---------------------+
| 0               | 0            | 134217728 | 1              | 2011-08-07 17:42:14 |
+-----------------+--------------+-----------+----------------+---------------------+
+-------------+------------+-----------------+----------+----------------+---------+
| Update_time | Check_time | Collation       | Checksum | Create_options | Comment |
+-------------+------------+-----------------+----------+----------------+---------+
| NULL        | NULL       | utf8_unicode_ci | NULL     |                |         |
+-------------+------------+-----------------+----------+----------------+---------+
1 row in set (0.02 sec)

Create Statement

Selbst die Create Anweisung muss man sich nicht sleber aus den oben stehenden Daten zusammen bauen. MySQL liefert auch an dieser Stelle bereits fertige Informationen. Mit „show create tablename“ erhält man die Anweisung bereits fix und fertig.

mysql> show create table partners;
+----------+------------------------------------------------------------+
| Table    | Create Table                                               |
+----------+------------------------------------------------------------+
| partners | CREATE TABLE `partners` (
             `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
             `salutation` tinyint(3) unsigned NOT NULL,
             `title` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
             `firstname` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
             `lastname` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
             PRIMARY KEY (`id`) )
             ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
+----------+------------------------------------------------------------+
1 row in set (0.00 sec)


Deprecated: Directive 'allow_url_include' is deprecated in Unknown on line 0