Describe Table MySQL

Learn via video courses
Topics Covered

How To Describe a Table In MySQL?

The DESCRIBE command in MySQL provides a detailed overview of a table's structure, including its column names, data types used, and any constraints applied to the columns. Since we have multiple tables in our database, we require a way to efficiently retrieve this information. The DESCRIBE command fulfills this need by providing a comprehensive breakdown of each table's characteristics.

The DESC and DESCRIBE statements are interchangeable and case-insensitive, and they will produce the same output. In this article, we will explore how to use the DESCRIBE command in MySQL to obtain a comprehensive breakdown of a table's characteristics and provide a demonstration of how to display table information in MySQL Workbench. It is important to note that the DESC command provides details on a table's columns, such as their name, data type, and constraints, without displaying any data stored in the table.

Syntax

The SQL DESCRIBE or DESC command is used to show the structure of a table and has the following syntax:

OR

Steps for Executing DESCRIBE Statement

To execute the DESCRIBE statement, you need to follow these steps:

1. Login to your MySQL database using your username and password. This will enable you to connect to the database server

The initial step is to log in to the database server using your username and password. The command for logging in is:

2. Choose the database containing the table you want to describe:

To execute the DESCRIBE statement, the second step is to choose the database that contains the table you want to describe by using the USE command. After executing the command, you should see a message indicating that the database has been successfully changed. Here's the query for this:

Here, you need to select the name of the database that you want to work with and replace database_name with that name.

3. Execute the DESCRIBE command in MySQL:

The last step is to use the syntax DESCRIBE table_name to display the table information. Prior to executing this statement, you can use the SHOW TABLES statement to show all tables stored in the selected database. The syntax for the DESCRIBE statement is as follows:

Explanation of Output of DESCRIBE Statement

Let us consider a table named student_marks. To find the structure of this table, we can use the DESC or DESCRIBE command. For instance, if the given table has three columns named ROLL_NO, NAME, and MARKS, we can obtain the same information by running the DESC command as shown below:

Output:

The output of the DESCRIBE command for the table student_marks provides information about the structure of the table. The output includes the following columns:

  1. Field:
    This column displays the names of the columns or fields in the table. In this case, the three columns in the student_marks table are ROLL_NO, NAME, and MARKS.
  2. Type:
    This column shows the data type of the column. In this example, the ROLL_NO and MARKS columns are of type int, while the NAME column is of type text.
  3. Null:
    This column indicates whether the column can contain null values or not. In this case, ROLL_NO does not allow null values.

How to Display Table Information in MySQL Workbench?

Here are the steps to follow in order to display table information in MySQL Workbench:

  1. Launch the MySQL Workbench tool and log in to the MySQL database server using your username and password.
  2. After successful login, go to the Navigation tab.
  3. In the navigation tab, click on the Schema menu.
  4. From the schema menu, select the database you want to display the table information. Here you will see multiple options displayed for databases as shown below: schema-menu-in-mysql-workbench
  5. To display all the tables stored in a database, click on the Tables option under the Schema menu in the Navigation tab. Select the table whose column information you want to display. Select the three icons menu as shown in the image below: schema menu in the navigation tab
  6. After clicking the icon located in the red rectangular box, the following image will be displayed: columns menu
  7. To view the table structure, click on the Columns menu and you will be able to see all the structure of a table. table structure

MySQL SHOW COLUMNS Command

In MySQL, you can use the SHOW COLUMNS command as an alternative to the DESCRIBE statement to retrieve information about a table's structure. Both commands can be used to retrieve information about a table's structure. The difference is that the DESCRIBE statement provides a more concise output, while the SHOW COLUMNS command provides a more detailed output with additional information. The SHOW COLUMNS command is known for its flexibility in providing column details.

The SHOW COLUMNS command follows the below syntax:

Here, table_name is the name of the table for which you want to retrieve column details.

For instance, running the below query would provide all column details of a table in a specific database:

The output of the command would typically display a table containing the column details such as Field (column name), Type (data type), Null (whether the column can contain null values or not), Key (column's key type), Default (column's default value), and Extra (additional information about the column).

employees table structure example

Conclusion

  • The DESCRIBE or DESC command in MySQL is a useful way to retrieve information about a table's structure, including column names, data types, and constraints.
  • To use the DESCRIBE command, you need to log in to the MySQL server, select the database, and execute the command with the name of the table you want to describe.
  • The output of the DESCRIBE command includes columns for the name, type, and nullability of each column in the table.
  • In addition to the DESCRIBE command, you can use the SHOW COLUMNS command to retrieve information about a table's structure, which provides a more detailed output with additional information.
  • MySQL Workbench provides a convenient GUI for viewing table information, including columns and their details.