MySQL Tutorial

MySQL Installation and Configuration

MySQL Database Operations

Database Design

MySQL Data Types

MySQL Storage Engines

MySQL Basic Operations of Tables

MySQL Constraints

MySQL Operators

MySQL Function

MySQL Manipulate Table Data

MySQL View

MySQL Indexes

MySQL Stored Procedure

MySQL Trigger

MySQL Transactions

MySQL Character Set

MySQL User Management

MySQL Database Backup and Recovery

MySQL Log

MySQL Performance Optimization

MySQL Display Table Structure

In this tutorial, you'll learn how to display the structure of a table in a MySQL database. This can help you understand the schema, data types, and constraints of a table.

Prerequisites:

  • A MySQL server up and running
  • Access to a MySQL user account with privileges to query tables

Tutorial:

  • Connect to the MySQL server:

To start the mysql command-line client, open a terminal or command prompt, and enter:

mysql -u [username] -p

Replace [username] with your MySQL username and enter your password when prompted.

  • Select a database:

Select the database containing the table whose structure you want to display:

USE [database_name];

Replace [database_name] with the name of your database.

  • Display the table structure:

To display the structure of a table, use the DESCRIBE command or its shorthand version DESC:

DESCRIBE [table_name];

Replace [table_name] with the name of the table whose structure you want to display. For example, if you have a users table and want to see its structure, run:

DESCRIBE users;

The output will show you the table structure, including column names, data types, constraints (such as NOT NULL or AUTO_INCREMENT), and keys (such as PRIMARY KEY).

Alternatively, you can use the SHOW CREATE TABLE statement to display the table's structure in the form of a CREATE TABLE statement:

SHOW CREATE TABLE [table_name];

Replace [table_name] with the name of the table whose structure you want to display. This command will provide you with the SQL query used to create the table, including all column definitions, indexes, and constraints.

  • Exit the MySQL command-line client:
EXIT;

Now you know how to display the structure of a table in a MySQL database using the DESCRIBE and SHOW CREATE TABLE statements. These commands are useful for understanding the schema of a table, debugging, or generating SQL scripts to recreate a table's structure in another database.

  1. MySQL display table structure command:

    • Use the DESCRIBE or SHOW COLUMNS statement to display the structure of a table.
      DESCRIBE table_name;
      -- or
      SHOW COLUMNS FROM table_name;
      
  2. How to show table structure in MySQL:

    • Use the DESCRIBE or SHOW COLUMNS command to show the structure of a table.
      DESCRIBE table_name;
      -- or
      SHOW COLUMNS FROM table_name;
      
  3. Viewing table schema in MySQL:

    • The table schema can be viewed using the DESCRIBE statement.
      DESCRIBE table_name;
      
  4. List columns and data types in MySQL table:

    • Use DESCRIBE or SHOW COLUMNS to list columns and their data types.
      DESCRIBE table_name;
      -- or
      SHOW COLUMNS FROM table_name;
      
  5. SHOW COLUMNS in MySQL for table structure:

    • The SHOW COLUMNS statement provides information about the columns in a table.
      SHOW COLUMNS FROM table_name;
      
  6. Displaying table information in MySQL:

    • Retrieve detailed information about a table using the SHOW TABLE STATUS statement.
      SHOW TABLE STATUS LIKE 'table_name';
      
  7. MySQL describe table example:

    • Example using DESCRIBE:
      DESCRIBE employees;
      
  8. Getting table metadata in MySQL:

    • Access table metadata from the INFORMATION_SCHEMA.COLUMNS table.
      SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'employees';
      
  9. Viewing indexes in a MySQL table:

    • Use SHOW INDEX or query INFORMATION_SCHEMA.STATISTICS to view indexes.
      SHOW INDEX FROM table_name;
      -- or
      SELECT * FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_NAME = 'table_name';
      
  10. Displaying table comments in MySQL:

    • Retrieve table comments from INFORMATION_SCHEMA.TABLES.
      SELECT TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'table_name';