Total number of fields in all tables in database?

MySQLMySQLi Database

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

We are using ‘sample’ database which consists of a lot of tables with fields. Following is the query to get total number of fields in all tables in database:

mysql> SELECT COUNT(*) AS TOTAL_NUMBER_OF_FIELDS
   -> FROM INFORMATION_SCHEMA.COLUMNS
   -> WHERE TABLE_SCHEMA = 'sample';

This will produce the following output

+------------------------+
| TOTAL_NUMBER_OF_FIELDS |
+------------------------+
| 796                    |
+------------------------+
1 row in set (0.04 sec)

Now, let us check another database ‘test’. Following is the query to get total number of fields in all tables in database:

mysql> SELECT COUNT(*) AS TOTAL_NUMBER_OF_FIELDS
   -> FROM INFORMATION_SCHEMA.COLUMNS
   -> WHERE TABLE_SCHEMA= 'test';

This will produce the following output

+------------------------+
| TOTAL_NUMBER_OF_FIELDS |
+------------------------+
| 1108                   |
+------------------------+
1 row in set (0.03 sec)
raja
Published on 17-Apr-2019 14:32:05
Advertisements