MySQL: 3 ways to see the structure of a table

Updated: January 25, 2024 By: Guest Contributor Post a comment

Introduction

Understanding the structure of database tables is crucial for effective data management, and MySQL provides a variety of ways to examine table schema. In MySQL 8, you can view the structure using different techniques each suitable for various tasks. This tutorial covers several solutions for viewing table structure in MySQL 8.

Using DESCRIBE Statement

The DESCRIBE statement is a simple and quick way to view the basic structure of a table, providing a set of essential details for each column such as field type, nullability, default values, and primary or unique keys.

  1. Access your MySQL database using MySQL client tool or terminal.
  2. Type the DESCRIBE command followed by the table name.
  3. Press ENTER to execute the command.

Example:

DESCRIBE employees;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| first_name  | varchar(50)  | YES  |     | NULL    |                |
| last_name   | varchar(50)  | YES  |     | NULL    |                |
| hire_date   | date         | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

Notes: DESCRIBE is user-friendly and fast, but it shows limited information. It’s best for quick look-ups rather than in-depth analysis.

Using SHOW CREATE TABLE

The SHOW CREATE TABLE statement displays the SQL statement that creates the given table. This is helpful for developers and DBAs who want to replicate the table structure elsewhere or review the exact column definitions, keys, and constraints.

  1. Open MySQL client tool or terminal and connect to your database.
  2. Run the SHOW CREATE TABLE command followed by the table name.
  3. Review the output.

Example:

SHOW CREATE TABLE employees;
+--------------+--------------------------------------------------------------------+
| Table        | Create Table                                                       |
+--------------+--------------------------------------------------------------------+
| employees    | CREATE TABLE 'employees' (
'ID' int(11) NOT NULL AUTO_INCREMENT,
'first_name' varchar(255) DEFAULT NULL,
'last_name' varchar(255) DEFAULT NULL,
'hire_date' date DEFAULT NULL,
PRIMARY KEY ('ID')) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+--------------+--------------------------------------------------------------------+

Notes: This method reveals the complete table definition. It’s comprehensive but the output can be verbose for tables with many columns or complex constraints.

Using INFORMATION_SCHEMA.TABLES

Querying the INFORMATION_SCHEMA.TABLES view provides metadata about your tables. You can find properties such as table type, storage engine, row format, and table version among others.

  1. Establish a connection to MySQL through your preferred client or terminal.
  2. Use SELECT statement to retrieve table structure information from INFORMATION_SCHEMA.TABLES.
  3. Filter the result set to the table of interest using a WHERE clause.

Example:

SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE, ENGINE, TABLE_ROWS, CREATE_TIME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'your_database_name' AND TABLE_NAME = 'employees';

Notes: While this method does not show column-level information, it is excellent for obtaining an overview of table characteristics from a high-level perspective.

Conclusion

Understanding the structure of tables in MySQL is a fundamental skill for anyone working with databases. Whether you need a quick overview or a detailed account of table definitions, MySQL 8 provides you with multiple tools to get the job required done effectively. Choose the most appropriate method based on the depth of information needed and the specific context of your task.