Sling Academy
Home/MySQL/MySQL: 3 ways to see the structure of a table

MySQL: 3 ways to see the structure of a table

Last updated: January 25, 2024

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.

Next Article: Ways to Rename a Table in MySQL 8

Previous Article: Unsigned integer vs signed integer in MySQL 8: Explained with examples

Series: MySQL Tutorials: From Basic to Advanced

MySQL

You May Also Like

  • MySQL: How to reset the AUTO_INCREMENT value of a table
  • MySQL: How to add a calculated column to SELECT query
  • MySQL: Eliminate orphan rows in one-to-many relationships
  • MySQL: Using R-Tree Indexes for Spatial Data Types
  • How to Create Custom Collations in MySQL
  • Using Hash Indexes in MySQL: A Practical Guide
  • Understanding Full-Text Indexes in MySQL
  • Partial Indexes in MySQL: A Practical Guide
  • MySQL: How to Remove FOREIGN KEY Constraints
  • Using ENUM in MySQL 8: A Practical Guide (with Examples)
  • MySQL: Creating a Fixed-Size Table by Using Triggers
  • One-to-Many Relationship in MySQL 8: A Practical Guide
  • Using Regular Expressions in MySQL 8: The Complete Guide
  • Using Loops in MySQL: A Practical Guide (with Examples)
  • How to Execute an SQL File in VS Code
  • Making use of the JSON_REMOVE() function in MySQL 8
  • MySQL 8: How to count rows in related tables
  • Replication in MySQL 8: A Comprehensive Guide
  • MySQL 8: FIRST_VALUE(), LAST_VALUE(), and NTH_VALUE() functions – Explained with examples