Number of columns

前端 未结 5 586

how do you count the number of columns in a table in oracle?

相关标签:
5条回答
  • 2020-12-29 14:33
    SELECT count(*) FROM user_tab_columns WHERE table_name = 'FOO'
    

    should give you the number of columns in foo. You can obtain quite a bit of information from USER_TAB_COLUMNS and USER_TABLES (there are also ALL_ and DBA_ variants).

    0 讨论(0)
  • 2020-12-29 14:36

    @derobert has a good answer, as long as you are trying to count the columns in a table you own. If you need to count columns in another schema's tables, you'll need to use the all_tab_columns view. One of the additional columns in this view is the table owner. This is also useful when the same tablename exists in multiple schemas. Note that you must have privileges on the tables in order to see them in the all_tab_columns view. The query becomes:

    select count(*) from all_tab_columns where owner='BAR' and table_name='FOO';
    

    Note the owner and tablename columns are typically upper case.

    0 讨论(0)
  • 2020-12-29 14:43

    Number of column and number of rows in oracle table:

    SELECT u.table_name Table_Name, 
           Count(*)     Table_Columns, 
           u.num_rows   Table_Rows 
    FROM   user_tab_columns c, 
           user_tables u 
    WHERE  u.table_name = c.table_name 
    GROUP  BY u.table_name, 
              u.num_rows 
    
    0 讨论(0)
  • 2020-12-29 14:44

    Old question - but I recently needed this along with the row count... here is a query for both - sorted by row count desc:

    SELECT t.owner, 
           t.table_name, 
           t.num_rows, 
           Count(*) 
    FROM   all_tables t 
           LEFT JOIN all_tab_columns c 
                  ON t.table_name = c.table_name 
    WHERE  num_rows IS NOT NULL 
    GROUP  BY t.owner, 
              t.table_name, 
              t.num_rows 
    ORDER  BY t.num_rows DESC; 
    
    0 讨论(0)
  • 2020-12-29 14:51

    If Oracle supported INFORMATION_SCHEMA.COLUMNS, I'd say use that. But as others have said, use the USER_% views.

    For completeness, the following link describes what systems support the SQL-92 Standard. Systems that support INFORMATION_SCHEMA

    0 讨论(0)
提交回复
热议问题