Skip to content

Instantly share code, notes, and snippets.

@tym-xqo
Last active July 17, 2019 22:08
Show Gist options
  • Save tym-xqo/3d75cc2f0e169b95566c413511895057 to your computer and use it in GitHub Desktop.
Save tym-xqo/3d75cc2f0e169b95566c413511895057 to your computer and use it in GitHub Desktop.
select c.table_schema
, c.constraint_name
, c.table_name
, k.column_name
, u.table_schema as foreign_table_schema
, u.table_name as foreign_table_name
, u.column_name as foreign_column_name
from information_schema.table_constraints as c
join information_schema.key_column_usage as k
on c.constraint_name = k.constraint_name
and c.table_schema = k.table_schema
join information_schema.constraint_column_usage as u
on u.constraint_name = c.constraint_name
and u.table_schema = c.table_schema
where c.constraint_type = 'FOREIGN KEY'
and c.table_name='some_table';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment