00:00

MySQL Show Tables

Last updated:

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:

show-tables.sql
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:

query.sql
SHOW FULL TABLES;

Result: 6 rows

Tables_in_retailTable_type
customer_addressesBASE TABLE
customersBASE TABLE
order_itemsBASE TABLE
ordersBASE TABLE
productsBASE TABLE
pune_customersVIEW

Filter to views only (or base tables only) with WHERE:

query.sql
SHOW FULL TABLES WHERE Table_type = 'VIEW';

Result: 1 row

Tables_in_retailTable_type
pune_customersVIEW

3. Filtering with LIKE

query.sql
SHOW TABLES LIKE 'customer%';

Result: 2 rows

Tables_in_retail (customer%)
customer_addresses
customers
query.sql
SHOW TABLES LIKE '%order%';

Result: 2 rows

Tables_in_retail (%order%)
order_items
orders
The 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):

query.sql
SHOW TABLES FROM warehouse;

Result: 2 rows

Tables_in_warehouse
racks
stock
Terminal
mysql -u root -p -e "SHOW TABLES FROM warehouse"

5. Errors

query.sql
SHOW TABLES FROM storeroom;
Output
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:

query.sql
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_NAMETABLE_TYPEENGINETABLE_ROWS
customer_addressesBASE TABLEInnoDB0
customersBASE TABLEInnoDB3
order_itemsBASE TABLEInnoDB0
ordersBASE TABLEInnoDB4
productsBASE TABLEInnoDB2
pune_customersVIEWNULLNULL
table_rows is an estimate for InnoDB (refreshed by ANALYZE TABLE). For an exact number use SELECT COUNT(*) FROM table.

Count tables per database:

query.sql
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_SCHEMAtables
retail5
warehouse2

7. Exact Row Counts for Every Table

Generate the counting query from metadata, then run it:

generate.sql
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
run-generated.sql
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

tbln
customer_addresses0
customers3
order_items0
orders4
products2

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:

query.sql
SHOW TABLE STATUS LIKE 'orders';

Result: 1 row

NameEngineRowsAvg_row_lengthData_lengthIndex_lengthCollation
ordersInnoDB44096163840utf8mb4_0900_ai_ci

9. Listing Tables from Java

ListTables.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.
solution.sql
SHOW FULL TABLES FROM retail WHERE Table_type = 'BASE TABLE' AND Tables_in_retail LIKE 'order%';

Result: 2 rows

Tables_in_retailTable_type
order_itemsBASE TABLE
ordersBASE TABLE
Exercise 2: Using information_schema, list the tables in retail that have a column named customer_id.
solution.sql
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 TABLES lists tables and views in the current database.
  • SHOW FULL TABLES adds Table_type (BASE TABLE or VIEW).
  • Filter with LIKE or WHERE; use FROM db for another database.
  • information_schema.tables and SHOW TABLE STATUS give engines, estimates and sizes.
  • Use COUNT(*) for exact row counts.