MySQL SHOW COLUMNS and DESCRIBE: List All Columns in a Table

MySQL SHOW COLUMNS and DESCRIBE: List All Columns in a Table

MySQL SHOW COLUMNS and DESCRIBE: List All Columns in a Table

Both the SHOW COLUMNS and DESCRIBE commands in MySQL display the structure of a table, including details about its columns such as names, data types, and constraints.

1. Using SHOW COLUMNS

The SHOW COLUMNS command provides detailed information about all columns in a table.

Syntax

SHOW COLUMNS FROM table_name;

Example

SHOW COLUMNS FROM employees;

Output
This will display a list of columns in the employees table, along with their data types, whether they can be NULL, any default values, and if they are part of a PRIMARY KEY, etc.

FieldTypeNullKeyDefaultExtra
idint(11)NOPRINULLauto_increment
first_namevarchar(100)YESNULL
last_namevarchar(100)YESNULL
hire_datedateYESNULL

2. Using DESCRIBE

The DESCRIBE command is essentially a shortcut for SHOW COLUMNS. It gives the same output, making it easy to quickly inspect the table's structure.

Syntax

DESCRIBE table_name;

Example

DESCRIBE employees;

Output
Similar to SHOW COLUMNS, it will list all columns in the employees table with their details.

FieldTypeNullKeyDefaultExtra
idint(11)NOPRINULLauto_increment
first_namevarchar(100)YESNULL
last_namevarchar(100)YESNULL
hire_datedateYESNULL

3. Key Differences Between SHOW COLUMNS and DESCRIBE

  • Functionality: Both SHOW COLUMNS and DESCRIBE do the same thing; they list column details for a given table.
  • Syntax: DESCRIBE is a shorthand for SHOW COLUMNS. It is a bit more convenient to use in simple queries.

4. Additional Information with SHOW TABLE STATUS

If you need more information about a table, such as its storage engine, number of rows, or when it was last updated, you can use the SHOW TABLE STATUS command.

Example

SHOW TABLE STATUS LIKE 'employees';

Output
This will show additional metadata about the employees table.

NameEngineVersionRow_formatRowsAvg_row_lengthData_lengthIndex_lengthData_freeAuto_incrementCreate_timeUpdate_time
employeesInnoDB10Compact100015015000050000020012023-01-01 12:00:002023-01-02 13:00:00

5. Conclusion

  • SHOW COLUMNS and DESCRIBE list all columns in a table along with their properties.
  • DESCRIBE is simply a shortcut for SHOW COLUMNS.
  • For more detailed table metadata, use SHOW TABLE STATUS.

šŸš€ Quickly inspect and analyze the structure of your tables with these commands!

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close