Number of Views and Tables In Each Database – MySQL

INFORMATION SCHEMA (also referred as system catalog) gives you access to database metadata and MySQL server information like the name of a database or table, the data type of a column, and access privileges.

We’ll look at how to get total number of views and tables from the database.

— Get all Tables and Views Count from a specific database
SELECT a.TABLE_SCHEMA, a.TABLE_TYPE, COUNT(*)
FROM INFORMATION_SCHEMA.TABLE a
JOIN INFORMATION_SCHEMA.VIEWS b ON a.TABLE_SCHEMA = b.TABLE_SCHEMA
WHERE a.TABLE_SCHEMA = ‘sometest’
GROUP BY a.TABLE_SCHEMA, a.TABLE_TYPE;

— Get all Views Count from a specific database
SELECT TABLE_SCHEMA, COUNT(TABLE_NAME)
FROM information_schema.VIEWS
WHERE TABLE_SCHEMA = ‘TestDB’
GROUP BY TABLE_SCHEMA;

— Get all Tables Count from a specific database
SELECT TABLE_SCHEMA, COUNT(TABLE_NAME)
FROM information_schema.tables
WHERE TABLE_TYPE = ‘BASE TABLE’
AND TABLE_SCHEMA = ‘TestDB’
GROUP BY TABLE_SCHEMA;

Happy learning!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s