MySQL Show Tables
Once a database has a few tables, you need a quick way to see what is in it. SHOW TABLES lists the tables (and views) of a database. This tutorial shows the variations of the statement and how to get richer information such as table type, row counts and sizes.
1. Listing Tables in the Current Database
Select a database, then list its tables. The column is named Tables_in_ followed by the database name:
USE retail;
SHOW TABLES;
Result: 6 rows
| Tables_in_retail |
|---|
| customer_addresses |
| customers |
| order_items |
| orders |
| products |
| pune_customers |
Note that pune_customers is a view, but SHOW TABLES lists views too.
2. SHOW FULL TABLES: Tables vs Views
Add FULL to see a second column, Table_type:
SHOW FULL TABLES;
Result: 6 rows
| Tables_in_retail | Table_type |
|---|---|
| customer_addresses | BASE TABLE |
| customers | BASE TABLE |
| order_items | BASE TABLE |
| orders | BASE TABLE |
| products | BASE TABLE |
| pune_customers | VIEW |
Filter to views only (or base tables only) with WHERE:
SHOW FULL TABLES WHERE Table_type = 'VIEW';
Result: 1 row
| Tables_in_retail | Table_type |
|---|---|
| pune_customers | VIEW |
3. Filtering with LIKE
SHOW TABLES LIKE 'customer%';
Result: 2 rows
| Tables_in_retail (customer%) |
|---|
| customer_addresses |
| customers |
SHOW TABLES LIKE '%order%';
Result: 2 rows
| Tables_in_retail (%order%) |
|---|
| order_items |
| orders |
LIKE pattern applies to the table name; % means any characters and _ any single character.4. Listing Tables of Another Database
No need to switch with USE; add FROM (or IN):
SHOW TABLES FROM warehouse;
Result: 2 rows
| Tables_in_warehouse |
|---|
| racks |
| stock |
mysql -u root -p -e "SHOW TABLES FROM warehouse"
5. Errors
SHOW TABLES FROM storeroom;
ERROR 1049 (42000): Unknown database 'storeroom'
Without a selected database, plain SHOW TABLES fails with ERROR 1046 (3D000): No database selected.
6. Using information_schema.tables
For sorting, counting or joining, query the metadata table directly:
SELECT table_name, table_type, engine, table_rows
FROM information_schema.tables
WHERE table_schema = 'retail'
ORDER BY table_type, table_name;
Result: 6 rows
| TABLE_NAME | TABLE_TYPE | ENGINE | TABLE_ROWS |
|---|---|---|---|
| customer_addresses | BASE TABLE | InnoDB | 0 |
| customers | BASE TABLE | InnoDB | 3 |
| order_items | BASE TABLE | InnoDB | 0 |
| orders | BASE TABLE | InnoDB | 4 |
| products | BASE TABLE | InnoDB | 2 |
| pune_customers | VIEW | NULL | NULL |
table_rows is an estimate for InnoDB (refreshed by ANALYZE TABLE). For an exact number use SELECT COUNT(*) FROM table.Count tables per database:
SELECT table_schema, COUNT(*) AS tables
FROM information_schema.tables
WHERE table_schema IN ('retail', 'warehouse') AND table_type = 'BASE TABLE'
GROUP BY table_schema;
Result: 2 rows
| TABLE_SCHEMA | tables |
|---|---|
| retail | 5 |
| warehouse | 2 |
7. Exact Row Counts for Every Table
Generate the counting query from metadata, then run it:
SELECT GROUP_CONCAT(
CONCAT('SELECT ''', table_name, ''' AS tbl, COUNT(*) AS n FROM retail.', table_name)
SEPARATOR ' UNION ALL ') AS generated_sql
FROM information_schema.tables
WHERE table_schema = 'retail' AND table_type = 'BASE TABLE';
Result: 1 row
| generated_sql |
|---|
| SELECT 'customer_addresses' AS tbl, COUNT(*) AS n FROM retail.customer_addresses UNION ALL SELECT 'customers' AS tbl, COUNT(*) AS n FROM retail.customers UNION ALL SELECT 'order_items' AS tbl, COUNT(*) AS n FROM retail.order_items UNION ALL SELECT 'orders' AS tbl, COUNT(*) AS n FROM retail.orders UNION ALL SELECT 'products' AS tbl, COUNT(*) AS n FROM retail.products |
SELECT 'customer_addresses' AS tbl, COUNT(*) AS n FROM retail.customer_addresses UNION ALL
SELECT 'customers', COUNT(*) FROM retail.customers UNION ALL
SELECT 'order_items', COUNT(*) FROM retail.order_items UNION ALL
SELECT 'orders', COUNT(*) FROM retail.orders UNION ALL
SELECT 'products', COUNT(*) FROM retail.products;
Result: 5 rows
| tbl | n |
|---|---|
| customer_addresses | 0 |
| customers | 3 |
| order_items | 0 |
| orders | 4 |
| products | 2 |
8. SHOW TABLE STATUS
SHOW TABLE STATUS returns one detailed row per table: engine, row estimate, average row length, data and index size, auto-increment value, collation and more:
SHOW TABLE STATUS LIKE 'orders';
Result: 1 row
| Name | Engine | Rows | Avg_row_length | Data_length | Index_length | Collation |
|---|---|---|---|---|---|---|
| orders | InnoDB | 4 | 4096 | 16384 | 0 | utf8mb4_0900_ai_ci |
9. Listing Tables from Java
try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/retail", "root", "your-password");
ResultSet rs = con.getMetaData().getTables("retail", null, "%", new String[] {"TABLE", "VIEW"})) {
while (rs.next()) {
System.out.println(rs.getString("TABLE_NAME") + " - " + rs.getString("TABLE_TYPE"));
}
}
10. Practice Exercises
Exercise 1: List only the base tables (no views) in retail whose name starts with order.
SHOW FULL TABLES FROM retail WHERE Table_type = 'BASE TABLE' AND Tables_in_retail LIKE 'order%';
Result: 2 rows
| Tables_in_retail | Table_type |
|---|---|
| order_items | BASE TABLE |
| orders | BASE TABLE |
Exercise 2: Using information_schema, list the tables in retail that have a column named customer_id.
SELECT table_name FROM information_schema.columns
WHERE table_schema = 'retail' AND column_name = 'customer_id';
Result: 2 rows
| TABLE_NAME |
|---|
| customer_addresses |
| orders |
11. Summary
SHOW TABLESlists tables and views in the current database.SHOW FULL TABLESaddsTable_type(BASE TABLE or VIEW).- Filter with
LIKEorWHERE; useFROM dbfor another database. information_schema.tablesandSHOW TABLE STATUSgive engines, estimates and sizes.- Use
COUNT(*)for exact row counts.