Home > Enterprise >  SQL Query to list all the tables
SQL Query to list all the tables

Time:10-04

I want to write a query to display the list of tables in a specific database along with its table type and number of attributes for that table.

For Example,

enter image description here

Can anyone help me to get it resolved.

CodePudding user response:

Sure, you can query systems tables like that :

SELECT TABLE_NAME,TABLE_ROWS,TABLE_TYPE 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_SCHEMA='DATABASENAME'

Change DATABASENAME for your db name. There is many others options you can display, check :

SELECT *
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_SCHEMA='DATABASENAME'

EDIT : Version with JOIN so we can count columns

SELECT C.TABLE_NAME,T.TABLE_ROWS,T.TABLE_TYPE, count(C.table_name) AS COLUMNS_NUMBER
FROM INFORMATION_SCHEMA.TABLES AS T
INNER JOIN INFORMATION_SCHEMA.COLUMNS AS C 
ON C.TABLE_SCHEMA = T.TABLE_SCHEMA AND  T.TABLE_NAME = C.TABLE_NAME
WHERE T.TABLE_SCHEMA='DATABASENAME'
GROUP BY C.TABLE_NAME, T.TABLE_SCHEMA,T.TABLE_ROWS,T.TABLE_TYPE
  • Related