MySQL - DESCRIBE Statement

You can get the information about the table structure using the DESCRIBE statement. This statement works as a shortcut for SHOW COLUMNS. You can also use DESC for this statement.
Following is the syntax of the DESCRIBE statement −
DESCRIBE DESC tbl_name [col_name | wild]

Assume we have created a table with name Sales in MySQL database using CREATE statement as follows −
mysql> CREATE TABLE sales(
   ID INT,
   ProductName VARCHAR(255),
   CustomerName VARCHAR(255),
   DispatchDate date,
   DeliveryTime time,
   Price INT,
   Location VARCHAR(255)
);
Query OK, 0 rows affected (2.22 sec)
Following query gets the information about columns of the sales table −
mysql> DESCRIBE SALES;
+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| ID           | int          | YES  |     | NULL    |       |
| ProductName  | varchar(255) | YES  |     | NULL    |       |
| CustomerName | varchar(255) | YES  |     | NULL    |       |
| DispatchDate | date         | YES  |     | NULL    |       |
| DeliveryTime | time         | YES  |     | NULL    |       |
| Price        | int          | YES  |     | NULL    |       |
| Location     | varchar(255) | YES  |     | NULL    |       | 
+--------------+--------------+------+-----+---------+-------+
7 rows in set (0.42 sec)
By default, this statement provides information about all the columns in the specified table you can retrieve information about a particular table by specifying the name of the column −
mysql> DESCRIBE SALES ProductName;
+-------------+--------------+------+-----+---------+-------+
| Field       | Type         | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| ProductName | varchar(255) | YES  |     | NULL    |       |
+-------------+--------------+------+-----+---------+-------+
1 row in set (0.07 sec)
You can also retrieve the column information using the DESC statement instead of DESCRIBE.
mysql> DESC SALES;
+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| ID           | int          | YES  |     | NULL    |       |
| ProductName  | varchar(255) | YES  |     | NULL    |       |
| CustomerName | varchar(255) | YES  |     | NULL    |       |
| DispatchDate | date         | YES  |     | NULL    |       |
| DeliveryTime | time         | YES  |     | NULL    |       |
| Price        | int          | YES  |     | NULL    |       |
| Location     | varchar(255) | YES  |     | NULL    |       |
+--------------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
You can retrieve the information in various formats using the explain_type option. The value to this option can be TRADITIONAL, JSON and, TREE as follows −
mysql> DESC FORMAT = TREE SELECT * FROM SALES;
+---------------------------------------------+
| EXPLAIN                                     |
+---------------------------------------------+
| –> Table scan on SALES (cost=0.35 rows=1) |
+---------------------------------------------+
1 row in set (0.09 sec)

In traditional format −
mysql> DESC FORMAT = TRADITIONAL SELECT * FROM SALES;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| 1  | SIMPLE      | SALES | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 1    | 100.00   | NULL  |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.07 sec)
In JSON format −
mysql> DESC FORMAT = JSON SELECT * FROM SALES;
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| {
   "query_block": {
      "select_id": 1,
      "cost_info": {
         "query_cost": "0.35"
      },
      "table": {
         "table_name": "SALES",
         "access_type": "ALL",
         "rows_examined_per_scan": 1,
         "rows_produced_per_join": 1,
         "filtered": "100.00",
         "cost_info": {
            "read_cost": "0.25",
            "eval_cost": "0.10",
            "prefix_cost": "0.35",
            "data_read_per_join": "3K"
         },
         "used_columns": [
            "ID",
            "ProductName",
            "CustomerName",
            "DispatchDate",
            "DeliveryTime",
            "Price",
            "Location"
         ]
      }
   }
} |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)

    31 Lectures 
     6 hours 

 Eduonix Learning Solutions

    84 Lectures 
     5.5 hours 

 Frahaan Hussain

    6 Lectures 
     3.5 hours 

 DATAhill Solutions Srinivas Reddy

    60 Lectures 
     10 hours 

 Vijay Kumar Parvatha Reddy

    10 Lectures 
     1 hours 

 Harshit Srivastava

    25 Lectures 
     4 hours 

 Trevoir Williams
 Print
 Add Notes
 Bookmark this page