Re: [SQL] SQL-Query 2 get primary key - Mailing list pgsql-hackers

From D'Arcy" "J.M." Cain
Subject Re: [SQL] SQL-Query 2 get primary key
Date
Msg-id m10E4Vi-0000boC@druid.net
Whole thread Raw
List pgsql-hackers
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.attrelidAND       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@{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.



pgsql-hackers by date:

Previous
From: Roland Roberts
Date:
Subject: Re: [HACKERS] lower() broken?
Next
From: Vince Vielhaber
Date:
Subject: Re: [HACKERS] lower() broken?