Get table names using SELECT statement in MySQL
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In MySQL, there's often a need to retrieve a list of all table names from a specific database. This can be crucial for database management, reporting, or automation tasks. Although MySQL does not provide a direct SELECT statement to get table names, there are straightforward methods to achieve this goal.
Accessing Table Names in MySQL
MySQL stores metadata about databases, tables, and other objects in a special database called information_schema. You can use a SELECT statement to query this database in order to obtain table names.
Example Query to Get Table Names
Here's a basic query that retrieves table names from a specific database:
information_schema.tables: This is the table that contains information about all tables in all databases.table_schema: This column includes the name of the database. You need to filter it to match your desired database.table_name: This column contains the name of the table.
Step-by-step Breakdown
- Choosing the Schema: Ensure that you're querying the correct
table_schemavalue. If your database's name ismy_database, replace'your_database_name'with'my_database'. - Selecting Data: The
SELECT table_namecommand is used to fetch the column that contains table names. - Filtering Criteria: Apply a
WHEREclause to ensure only tables from the specified database are listed.
Adding Conditions
If you need to filter the result further, for example, to get tables with names starting with 'temp', you can adjust the query with the LIKE operator:
Query for Specific Table Types
Sometimes, you might need to filter tables based on their type, such as base tables or views. TABLE_TYPE is an attribute in information_schema.tables that can be used:
BASE TABLE: Refers to regular tables.VIEW: Refers to views.
Include Other Metadata
If you seek more information alongside table names, such as their creation time or table comment, modify the SELECT clause:
This will fetch the table name, creation time, and any comments about the table.
Using MySQL Workbench
For users using MySQL Workbench, a GUI tool for MySQL, you can visually retrieve table names:
- Open Workbench and connect to the desired MySQL server.
- Navigate to the
Schemastab. - Expand the database to view its tables.
Script for Automation
For automation, you might consider writing a script using a language like Python with a MySQL connector to fetch and process table names programmatically:
This Python script uses the mysql.connector library to connect and execute the SQL query, printing out each table name.
Summary Table
Below is a table summarizing key components related to fetching table names in MySQL:
| Component | Description |
information_schema | Database storing metadata |
tables | Table holding information about tables |
table_schema | Column to filter by database name |
table_name | Column containing the table's name |
table_type | Column to filter by table type |
create_time | Metadata on table creation time |
table_comment | Descriptive comments about a table |
Understanding how to list tables in a database is vital for managing databases efficiently. Whether you're managing a simple dataset or a complex enterprise-grade database, knowing how to extract key metadata using SQL can significantly enhance your database operations.

