Skip to content

Instantly share code, notes, and snippets.

@xivSolutions
Last active January 2, 2016 11:09
Show Gist options
  • Save xivSolutions/8294725 to your computer and use it in GitHub Desktop.
Save xivSolutions/8294725 to your computer and use it in GitHub Desktop.
Crude pg SQL to select Foreign Key relationships in Db
SELECT
parent.table_name parent,
parent.constraint_name key_name,
tc.constraint_type key_type,
(SELECT c.table_name
FROM information_schema.table_constraints c
WHERE c.constraint_name = parent.constraint_name) child,
(SELECT pid.column_name
FROM information_schema.constraint_column_usage pid
WHERE pid.constraint_name = parent.constraint_name) pid,
(SELECT cid.column_name
FROM information_schema.key_column_usage cid
WHERE cid.constraint_name = parent.constraint_name) cid
FROM information_schema.table_constraints tc
INNER JOIN information_schema.constraint_table_usage parent
ON parent.constraint_name = tc.constraint_name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment