Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the number of columns in a table

Tags:

sql

mysql

People also ask

How do I find the number of columns in a SQL database?

SELECT count(*) as No_of_Column FROM information_schema. columns WHERE table_name ='geeksforgeeks'; Here, COUNT(*) counts the number of columns returned by the INFORMATION_SCHEMA . columns one by one and provides the final count of the columns.

How do I find the column size of a table in SQL?

Use COL_LENGTH() to Get a Column's Length in SQL Server In SQL Server, you can use the COL_LENGTH() function to get the length of a column. More specifically, the function returns the defined length of the column, in bytes. The function accepts two arguments: the table name, and the column name.


SELECT COUNT(*)
  FROM INFORMATION_SCHEMA.COLUMNS
 WHERE table_catalog = 'database_name' -- the database
   AND table_name = 'table_name'

SELECT COUNT(COLUMN_NAME) 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE 
    TABLE_CATALOG = 'Database name' 
    AND TABLE_SCHEMA = 'dbo' 
    AND TABLE_NAME = 'table name'

SELECT count(*)
FROM information_schema.columns
WHERE table_name = 'Your_table_name';

Note: Your_table_name should be replaced by your actual table name


Using JDBC in Java:

    String quer="SELECT * FROM sample2 where 1=2";
    
    Statement st=con.createStatement();
    ResultSet rs=st.executeQuery(quer);
    ResultSetMetaData rsmd = rs.getMetaData();
    int NumOfCol=0;
    NumOfCol=rsmd.getColumnCount();
    System.out.println("Query Executed!! No of Colm="+NumOfCol);

Its been little late but please take it from me...

In the editor(New Query) by select the database object it can be a table too, if we use the Shortcut Key Alt+F1 we will get all the information of the object and I think will solve your problem as well.


Well I tried Nathan Koop's answer and it didn't work for me. I changed it to the following and it did work:

SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'table_name'

It also didn't work if I put USE 'database_name' nor WHERE table_catalog = 'database_name' AND table_name' = 'table_name'. I actually will be happy to know why.