Bookmark and Share BitCoin Donate: 13t8gAWVpHP2ddxMp88d1NFpZjnFJC6UwK

Find foreign keys in DB2 tables

The easier way to do this is by querying the the database catalog tables ('data dictionary'). It took me some googleing to find out exactly what DB2 system catalog contains the relevant foreign key information.

SYSCAT.SYSRELS

You can easily use the following query to get a list of all tables referenced via the foreign keys of a given table(s):
select distinct reftbname
from sysibm.sysrels
where tbname in ('TABLE1', TABLE2', 'TABLE3')

These might also be interesting to you:

select * from SYSCAT.TABLES WHERE TABSCHEMA = 'Myschema'
select * from SYSCAT.COLUMNS WHERE TABSCHEMA = 'Myschema'
select * from SYSCAT.FUNCTIONS
select * from SYSCAT.COLUMNS WHERE COLNAME = 'FOREIGNKEY'
select * from SYSIBM.SYSRELS WHERE TBNAME = 'MYTABLE'