TUTORIAL INDEX

MySQL DESC

|

What is the DESC Statement in MySQL and Why Do We Use It?

When you want to view the structure of a table in MySQL, you type the DESC table_name statement. The DESC statement is used to display the details of a table, such as column names, data types, NULL values, keys, and other table information.


Syntax:

DESC table_name;

Example:

DESC Students;

Output:

If the specified table exists, MySQL displays the structure of the table with the following information:

+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int          | NO   | PRI | NULL    |       |
| name  | varchar(50)  | YES  |     | NULL    |       |
| age   | int          | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+

If the specified table does not exist, MySQL displays the following error message:

ERROR 1146 (42S02): 
Table 'database_name.table_name' doesn't exist

How the DESC Statement Works Internally


When you want to view the structure of a table in MySQL, you type the DESC statement. After pressing the Enter key, the DESC statement is sent to the MySQL server. The server checks whether the specified table exists. If it exists, MySQL retrieves the table structure information and displays it to the user. If it does not exist, MySQL displays an error message.

Remember that the DESC statement does not create, delete, move, or modify any table or data. It only displays the structure and information of the selected table.

Flowchart:

👤 User
   |
   ▼
⌨️ DESC table_name;
   |
   ▼
🖥️ MySQL Server
   |
   ▼
🔍 Check Table Exists
   |
 ┌───────┴───────┐
 ▼               ▼
✅ Yes       ❌ No
 |               |
 ▼               ▼
📋 Display  ⚠️ Error
 Table          Message
 Structure

Precautions While Using DESC Statement:

  1. Before using the DESC statement, you should know the correct table name and its spelling. Otherwise, MySQL will display an error message.
  2. Before using the DESC statement, make sure that the table exists in the selected database.
  3. You should select the correct database before viewing the table structure.
  4. The DESC statement only displays table structure; it does not display the data stored inside the table.

How to Use the DESC Statement in MySQL: Step-by-Step Guide

If you use the DESC statement in MySQL, you first need to select the database that contains the required table. After selecting the database, you use the DESC statement to view the table structure. The complete process is as follows:

Step 1: You connect to the MySQL server by using the MySQL login command.

mysql -u username -p

Step 2: You select the required database by using the USE statement.

USE database_name;

Step 3: You type the DESC statement to view the table structure.

DESC table_name;

Step 4: After executing the statement, MySQL displays the structure and information of the selected table.

Note:

  • The DESC statement displays the structure of a table.
  • The DESC statement does not display the data stored inside the table.
  • The DESC statement can also be written as DESCRIBE table_name;

Don't Confuse DESC with SELECT Statement

Many beginners get confused between the DESC statement and the SELECT statement because both are used with tables. However, they perform different tasks. The DESC statement displays the structure and information about table columns, whereas the SELECT statement displays the data stored inside the table.

Therefore, when you want to view the table structure, use the DESC statement. If you want to view the records stored inside a table, use the SELECT statement.

ONLINE WEB TOOLS

WhatIsMyIpAddress
Shorterner