JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

Display tables and database structure in MySQL

Author:JIYIK Last Updated:2025/04/23 Views:

Today, we will learn about queries in MySQL that can display the table and database structure. We will use the mysqldump utility, DESCRIBEthe , SHOW TABLESand SHOW CREATE TABLEthe statements.

We are using MySQL version 8.0.28 while writing this tutorial.


Display table structure in MySQL

We can use different ways to get the table structure in MySQL according to our requirement. Some of them are given below.

Using the mysqldump utility in MySQL

We can use the mysqldump command-line tool to dump the complete database structure as a set of all CREATE TABLE statements.

Sample code:

# The `--compact` flag produces the compact output
mysqldump -u username -p ms23 --compact --no-data

For a specific table, we can add the name of the table after the database name, or use SHOW CREATE TABLEthe statement as shown below to achieve a similar result.

Sample code:

# To use this, you must be logged in to MySQL Server and a database.
SHOW CREATE TABLE courses;

We can use the DESCRIBE statement to get the list of columns discussed below.

Using the DESCRIBE (table) statement in MySQL

Sample code:

DESCRIBE courses;

We have to log in to MySQL server to use DESCRIBE statement. It displays the column list of a particular table such as attribute (field) names, their data types, default values, keys, etc.

Alternatively, we can use it as DESC CLASS; or EXPLAIN CLASS; and get the same result. If we forget the table name, we can use SHOW TABLES; statement to get a list of tables in a particular database.

Sample code:

SHOW TABLES;

Use mysqldump to display the database structure in MySQL

We use the command line tool called mysqldump to dump or create a copy or backup of the MySQL database. While using the mysqldump command, we use multiple flags as required.

For example, if we do not want to include the data while copying the database structure, we can use the --no-data flag. Let's use mysqldump to get the database structure only by considering the following scenario.

  • Get the database structure of only one table
  • Get only the database structure of multiple tables
  • Get database structure only for one or more databases
  • Get the database structure of all databases into a file

If you installed MySQL server in the default location, you must be in C:\Program Files\MySQL\MySQL Server 8.0\bin>the directory to use the following commands.

In the following commands, we have used various flags which you must understand before using these commands.

  1. The -u flag indicates the user name.
  2. -p flat means that the password will be provided when asked (you can also type the password using the -p flag without spaces, for example -p12345, but this is not recommended for security reasons).
  3. If we don't need to include data, we use --no-data. Alternatively, we can also use -d here.
  4. If we want to backup one or more databases, use --database.
  5. We use --all-databases to back up all databases at once.
  6. The greater than sign (>) saves the backup file. To restore, you can use the less than sign (<).

Sample code (only gets the database structure of one table):

mysqldump -u username -p --no-data DatabaseName TableName;

Sample code (only get the database structure of multiple tables):

mysqldump -u username -p --no-data DatabaseName TableName1 TableName2 TableNameN;

Sample code (only get the database structure of one/multiple databases):

# `-d` is used as an alternative to the `--no-data` option
mysqldump -u username -p -d --databases DatabaseName

We have learned how to dump the database structure to standard output. This means that we will have to scroll up and down in the terminal (command line window), which may not be helpful.

We can save the backup without data to a file using >. We can also do this using the command given above.

Sample code (get the database structure of all databases into one file):

mysqldump -u username -p --no-data --all-databases > E:\\Databases_Structure.sql

If you want to dump a remote database, read this article to find all the necessary information.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Converting from datetime type to date-only in MySQL

Publish Date:2025/04/23 Views:199 Category:MySQL

Today, we will learn the DATE(), CAST(), CONVERT() and DATE_FORMAT() methods to convert DATETIME type to DATE type in MySQL. The above mentioned methods can be used in MySQL 4.0 and above. Converting from DATETIME to DATE in MySQL To unders

Changing max_allowed_packet Size in MySQL Server

Publish Date:2025/04/22 Views:193 Category:MySQL

This article explains how to change the max_allowed_packet size in MySQL server. To understand this, we will use two operating systems, Windows 10 and Linux (Ubuntu). Changing max_allowed_packet Size in MySQL Server If we try to upload a fi

Zerofill usage, advantages and alternatives in MySQL

Publish Date:2025/04/22 Views:196 Category:MySQL

In this article we will understand the uses, advantages and alternatives of ZEROFILL attribute in MySQL. Use and benefits of the ZEROFILL attribute in MySQL The benefit of using the ZEROFILL attribute is that it has nothing to do with input

Compare only MySQL timestamp dates to date parameters

Publish Date:2025/04/22 Views:64 Category:MySQL

In this article we will use the DATE() , CAST() , and CONVERT() functions to compare MySQL timestamp dates with only the date parameter. DATE() vs. CAST() vs. CONVERT() in MySQL Below is a brief description of each function. You can also fi

Calculating Percentages in MySQL

Publish Date:2025/04/22 Views:67 Category:MySQL

We will use one or more columns to calculate percentages in MySQL. There are different ways to do this, and for each method we will use an example table. Calculate percentage using a column in MySQL We have a table called sales where ID, Re

Selecting multiple values using WHERE in MySQL

Publish Date:2025/04/22 Views:186 Category:MySQL

This article is about using MySQL query to get data from a specific table or relation that satisfies a specific condition. To do this, the WHERE clause is used in the SQL query. WHERE clause in SQL query WHERE The clause specifies the condi

Changing the connection timeout in MySQL

Publish Date:2025/04/22 Views:59 Category:MySQL

We are learning how to change the connection timeout in MySQL using Linux (Ubuntu 20.04) and Windows operating systems. Changing the connection timeout in MySQL Sometimes you keep losing connection to the MySQL server because the connect_ti

MySQL fix Data Is Truncated for a Column error

Publish Date:2025/04/22 Views:102 Category:MySQL

This article describes possible causes and solutions for the MySQL error Data is truncated for a column . Fix data truncated due to column error in MySQL Here, we will discuss the possible causes and solutions to eliminate MySQL data trunca

MySQL Error Server PID File Could Not Be Found Solution

Publish Date:2025/04/22 Views:193 Category:MySQL

In this article, we will study about the Error! Error Server PID File Could Not Be Found! in MySQL and its solution with full explanation. MySQL PID file The file that contains the process identification number or process ID of a running My

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial