409

From the MySQL console, what command displays the schema of any given table?

HDJEMAI
  • 7,766
  • 41
  • 60
  • 81
dlamblin
  • 40,676
  • 19
  • 92
  • 127
  • Related, if it's of interest, I wrote a *Describe All Tables* in [this Answer](http://stackoverflow.com/a/38679580). – Drew Jul 31 '16 at 19:52

5 Answers5

592
describe [db_name.]table_name;

for formatted output, or

show create table [db_name.]table_name;

for the SQL statement that can be used to create a table.

Omry Yadan
  • 25,948
  • 16
  • 54
  • 77
112
SHOW CREATE TABLE yourTable;

or

SHOW COLUMNS FROM yourTable;
Bobby
  • 10,998
  • 5
  • 42
  • 67
19

You can also use shorthand for describe as desc for table description.

desc [db_name.]table_name;

or

use db_name;
desc table_name;

You can also use explain for table description.

explain [db_name.]table_name;

See official doc

Will give output like:

+----------+-------------+------+-----+---------+-------+
| 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    |       |
+----------+-------------+------+-----+---------+-------+
Somnath Muluk
  • 46,917
  • 28
  • 204
  • 217
13

Perhaps the question needs to be slightly more precise here about what is required because it can be read it two different ways. i.e.

  1. How do I get the structure/definition for a table in mysql?
  2. How do I get the name of the schema/database this table resides in?

Given the accepted answer, the OP clearly intended it to be interpreted the first way. For anybody reading the question the other way try

SELECT `table_schema` 
FROM `information_schema`.`tables` 
WHERE `table_name` = 'whatever';
Paul Campbell
  • 1,712
  • 2
  • 10
  • 17
  • Your answer for the second point helped me for what I'm looking. My editor thrown the error saying "Invalid Object" on the table I tried to use and I have no clue about the DB or Schema under which the table falls in as the table is already created by someone else already, until I got this information – mannedear Nov 24 '17 at 16:13
5
SELECT COLUMN_NAME, TABLE_NAME,table_schema
FROM INFORMATION_SCHEMA.COLUMNS;
Lam
  • 335
  • 5
  • 11