Open In App

How to Show/List Tables in MySQL Database

Last Updated : 24 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In MySQL, the SHOW TABLES command is a powerful tool used to list the tables within a specific database. This command provides a convenient way to view the tables that exist in a database without needing to query the database schema directly.

In this article, we are going to explore various ways where we can show tables and list down in multiple ways to modify tables on user requirements and so on.

MySQL Show/List Tables

In MySQL, The SHOW TABLES command is used to list the tables in a specific database. It provides a simple way to see the tables that exist within a database without having to query the database schema directly. The command returns a result set containing the names of all tables in the selected database. This is particularly useful when managing databases with multiple tables, as it allows us to quickly view the available tables and their names.

Syntax:

SHOW TABLES;

MySQL returns the results in a table with one column — Tables_in_DatabaseName. The tables are ordered in alphabetical order. The summary line tells us how many rows (or tables) are there in the database.

Steps to Get the List of Tables

Given below are the necessary steps to get the list of tables:

Step 1: Open the MySQL Command Line or MSI installer. Now Log in to the MySQL database server with the help of password. Now, we are connected to the MySQL server, where we can execute all the SQL statements.

Step 2: Next, choose the required database by using the following command :

USE DATABASE_NAME;

Step 3: Finally, execute the SHOW TABLES command.

Let us understand this with an example. Suppose, we have a database name “northwind” that contains many tables. Now run the following statement to list the tables it includes:

USE northwind;
SHOW TABLES;

Output:

table

Tables in database

The query first selects the “northwind” database as the current database and then lists all the tables in that database.

SHOW FULL TABLES statement

The SHOW TABLES command allows us to show a table that is either a base table or a view. To also include the table type in the result, we can use the following command:

SHOW FULL TABLES;

Output:

full

Full Table

If we want to show or list the table name from different databases or database to which we are not connected without switching then, MySQL allows us to use the FROM or IN clause followed by the database name. The following statement explains it more clearly:

SHOW TABLES FROM northwind;  
OR,
SHOW TABLES IN northwind;

Output:

in

Using IN command

Show Tables Using Pattern Matching

There might be huge databases stored on our server. For a database that has many tables, listing all tables at a time may not be intuitive. In such situations, we can use the LIKE expression with MySQL’s SHOW TABLES command to filter the list and only display tables that match a specific pattern.

The SHOW TABLES command in MySQL allow us to filter the displayed tables using patterns with the LIKE and WHERE clauses.

SHOW TABLES LIKE pattern;  
OR,
SHOW TABLES WHERE expression;

Example 1: Using LIKE Pattern

Suppose, we want the statement to return only the names of those databases that begin with the letter ‘C’. The query will look as follows:

SHOW TABLES LIKE 'C%';

Output:

l1

Using “C%”

Let us see another statement that returned the table names starting with “Ord%”, where percent (%) sign assumes zero, one, or multiple characters:

SHOW TABLES LIKE 'Ord%';

Output:

like

Using “Ord%”

Example 2: Using WHERE clause

This statement explains using the WHERE clause in the SHOW TABLES statement to list all the views in the northwind database.

SHOW TABLES FROM northwind WHERE Table_type= "BASE TABLE";  

Output:

full

using WHERE clause

If MySQL doesn’t allow access to a base table or view, then those tables won’t appear in the result when using the SHOW TABLES command.

Here, we can also see another example of Show Tables statement with the WHERE clause:

SHOW TABLES In northwind  WHERE Tables_in_northwind = "Customer";  

Output:

where

Table records

Conclusion

Overall, We can use the SHOW TABLE statement to list all tables in a database. Using the SHOW FULL TABLE statement to return an additional column that indicates the object is a view or table. We can also show SHOW TABLE FROM statement to list tables in a database. Lastly, using the SHOW TABLE WHERE statement or SHOW TABLE LIKE statement to filter the tables in a database.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads