IBM DB2 Query Toolbox - List tables with most relationships in Db2 database



Desenvolvido por DORNELLES Carlos Alberto - Analista de Sistemas - Brasília DF. - cad_cobol@hotmail.com

IBM Db2 Query Toolbox - List tables with most relationships in Db2 database

A consulta abaixo lista as tabelas com a maioria dos relacionamentos (tanto as chaves estrangeiras quanto as referências FK de outras tabelas).
Query below lists tables with most relationships (both foreign keys and FK references from other tables).

Consulta - Query

SELECT REFFK.TABLE
,      FOREIGN_KEYS + COALESCE(REFERENCES,0) AS RELATIONSHIPS
,      FOREIGN_KEYS
,      COALESCE(REFERENCES,0) AS REFERENCES
,      REFERENCED_TABLES + COALESCE(REFERENCING_TABLES,0) AS RELATED_TABLES
,      REFERENCED_TABLES
,      COALESCE(REFERENCING_TABLES,0) AS REFERENCING_TABLES
  FROM 
     ( SELECT TABSCHEMA CONCAT '.' CONCAT TABNAME AS TABLE
	   ,      COUNT(CONSTNAME) AS FOREIGN_KEYS
	   ,      COUNT(DISTINCT REFTABSCHEMA CONCAT '.' CONCAT REFTABNAME)
                                                   AS REFERENCED_TABLES 
         FROM SYSCAT.REFERENCES
        GROUP BY TABSCHEMA CONCAT '.' CONCAT TABNAME
     ) REFFK
     LEFT OUTER JOIN
     ( SELECT DISTINCT REFTABSCHEMA CONCAT '.' CONCAT REFTABNAME AS TABLE
	   ,      COUNT(*) AS REFERENCES
	   ,      COUNT(DISTINCT TABSCHEMA CONCAT '.' CONCAT TABNAME)
                                            AS REFERENCING_TABLES 
         FROM SYSCAT.REFERENCES
        GROUP BY REFTABSCHEMA CONCAT '.' CONCAT REFTABNAME
     ) REFBYFK ON REFFK.TABLE = REFBYFK.TABLE
  ORDER BY FOREIGN_KEYS + COALESCE(REFERENCES,0) DESC

Colunas

  • tabela - nome da tabela com nome do esquema
  • relacionamentos - número de relacionamentos (referências FKs e FK)
  • Foreign_keys - número de chaves estrangeiras em uma tabela
  • referências - número de referências FK de outras tabelas
  • related_tables - número de diferentes tabelas relacionadas (independentemente do tipo de relacionamento/direção da tabela é contada apenas uma vez)
  • referenced_tables - nubmer de tabelas diferentes referenciadas com FKs (observe que a tabela pode ser relacionada mais de uma vez, portanto, o número de FKs e o número de tabelas referenciadas podem ser diferentes)
  • referencing_tables - nubmer de diferentes tabelas referenciando com chaves estrangeiras esta tabela

Linhas

  • Uma linha representa uma tabela em um banco de dados
  • Escopo das linhas: todas as tabelas em um banco de dados
  • Ordenado pelo número de relacionamentos (chaves estrangeiras e referências) daqueles com mais

Columns

  • table - name of table with schema name
  • relationships - number of relationships (FKs and FK references)
  • foreign_keys - number of foreign keys in a table
  • references - number of FK references from other tables
  • related_tables - number of different related tables (regardless of relationship type/direction table is counted only once)
  • referenced_tables - nubmer of different tables referenced with FKs (please note that table can be related more than once so number of FKs and number of referenced tables can be different)
  • referencing_tables - nubmer of different tables referencing with foreign keys this table

Rows

  • One row represents one table in a database
  • Scope of rows: all tables in a database
  • Ordered by number of relationships (foreing keys and references) from the ones with the most

Resultado - Sample results


Copyright © Dataedo.