Get list of all foreign key constraints.

Here is the way i do this -

SELECT conname,
       t.relname AS TABLE,
       tf.relname AS ftable,
       a.attname AS field,
       af.attname AS ffield
FROM pg_constraint c,
     pg_attribute a,
                   pg_attribute af,
                   pg_stat_user_tables t,
                   pg_stat_user_tables tf
WHERE c.contype='f'
  AND c.conkey[1]=a.attnum
  AND c.conrelid=a.attrelid
  AND c.confkey[1]=af.attnum
  AND c.confrelid=af.attrelid
  AND t.relid=c.conrelid
  AND tf.relid=c.confrelid

No Comments yet »

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
Click to hear an audio file of the anti-spam word