IBM DB2 Query Toolbox - List table columns with their foreign keys



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

IBM Db2 Query Toolbox - List table columns with their foreign keys in Db2 database

A consulta abaixo retorna todas as colunas de todas as tabelas em um banco de dados com uma referência de chave estrangeira se a coluna tiver uma.
Query below returns all columns from all tables in a database with a foreign key refererence if column has one.

Consulta - Query

SELECT COL.TABSCHEMA CONCAT '.' CONCAT COL.TABNAME AS TABLE_NAME
,      COL.COLNO AS COLUMN_ID
,      COL.COLNAME AS COLUMN_NAME
,      CASE WHEN FK_CONSTRAINT_NAME IS NOT NULL THEN '>-' ELSE NULL END AS REL
,      CONST.PRIMARY_TABLE
,      CONST.PK_COLUMN_NAME
,      CONST.NO
,      CONST.FK_CONSTRAINT_NAME    
  FROM SYSCAT.COLUMNS COL
       INNER JOIN SYSCAT.TABLES TAB ON 
             TAB.TABSCHEMA = COL.TABSCHEMA AND TAB.TABNAME = COL.TABNAME
       LEFT OUTER JOIN 
   (   SELECT REF.TABSCHEMA
       ,      REF.TABNAME
	   ,      REF.CONSTNAME AS FK_CONSTRAINT_NAME
	   ,      KEY.COLNAME  AS FK_COLUMN_NAME
	   ,      REF.REFTABSCHEMA CONCAT '.' CONCAT REF.REFTABNAME AS PRIMARY_TABLE
	   ,      KEYPK.COLNAME AS PK_COLUMN_NAME
	   ,      REF.PK_COLNAMES
	   ,      KEY.COLSEQ  AS NO
         FROM SYSCAT.REFERENCES REF
              LEFT OUTER JOIN SYSCAT.KEYCOLUSE KEY ON 
                   KEY.TABSCHEMA = REF.TABSCHEMA AND KEY.TABNAME = REF.TABNAME 
               AND KEY.CONSTNAME = REF.CONSTNAME
              LEFT OUTER JOIN SYSCAT.KEYCOLUSE KEYPK ON 
                   KEYPK.TABSCHEMA = REF.REFTABSCHEMA 
               AND KEYPK.TABNAME = REF.REFTABNAME 
               AND KEYPK.CONSTNAME = REF.REFKEYNAME 
               AND KEYPK.COLSEQ=KEY.COLSEQ 
   )      CONST ON CONST.TABSCHEMA = COL.TABSCHEMA AND CONST.TABNAME = COL.TABNAME 
            AND CONST.FK_COLUMN_NAME = COL.COLNAME
 WHERE TAB.TYPE = 'T'
   AND TAB.TABSCHEMA NOT LIKE 'SYS%'  
 ORDER BY TABLE_NAME, COLUMN_ID

Colunas

  • tabela - tabela em um banco de dados com nome de esquema
  • column_id - número de colunas em um banco de dados
  • column_name - nome da coluna
  • rel - símbolo de relacionamento ('> -') indicando chave estrangeira e direção
  • tabela_primária - tabela referenciada
  • pk_column_name - coluna referenciada
  • não - id da coluna em uma restrição de chave
  • fk_constraint_name - nome de restrição de chave estrangeira

Linhas

  • Uma linha representa uma coluna de cada tabela em um banco de dados
  • Escopo das linhas: todas as colunas de todas as tabelas em um banco de dados
  • Ordenado por esquema e nome da tabela, id da coluna em uma tabela

Columns

  • table - table in a database with schema name
  • column_id - number of column in a database
  • column_name - column name
  • rel - relationship symbol ('>-') indicating foreign key and direction
  • primary_table - referenced table
  • pk_column_name - referenced column
  • no - column id in a key constraint
  • fk_constraint_name - foreign key constraint name

Rows

  • One row represents one column of every table in a database
  • Scope of rows: all columns from all tables in a database
  • Ordered by table schema and name, column id in a table

Resultado - Sample results

Algumas colunas no banco de dados de amostra com suas chaves anteriores:
A few columns in Sample database with their foregin keys:


Copyright © Dataedo.