Вопрос

I have been trying to find a way to join the all_tab_columns and all_cons_columns in Oracle so I can extract the information of the number of columns of every data type in the schema and also the number of constraints those columns take part in. I was able to extract the information regarding the table name and the number of columns of each of the data types with the following query:

SELECT DISTINCT atc.TABLE_NAME, 
       atc.DATA_TYPE, 
       COUNT(atc.DATA_TYPE)
  FROM all_tab_columns atc
 WHERE atc.OWNER = 'PARRANDEROS'
 GROUP BY atc.DATA_TYPE, 
          atc.TABLE_NAME

But when trying to add the info from all_cons_columns everything gets confusing. This is the query that came to mind but the results make no sense:

SELECT DISTINCT atc.TABLE_NAME, 
                atc.DATA_TYPE, 
                COUNT(acc.COLUMN_NAME), 
                COUNT(atc.DATA_TYPE)
 FROM all_tab_columns atc 
INNER JOIN all_cons_columns acc 
   ON acc.COLUMN_NAME = atc.COLUMN_NAME AND 
      acc.TABLE_NAME = atc.TABLE_NAME
WHERE atc.OWNER = 'PARRANDEROS'
GROUP BY atc.DATA_TYPE, 
         atc.TABLE_NAME, 
         acc.COLUMN_NAME

Please tell me if I'm not clear enough. I'm really new to SQL but have been trying my best to find a solution to this.

Это было полезно?

Решение

I don't understand what theis information will bring you, maybe if you explain what you intend to get we can help in a better way. Anyway, I think the select you want is this one. Hope this helps.

SELECT atc.TABLE_NAME, 
       atc.DATA_TYPE, 
       count(distinct atc.column_name) cols, 
       count(distinct acc.constraint_name) cons
 FROM all_tab_columns atc 
INNER JOIN all_cons_columns acc 
   ON acc.COLUMN_NAME = atc.COLUMN_NAME AND 
      acc.TABLE_NAME = atc.TABLE_NAME
WHERE atc.OWNER = 'PARRANDEROS'
GROUP BY atc.DATA_TYPE, 
         atc.TABLE_NAME;
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top