简体   繁体   中英

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?

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

试试这个(登录到当前架构时):

select count(*) from information_schema.columns where table_schema = DATABASE();

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..

Below example may works and modify queries as per your requirement.

Use [Your_DB_Name]

/* Count Total Number Of Tables */
SELECT COUNT(*) AS 'Total Tables' FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'

/* Count Total Number Of Views */
SELECT COUNT(*)  AS 'Total Views'  FROM INFORMATION_SCHEMA.VIEWS

/* Count Total Number Of Stored Procedures */
SELECT COUNT(*)  AS 'Total SPs'  FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = 'PROCEDURE'

/* Count Total Number Of UDF(User Defined Functions) */
SELECT COUNT(*)  AS 'Total Functions' FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = 'FUNCTION'

Example:

在此输入图像描述

Try this:

SELECT COUNT(*) 
FROM INFORMATION_SCHEMA.COLUMNS

You can get the queries you need from this open source project called anywhereindb . The final result of this project goes further than you need, but you can look into the code and take out the part where it figures out all the parts of the tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM