Re: List prime key column names

From: "CN" <cnliou9(at)fastmail(dot)fm>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: List prime key column names
Date: 2004-06-18 02:45:26
Message-ID: 1087526726.1876.198672942@webmail.messagingengine.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> I am trying to retrieve the column names that comprise primary key of a
> table.
> According to manual, pg_index.indkey is an array of indnatts whose usage
> I am unaware of.
>
> CREATE TABLE table1 (
> PRIMARY KEY (c1,c2),
> c1 SMALLINT,
> c2 VARCHAR(10),
> c3 VARCHAR(10)
> )WITHOUT OIDS;
>
> I need a SQL to list the following 2 rows:
>
> c1
> c2

The following SQL:

select attname
from pg_class,pg_index,pg_attribute
where relname='table1' and relkind='r' and relhaspkey
and indrelid=pg_class.oid and indisprimary
and attrelid=pg_class.oid and attnum = ANY (indkey)

gives error: "ERROR: op ANY/ALL (array) requires array on right side"

Further tests show that this appears to work:

select attname
from pg_class,pg_index,pg_attribute
where relname='table1' and relkind='r' and relhaspkey
and indrelid=pg_class.oid and indisprimary
and attrelid=pg_class.oid and attnum IN
(indkey[0],indkey[1],indkey[2].....indkey[63])

But is there any shortcut?

Regards,
CN

Browse pgsql-general by date

  From Date Subject
Next Message Tom Allison 2004-06-18 02:47:01 Re: [GENERAL] SCO embraces MySQL
Previous Message Tom Allison 2004-06-18 02:43:15 Re: 7.4 windows version?