Skip to content

Instantly share code, notes, and snippets.

@rmsj
Created March 30, 2015 02:38
Show Gist options
  • Save rmsj/7d497efde1da7086518b to your computer and use it in GitHub Desktop.
Save rmsj/7d497efde1da7086518b to your computer and use it in GitHub Desktop.
Get all tables that reference a table.field on PostgreSQL
select R.*
from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE u
inner join INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS FK
on U.CONSTRAINT_CATALOG = FK.UNIQUE_CONSTRAINT_CATALOG
and U.CONSTRAINT_SCHEMA = FK.UNIQUE_CONSTRAINT_SCHEMA
and U.CONSTRAINT_NAME = FK.UNIQUE_CONSTRAINT_NAME
inner join INFORMATION_SCHEMA.KEY_COLUMN_USAGE R
ON R.CONSTRAINT_CATALOG = FK.CONSTRAINT_CATALOG
AND R.CONSTRAINT_SCHEMA = FK.CONSTRAINT_SCHEMA
AND R.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
WHERE U.COLUMN_NAME = 'customer_contact_id'
AND U.TABLE_NAME = 'customer_contact'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment