Find tables without relationships - Loner Tables - in Snowflake

This query listed tables that have no foreign keys, meaning they are not referencing any table or are not on the "many" side of FK.

Query below lists tables that have no foreign key and are not referenced by any foreign key either. Something we called Loner Tables. This diagram illustrates the concept:

Learn more about Loner Tables

See also:

Query

select 'No FKs >-' as refs,
       tab.table_schema,
       tab.table_name,
       '>- no FKs' as foreign_keys
from information_schema.tables tab
left join(
          select distinct tco.table_schema,
                          tco.table_name
          from information_schema.referential_constraints rco
          join information_schema.table_constraints tco
               on rco.unique_constraint_name = tco.constraint_name
               and rco.unique_constraint_schema = tco.table_schema
          union
          select distinct tco.table_schema,
                          tco.table_name
          from information_schema.referential_constraints rco
          join information_schema.table_constraints tco
               on rco.constraint_name = tco.constraint_name
               and rco.constraint_schema = tco.table_schema
) rel on rel.table_name = tab.table_name
      and rel.table_schema = tab.table_schema
where tab.table_type ='BASE TABLE'
      and rel.table_name is null
order by tab.table_schema,
         tab.table_name;

Columns

  • refs - icon of FK reference to this table absence
  • table_schema - name of the schema
  • table_name - name of the table
  • fks - icon of FK absence

Rows

  • One row: represents one table
  • Scope of rows: tables not refererencing and not being referenced with foreign key with any table
  • Ordered by: schema name, table name

Sample results

Comments are only visible when the visitor has consented to statistics cookies. To see and add comments please accept statistics cookies.
0
There are no comments. Click here to write the first comment.