pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Chris Hoover <revoohc@gmail.com>
To: pgsql-admin@postgresql.org Admin <pgsql-admin@postgresql.org>
Subject: help with query
Date: Thu, 16 Aug 2007 11:19:24 -0400
Message-ID: <1d219a6f0708160819q1e8d0a7fw5c9e3a87a30c4ffa@mail.gmail.com> (raw)

I need a little bit of help.  I need to use sql to pull any tables that have
the a foreign key referencing a given tables primary key.

So far I have come up with the listed query.  It works great for single
column primary keys, but if a table has a multi column primary key, it is
returning to many rows.  How can I get it to work for tables with
multi-column primary keys as well as single column primary keys?

Thanks,

Chris

select    a.relname as table_name,
    c.attname as column_name,
    w.typname as domain_name
from    pg_class a,
    pg_constraint b,
    pg_attribute c,
    pg_type w
where    a.oid = b.conrelid
and    c.atttypid = w.oid
and    c.attnum = any (b.conkey)
and    a.oid = c.attrelid
and    b.contype = 'f'
and    a.relkind = 'r'
and    c.attname in (    select    z.attname
            from    pg_class x,
                pg_constraint y,
                pg_attribute z
            where    x.oid = y.conrelid
            and    z.attnum = any (y.conkey)
            and    x.oid = z.attrelid
            and    y.contype = 'p'
            and    x.relname = 'table' ) ;

view thread (2+ messages)  latest in thread

Message-ID: <1d219a6f0708160819q1e8d0a7fw5c9e3a87a30c4ffa@mail.gmail.com>
Permalink:  ../1d219a6f0708160819q1e8d0a7fw5c9e3a87a30c4ffa@mail.gmail.com/
Also on:    postgresql.org/message-id/1d219a6f0708160819q1e8d0a7fw5c9e3a87a30c4ffa@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: revoohc@gmail.com
  Subject: Re: help with query
  In-Reply-To: <1d219a6f0708160819q1e8d0a7fw5c9e3a87a30c4ffa@mail.gmail.com>
  References: <1d219a6f0708160819q1e8d0a7fw5c9e3a87a30c4ffa@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox