List tables with most foreign keys in Db2 database

Query below lists tables with their number of foreign keys and number of tables they refer to.

See also:

Query

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
order by count(*) desc

Columns

  • table - table with schema name
  • foreign_keys - number of foreign keys in a table
  • referenced_tables - number of referenced tables. Note that it is not the same as number of foreign keys, as multiple foreign keys may reference the same table.

Rows

  • One row represents one table in a database
  • Scope of rows: tables in a database that have foreign keys (reference other tables)
  • Ordered by number of foreign keys from the ones with the most

Sample results

Tables in GSDB with most foreign keys: