Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting number of fields in a database with an SQL Statement?

How would I get the number of fields/entries in a database using an SQL Statement?

like image 777
Ash Avatar asked Jun 23 '09 16:06

Ash


People also ask

How to get number of fields in mysql table?

Get number of fields in MySQL table? To display number of fields in MySQL, use the COUNT (*). Following is the syntax −

How to get list of all tables and fields in database?

Get list of all the tables and the fields in database: Select * From INFORMATION_SCHEMA.COLUMNS Where TABLE_CATALOG Like 'DatabaseName' Get list of all the fields in table: Select * From INFORMATION_SCHEMA.COLUMNS Where TABLE_CATALOG Like 'DatabaseName' And TABLE_NAME Like 'TableName'

How to see how many columns in a table in SQL?

The query is, Here, the table has 6 columns in it. To see the table use the ‘ DESC table_name ‘ query. If we use a Microsoft SQL server then we need to use ‘EXEC sp_help’ in place of DESC.

How do I get the number of rows in an SQL table?

SQL COUNT (*) example. To get the number of rows in the employees table, you use the COUNT (*) function table as follows: SELECT COUNT (*) FROM employees; See it in action. To find how many employees who work in the department id 6, you add the WHERE clause to the query as follows: SELECT COUNT (*) FROM employees WHERE department_id = 6;


1 Answers

mmm all the fields in all the tables? assuming standards (mssql, mysql, postgres) you can issue a query over information_schema.columns

  SELECT COUNT(*) 
  FROM INFORMATION_SCHEMA.COLUMNS 

Or grouped by table:

  SELECT TABLE_NAME, COUNT(*) 
  FROM INFORMATION_SCHEMA.COLUMNS 
  GROUP BY TABLE_NAME

If multiple schemas has the same table name in the same DB, you MUST include schema name as well (i.e: dbo.Books, user.Books, company.Books etc.) Otherwise you'll get the wrong results. So the best practice is:

SELECT TABLE_SCHEMA, TABLE_NAME, COUNT(*) 
FROM INFORMATION_SCHEMA.COLUMNS 
GROUP BY TABLE_SCHEMA, TABLE_NAME
like image 133
Jhonny D. Cano -Leftware- Avatar answered Oct 14 '22 15:10

Jhonny D. Cano -Leftware-