Course
Describe Tables
MySQL Tutorial
This SQL tutorial is structured for beginners to guide them from the foundational concepts to advanced data manipulation and querying techniques in SQL. By the end of this tutorial, you will have developed a robust understanding of SQL, equipping you with the knowledge to efficiently manage and analyze data across various database systems. This guide sets the stage for your growth into a skilled data professional, ready to tackle complex data challenges and contribute to the field of data analysis and database management.
DESCRIBE Tables
Describing a MySQL table refers to retrieving its definition or structure. When we describe a table, it basically includes the fields present, their datatypes, and if any constraints defined on them.
We can get the information about the table structure using the following SQL statements
- DESCRIBE Statement
- DESC Statement
- SHOW COLUMNS Statement
- EXPLAIN Statement
All these statements are used for the same purpose. Let us learn about them in detail, one by one, in this tutorial.
DESCRIBE Statement
The MySQL DESCRIBE statement is used to retrieve a table-related information, which consists of field names, field data types, and constraints (if any). This statement is a shortcut for the SHOW columns statement (they both retrieve the same information from a table).
Apart from retrieving a table's definition, this statement can be used to get the information of a particular field in a table.
Syntax
Following is the syntax of MySQL DESCRIBE statement
DESCRIBE table_name [col_name | wild];
Example
In the following example, we are creating a table named CUSTOMERS using the CREATE TABLE statement
CREATE TABLE CUSTOMERS ( ID INT AUTO_INCREMENT, NAME VARCHAR(20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID));
Now, execute the following query to get the information about columns of the CUSTOMERS table
DESCRIBE CUSTOMERS;
Output
Following is the columns information of CUSTOMERS table
Describing a specific column
By default, the DESCRIBE statement provides information about all the columns in the specified table. But you can also retrieve information about a particular column of a table by specifying the name of that column.
For example, the following query displays information about NAME column of CUSTOMERS table, which we created in the previous example.
DESCRIBE CUSTOMERS NAME;
Output
Following is the description of NAME column in CUSTOMERS table
DESC Statement
We can also retrieve the table information using the MySQL DESC statement instead of DESCRIBE. They both give the same results, so DESC is just a shortcut for DESCRIBE statement.
Syntax
Following is the syntax of the MySQL DESC statement
DESC table_name [col_name | wild];
Example
In this example, we are trying to get the information of CUSTOMERS table using DESC statement.
DESC CUSTOMERS;
Following is the columns information of CUSTOMERS table
Describing a specific column
We can also get the information of a specific column in a given table, similar to using DESCRIBE. Instead of DESCRIBE, we use DESC.
For example, the following query displays information about NAME column of CUSTOMERS table.
DESC CUSTOMERS NAME;
Output
Following is the description of NAME column in CUSTOMERS table
SHOW COLUMNS Statement
The MySQL SHOW COLUMNS Statement is used to display the information of all the columns present in a table. The DESCRIBE statement is a shortcut for this statement.
Note: This statement will not display information of a specific field.
Syntax
Following is the syntax of the SHOW COLUMNS statement
SHOW COLUMNS FROM table_name;
Example
Here, we are retrieving column information of the same CUSTOMERS table using the SHOW COLUMNS statement.
SHOW COLUMNS FROM CUSTOMERS;
Following is the columns information of CUSTOMERS table
EXPLAIN Statement
The MySQL EXPLAIN Statement is a synonym of DESCRIBE Statement which retrieves the information of a table's structure such as column names, column data types, and constraints (if any).
Syntax
Following is the syntax of the SHOW COLUMNS statement
EXPLAIN table_name;
Example
In the following query, we are retrieving column information of the CUSTOMERS table using the EXPLAIN statement.
EXPLAIN CUSTOMERS;
Following is the columns information of CUSTOMERS table
Describe Tables in Different Formats
You can retrieve the information in various formats using the explain_type option. The value to this option can be TRADITIONAL, JSON and, TREE.
Syntax
Following is the syntax to describe tables in different formats
{EXPLAIN | DESCRIBE | DESC} explain_type: { FORMAT = format_name } select_statement
Example
In the following example, we are describing the CUSTOMERS table format as TRADITIONAL.
EXPLAIN FORMAT = TRADITIONAL SELECT * FROM CUSTOMERS;
Output
Executing the query above will produce the following output
Example
Here, we are describing the CUSTOMERS table format as JSON.
EXPLAIN FORMAT = JSON SELECT * FROM CUSTOMERS;
Output
Executing the query above will produce the following output
Example
In the following example, we are describing the CUSTOMERS table format as TREE.
EXPLAIN FORMAT = TREE SELECT * FROM CUSTOMERS;
Output
Executing the query above will produce the following output
Describing Table Using a Client Program
In addition to describe a table from MySQL Database using the MySQL query, we can also perform the DESCRIBE TABLE operation on a table using a client program.
Syntax
Following are the syntaxes to describe a table from MySQL Database in various programming languages
PHPNodeJSJavaPython
To describe a table from MySQL Database through a PHP program, we need to execute the Describe Table statement using the mysqli function query() as
$sql="Describe Table_name";$mysqli->query($sql);
Example
Following are the programs
PHPNodeJSJavaPython
$dbhost = 'localhost';$dbuser = 'root';$dbpass = 'password';$dbname = 'TUTORIALS';$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($mysqli->connect_errno) { printf("Connect failed: %s", $mysqli->connect_error); exit();}// printf('Connected successfully.');
$sql = " DESCRIBE sales ";if ($q = $mysqli->query($sql)) { printf(" Table described successfully."); while ($row = mysqli_fetch_array($q)) { echo "{$row['Field']} - {$row['Type']}\n"; }}if ($mysqli->errno) { printf("table could not be described .", $mysqli->error);}$mysqli->close();
Output
The output obtained is as follows
Table described successfully.ID - intProductName - varchar(255)CustomerName - varchar(255)DispatchDate - dateDeliveryTime - timePrice - intLocation - varchar(255)