From: Tom Lane <tgl@sss.pgh.pa.us>
To: Matlack, Brad <matlackb@ogden.disa.mil>
Cc: 'pgsql-sql@postgresql.org' <pgsql-sql@postgresql.org>
Subject: Re: Finding primary keys
Date: Mon, 10 Apr 2000 19:35:12 -0400
Message-ID: <26622.955409712@sss.pgh.pa.us> (raw)
In-Reply-To: <0918BE615199D311B4F500902785F904973816@kssgqm01.ogden.disa.mil>
References: <0918BE615199D311B4F500902785F904973816@kssgqm01.ogden.disa.mil>
"Matlack, Brad" <matlackb@ogden.disa.mil> writes:
> I'm trying to determine which fields in a table are primary keys, using a
> select statement.
This was just discussed in connection with the ODBC driver. The best
solution I saw was
select ta.attname, ia.attnum
from pg_attribute ta, pg_attribute ia, pg_class c, pg_index i
where c.relname = 'foo_pkey'
AND c.oid = i.indexrelid
AND ia.attrelid = i.indexrelid
AND ta.attrelid = i.indrelid
AND ta.attnum = i.indkey[ia.attnum-1]
ORDER BY ia.attnum;
where 'foo' is the table you are interested in.
You could also do just
select attname, attnum from pg_attribute a, pg_class c
where c.relname = 'foo_pkey'
AND attrelid = c.oid
AND attnum > 0
ORDER BY attnum;
but this'll not track renaming of columns via ALTER TABLE RENAME COLUMN,
since what it's showing you is actually the names of the columns in the
index relation, and ALTER doesn't bother to change those.
regards, tom lane
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-sql@postgresql.org
Cc: tgl@sss.pgh.pa.us, matlackb@ogden.disa.mil
Subject: Re: Finding primary keys
In-Reply-To: <26622.955409712@sss.pgh.pa.us>
References: <0918BE615199D311B4F500902785F904973816@kssgqm01.ogden.disa.mil> <26622.955409712@sss.pgh.pa.us>
* 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