Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 9E8CD9FA4F9 for ; Thu, 16 Aug 2007 12:19:32 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 23013-07-4 for ; Thu, 16 Aug 2007 12:19:27 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from nf-out-0910.google.com (nf-out-0910.google.com [64.233.182.187]) by postgresql.org (Postfix) with ESMTP id 5B2369F98C9 for ; Thu, 16 Aug 2007 12:19:26 -0300 (ADT) Received: by nf-out-0910.google.com with SMTP id f5so180179nfh for ; Thu, 16 Aug 2007 08:19:25 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type; b=pnsbA3OnCG68dxgVxIoQsrAsDtOHodaOHRgsvKpINJ/N9GNfMIamgRH5IzzJ0amxyeHF4/6g5cUJ7oYybIUxl/dAXE52TiMf5Msa+9Yt2LYeynvCaKOtz4kpKBu3RbSCkLrrhaTmHn1sxCFQAguAtVasGXUQmpZijuSZE5YAY/U= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:mime-version:content-type; b=Mw2fTbHJO4Kv74k5YA6ZSIDNhXl2OXsGtLepY2Cu6umlX3n8UtCDM5sUDvNFVZyfryqF3tCHNNiT+kKtNPKTiDn7x5CwBpiAYVtkc4aMtEA5XhDr/FrtSN5Uhm9X5SefYXcw8jgHYw0brbF4Ti6rhZQ11SAfzAIZ9sYcPmwMcQg= Received: by 10.78.140.17 with SMTP id n17mr683074hud.1187277564348; Thu, 16 Aug 2007 08:19:24 -0700 (PDT) Received: by 10.78.201.16 with HTTP; Thu, 16 Aug 2007 08:19:22 -0700 (PDT) Message-ID: <1d219a6f0708160819q1e8d0a7fw5c9e3a87a30c4ffa@mail.gmail.com> Date: Thu, 16 Aug 2007 11:19:24 -0400 From: "Chris Hoover" To: "pgsql-admin@postgresql.org Admin" Subject: help with query MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_219327_16787862.1187277564320" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200708/205 X-Sequence-Number: 26536 ------=_Part_219327_16787862.1187277564320 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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' ) ; ------=_Part_219327_16787862.1187277564320 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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' ) ;
------=_Part_219327_16787862.1187277564320--