X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id B74D4529E5 for ; Thu, 18 Aug 2005 13:18:59 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 30995-07 for ; Thu, 18 Aug 2005 16:18:51 +0000 (GMT) Received: from ox.eicat.ca (ox.eicat.ca [66.96.30.35]) by svr1.postgresql.org (Postfix) with ESMTP id AB50F528DB for ; Thu, 18 Aug 2005 13:18:45 -0300 (ADT) Received: from dilbert (H245.C18.B96.tor.eicat.ca [66.96.18.245]) by ox.eicat.ca (Postfix) with SMTP id 85269F1F9; Thu, 18 Aug 2005 12:18:43 -0400 (EDT) Date: Thu, 18 Aug 2005 12:18:44 -0400 From: "D'Arcy J.M. Cain" To: Roger Tannous Cc: pgsql-sql@postgresql.org Subject: Re: A Table's Primary Key Listing Message-Id: <20050818121844.33a09ad2.darcy@druid.net> In-Reply-To: <20050818143622.32448.qmail@web51910.mail.yahoo.com> References: <20050818143622.32448.qmail@web51910.mail.yahoo.com> X-Mailer: Sylpheed version 2.0.0 (GTK+ 2.6.8; i386--netbsdelf) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 required=5 tests=[none] X-Spam-Level: X-Archive-Number: 200508/144 X-Sequence-Number: 22372 On Thu, 18 Aug 2005 07:36:22 -0700 (PDT) Roger Tannous wrote: > Is there any means to get a list of the Primary Keys (or simply the > Primary Key if there's only one :) ) for a given table using an SQL query Here is what I do in PyGreSQL: SELECT pg_namespace.nspname, pg_class.relname,pg_attribute.attname FROM pg_class JOIN pg_namespace ON pg_namespace.oid=pg_class.relnamespace AND pg_namespace.nspname NOT LIKE 'pg_%' JOIN pg_attribute ON pg_attribute.attrelid=pg_class.oid AND pg_attribute.attisdropped='f' JOIN pg_index ON pg_index.indrelid=pg_class.oid AND pg_index.indisprimary='t' AND pg_index.indkey[0]=pg_attribute.attnum -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.