How to Check a Table in MySQL: Syntax and Examples
To check a table in MySQL, you can use the
SHOW TABLES command to list all tables in the current database or query the information_schema.tables to check for a specific table. Both methods help you verify if a table exists and view its presence quickly.Syntax
SHOW TABLES lists all tables in the current database. Use it to see all tables quickly.
Querying information_schema.tables lets you check if a specific table exists by filtering with the table name and database name.
mysql
SHOW TABLES; SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = 'your_database_name' AND TABLE_NAME = 'your_table_name';
Example
This example shows how to list all tables in the database and check if a table named employees exists.
mysql
USE company_db; SHOW TABLES; SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = 'company_db' AND TABLE_NAME = 'employees';
Output
Tables_in_company_db
--------------------
employees
departments
salaries
TABLE_NAME
----------
employees
Common Pitfalls
One common mistake is forgetting to select the correct database before running SHOW TABLES. Another is misspelling the database or table name when querying information_schema.tables, which will return no results even if the table exists.
Also, table names are case-sensitive on some systems, so check the exact spelling.
mysql
/* Wrong: No database selected */ SHOW TABLES; /* Right: Select database first */ USE company_db; SHOW TABLES;
Quick Reference
| Command | Description |
|---|---|
| SHOW TABLES; | Lists all tables in the current database |
| USE database_name; | Selects the database to work with |
| SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = 'db' AND TABLE_NAME = 'table'; | Checks if a specific table exists |
Key Takeaways
Use SHOW TABLES to quickly list all tables in the current database.
Query information_schema.tables to check for a specific table's existence.
Always select the correct database with USE before checking tables.
Table names can be case-sensitive depending on your system.
Double-check spelling of database and table names to avoid empty results.