How to get database structure in MySQL via query

Mysql

Mysql Problem Overview


Is it possible to somehow get structure of MySQL database, or just some table with simple query?

Or is there another way, how can I do it?

Mysql Solutions


Solution 1 - Mysql

I think that what you're after is DESCRIBE

DESCRIBE table;

You can also use SHOW TABLES

SHOW TABLES;

to get a list of the tables in your database.

Solution 2 - Mysql

To get the whole database structure as a set of CREATE TABLE statements, use mysqldump:

mysqldump database_name --compact --no-data

For single tables, add the table name after db name in mysqldump. You get the same results with SQL and SHOW CREATE TABLE:

SHOW CREATE TABLE table;

Or DESCRIBE if you prefer a column listing:

DESCRIBE table;

Solution 3 - Mysql

Take a look at the INFORMATION_SCHEMA.TABLES table. It contains metadata about all your tables.

Example:

SELECT * FROM `INFORMATION_SCHEMA`.`TABLES`
WHERE TABLE_NAME LIKE 'table1'

The advantage of this over other methods is that you can easily use queries like the one above as subqueries in your other queries.

Solution 4 - Mysql

using this:

SHOW CREATE TABLE `users`;

will give you the [DDL][1] for that table

DESCRIBE `users`

will list the columns in that table [1]: http://en.wikipedia.org/wiki/Data_Definition_Language

Solution 5 - Mysql

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_SCHEMA = 'test' AND TABLE_NAME ='products'; 

where Table_schema is database name

Solution 6 - Mysql

That's the SHOW CREATE TABLE query. You can query the SCHEMA TABLES, too.

SHOW CREATE TABLE YourTableName;

Solution 7 - Mysql

A variation of the first answer that I found useful

Open your command prompt and enter (you dont have to be logged into your mysql server)

mysqldump -hlocalhost -u<root> -p<password>  <dbname>  --compact --no-data > </path_to_mydump/>mysql.dmp

Solution 8 - Mysql

You can pick any one of the command below. All of them are more or less same:

SHOW CREATE TABLE TABLE_NAME;

DESC TABLE_NAME;

SHOW FULL COLUMNS FROM TABLE_NAME; (for column properties)

EXPLAIN TABLE_NAME;

DESCRIBE TABLE_NAME;

Solution 9 - Mysql

In the following example,

> playground is the database name and equipment is the table name

Another way is using SHOW-COLUMNS:5.5 (available also for 5.5>)

$ mysql -uroot -p<password> -h<host> -P<port> -e \
    "SHOW COLUMNS FROM playground.equipment"

And the output:

mysql: [Warning] Using a password on the command line interface can be insecure.
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| type  | varchar(50) | YES  |     | NULL    |                |
| quant | int(11)     | YES  |     | NULL    |                |
| color | varchar(25) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+

One can also use mysqlshow-client (also available for 5.5>) like following:

$ mysqlshow -uroot -p<password> -h<host> -P<port> \
    playground equipment

And the output:

mysqlshow: [Warning] Using a password on the command line interface can be insecure.
Database: playground  Table: equipment
+-------+-------------+-------------------+------+-----+---------+----------------+---------------------------------+---------+
| Field | Type        | Collation         | Null | Key | Default | Extra          | Privileges                      | Comment |
+-------+-------------+-------------------+------+-----+---------+----------------+---------------------------------+---------+
| id    | int(11)     |                   | NO   | PRI |         | auto_increment | select,insert,update,references |         |
| type  | varchar(50) | latin1_swedish_ci | YES  |     |         |                | select,insert,update,references |         |
| quant | int(11)     |                   | YES  |     |         |                | select,insert,update,references |         |
| color | varchar(25) | latin1_swedish_ci | YES  |     |         |                | select,insert,update,references |         |
+-------+-------------+-------------------+------+-----+---------+----------------+---------------------------------+---------+

Solution 10 - Mysql

SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='bodb' AND TABLE_NAME='abc';

works for getting all column names

Solution 11 - Mysql

Nowadays, people use DESC instead of DESCRIPTION. For example:- DESC users;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJakub ArnoldView Question on Stackoverflow
Solution 1 - MysqlBill the LizardView Answer on Stackoverflow
Solution 2 - MysqlEemeli KantolaView Answer on Stackoverflow
Solution 3 - MysqlZenshaiView Answer on Stackoverflow
Solution 4 - MysqlduckyflipView Answer on Stackoverflow
Solution 5 - MysqlashishView Answer on Stackoverflow
Solution 6 - MysqlsoulmergeView Answer on Stackoverflow
Solution 7 - MysqlLGGView Answer on Stackoverflow
Solution 8 - MysqlVasanth SaminathanView Answer on Stackoverflow
Solution 9 - MysqlShudipta SharmaView Answer on Stackoverflow
Solution 10 - MysqlRaja Nagendra KumarView Answer on Stackoverflow
Solution 11 - MysqlSh4dyView Answer on Stackoverflow