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
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:
Tutorial:
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 the database containing the table whose structure you want to display:
USE [database_name];
Replace [database_name]
with the name of your database.
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;
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.
MySQL display table structure command:
DESCRIBE
or SHOW COLUMNS
statement to display the structure of a table.DESCRIBE table_name; -- or SHOW COLUMNS FROM table_name;
How to show table structure in MySQL:
DESCRIBE
or SHOW COLUMNS
command to show the structure of a table.DESCRIBE table_name; -- or SHOW COLUMNS FROM table_name;
Viewing table schema in MySQL:
DESCRIBE
statement.DESCRIBE table_name;
List columns and data types in MySQL table:
DESCRIBE
or SHOW COLUMNS
to list columns and their data types.DESCRIBE table_name; -- or SHOW COLUMNS FROM table_name;
SHOW COLUMNS in MySQL for table structure:
SHOW COLUMNS
statement provides information about the columns in a table.SHOW COLUMNS FROM table_name;
Displaying table information in MySQL:
SHOW TABLE STATUS
statement.SHOW TABLE STATUS LIKE 'table_name';
MySQL describe table example:
DESCRIBE
:DESCRIBE employees;
Getting table metadata in MySQL:
INFORMATION_SCHEMA.COLUMNS
table.SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'employees';
Viewing indexes in a MySQL table:
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';
Displaying table comments in MySQL:
INFORMATION_SCHEMA.TABLES
.SELECT TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'table_name';