Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Total number of fields in all tables in database

I have a huge database with hundreds of tables and I want to find out the total fields (columns) defined in all of the tables.

Is there a sql query that can give me that? If not, what would be the best way?

like image 334
zaf Avatar asked May 21 '11 15:05

zaf


People also ask

What is the total number of fields in the table?

To get total number of fields in all tables in database, you can use information_schema. columns along with aggregate function count(*).

How do I count all tables in a database?

To check the count of tables. mysql> SELECT count(*) AS TOTALNUMBEROFTABLES -> FROM INFORMATION_SCHEMA. TABLES -> WHERE TABLE_SCHEMA = 'business'; The following output gives the count of all the tables.


3 Answers

Is this what you want?

select count(*)
from information_schema.columns
where table_schema = 'your_schema'

You can run it like this to see if it is reasonable:

select table_name, column_name
from information_schema.columns
where table_schema = 'your_schema'
order by 1, 2
like image 63
MJB Avatar answered Oct 11 '22 05:10

MJB


Try this (while logged into your current schema):

select count(*) from information_schema.columns where table_schema = DATABASE();
like image 26
MikeTheReader Avatar answered Oct 11 '22 04:10

MikeTheReader


I am new in mysql but if table information_schema.columns is the table with table_name and column_name information then you can use following query

select table_name, count( distinct column_name ) column_number_used
from information_schema.columns 
where table_schema = 'your_schema' 
group by table_name

this should give all table names with respective column number used in that table..

like image 45
pratik garg Avatar answered Oct 11 '22 05:10

pratik garg