Re: A Table's Primary Key Listing

From: "D'Arcy J(dot)M(dot) Cain" <darcy(at)druid(dot)net>
To: Roger Tannous <roger77_lb(at)yahoo(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: A Table's Primary Key Listing
Date: 2005-08-18 16:18:44
Message-ID: 20050818121844.33a09ad2.darcy@druid.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Thu, 18 Aug 2005 07:36:22 -0700 (PDT)
Roger Tannous <roger77_lb(at)yahoo(dot)com> 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 <darcy(at)druid(dot)net> | 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.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Roger Tannous 2005-08-18 16:40:57 Re: A Table's Primary Key Listing
Previous Message daq 2005-08-18 15:29:58 Re: A Table's Primary Key Listing