¿Cómo puedo mostrar el esquema de una tabla en una base de datos MySQL?


Desde la consola MySQL, ¿qué comando muestra el esquema de una tabla dada?

Author: Brian Tompsett - 汤莱恩, 2009-09-30

5 answers

describe [db_name.]table_name;

Para la salida formateada, o

show create table [db_name.]table_name;

Para la instrucción SQL que se puede usar para crear una tabla.

 405
Author: Omry Yadan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-28 19:44:50
SHOW CREATE TABLE yourTable;

O

SHOW COLUMNS FROM yourTable;
 89
Author: Bobby,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-24 06:57:17

También puede usar abreviatura para describir como desc para la descripción de la tabla.

Desc [db_name.] table_name;

O

Use db_name;
desc table_name;

También puede usar explain para la descripción de la tabla.

Explica [db_name.] table_name;

Véase documento oficial

Dará salida como:

+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(10)     | NO   | PRI | NULL    |       |
| name     | varchar(20) | YES  |     | NULL    |       |
| age      | int(10)     | YES  |     | NULL    |       |
| sex      | varchar(10) | YES  |     | NULL    |       |
| sal      | int(10)     | YES  |     | NULL    |       |
| location | varchar(20) | YES  |     | Pune    |       |
+----------+-------------+------+-----+---------+-------+
 12
Author: Somnath Muluk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-03 12:23:01
SELECT COLUMN_NAME, TABLE_NAME,table_schema
FROM INFORMATION_SCHEMA.COLUMNS;
 5
Author: Lam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-14 10:31:49

Tal vez la pregunta necesita ser un poco más precisa aquí sobre lo que se requiere porque se puede leerlo de dos maneras diferentes. es decir,

  1. ¿Cómo obtengo la estructura/definición de una tabla en mysql?
  2. ¿Cómo obtengo el nombre del esquema/base de datos en el que reside esta tabla?

Dada la respuesta aceptada, el OP pretendía claramente que se interpretara de la primera manera. Para cualquiera que lea la pregunta de otra manera intente

SELECT `table_schema` 
FROM `information_schema`.`tables` 
WHERE `table_name` = 'whatever';
 5
Author: Paul Campbell,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-10 11:33:46