Re: [SQL] SQL-Query 2 get primary key

From: "D'Arcy" "J(dot)M(dot)" Cain <darcy(at)druid(dot)net>
To: grimme(at)atix(dot)de (Marc Grimme)
Subject: Re: [SQL] SQL-Query 2 get primary key
Date: 1999-02-20 04:07:22
Message-ID: m10E4Vi-0000boC@druid.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-sql

Thus spake Marc Grimme
> if I create a table like this:
> CREATE TABLE test (
> id decimal(3) primary key,
> name varchar(32));
>
> how can I ask postgres which is the primary key from table test?

SELECT pg_class.relname, pg_attribute.attname
FROM pg_class, pg_attribute, pg_index
WHERE pg_class.oid = pg_attribute.attrelid AND
pg_class.oid = pg_index.indrelid AND
pg_index.indkey[0] = pg_attribute.attnum AND
pg_index.indisprimary = 't';

That lists all the primary keys in your database. Add a "WHERE pg_class
= 'test'" clause to get the specific table.

Note that this makes the assumption that only one field can be in the
primary key (no complex primary keys) but I don't think there will
ever be more than one the way we declare it now.

--
D'Arcy J.M. Cain <darcy(at){druid|vex}.net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 424 2871 (DoD#0082) (eNTP) | what's for dinner.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message The Hermit Hacker 1999-02-20 04:48:00 RE: [HACKERS] Major bug, possible, with Solaris 7?
Previous Message Daryl W. Dunbar 1999-02-20 03:50:13 RE: [HACKERS] Major bug, possible, with Solaris 7?

Browse pgsql-sql by date

  From Date Subject
Next Message Michael Olivier 1999-02-20 05:04:29 psql: how to find out type of an index (hash, btree, ...)?
Previous Message Eric Lee Green 1999-02-20 03:18:34