primary keys

From: Orion Henry <lab(at)orangekids(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: primary keys
Date: 2006-04-20 02:39:45
Message-ID: 4446F471.1090503@orangekids.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I'm trying to craft a query that will determine what column(s) are the
primary key for a given table. I have succeeded but the query is so
ugly that it borders on silly and cannot work for an arbitrary number of
tables since indkey is an int2vect and the ANY keyword does not work on
it.

Please tell me there's an easier way to do this. Here is the query for
tablename $table.

SELECT attname
FROM pg_index
JOIN pg_class ON (indrelid = pg_class.oid)
JOIN pg_attribute ON (attrelid = pg_class.oid)
WHERE indisprimary IS TRUE
AND (attnum = indkey[0] OR attnum = indkey[1] OR attnum = indkey[2])
AND relname = '$table';

Orion

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Orion 2006-04-20 02:46:45 locating a primary key
Previous Message Michael Glaesemann 2006-04-20 00:39:00 Re: Categories and Sub Categories (Nested)