List all tables refrenced by specific table (by FK) in Snowflake

Query below lists all tables refrenced with foregin key by specific table.

Query

select fk_tco.table_schema as foreign_schema,
       fk_tco.table_name as foreign_table,
       '>-' as rel,
       pk_tco.table_schema as referenced_schema,
       pk_tco.table_name as referenced_table
from information_schema.referential_constraints rco
join information_schema.table_constraints fk_tco 
          on fk_tco.constraint_name = rco.constraint_name
          and fk_tco.constraint_schema = rco.constraint_schema
join information_schema.table_constraints pk_tco
         on pk_tco.constraint_name = rco.unique_constraint_name
         and pk_tco.constraint_schema = rco.unique_constraint_schema
where fk_tco.table_name = 'TABLE_NAME' -- enter table name here
      -- and fk_tco.table_schema = 'SCHEMA_NAME'
order by fk_tco.table_schema,
         fk_tco.table_name;

Columns

  • foreign_schema- foreign schema name
  • foreign_table - foreign table name
  • rel - relationship symbol implicating direction
  • referenced_schema - rerefenced schema name
  • referenced_table - rerefenced table name

Rows

  • One row represents one referenced table
  • Scope of rows: all tables referenced by table
  • Ordered by referenced table schema and table name

Sample results

All tables referenced with FK by PAYMENT table.

0
There are no comments. Click here to write the first comment.