pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: "D'Arcy" "J.M." Cain <darcy@druid.net>
To: grimme@atix.de
Subject: Re: [SQL] SQL-Query 2 get primary key
Date: Fri, 19 Feb 1999 23:07:22 -0500 (EST)
Message-ID: <m10E4Vi-0000boC@druid.net> (raw)
In-Reply-To: <36CD659A.438BE39B@atix.de>

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@{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.




view thread (10+ messages)  latest in thread

Message-ID: <m10E4Vi-0000boC@druid.net>
Permalink:  ../m10E4Vi-0000boC@druid.net/
Also on:    postgresql.org/message-id/m10E4Vi-0000boC@druid.net

 · 

reply

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: darcy@druid.net, grimme@atix.de
  Subject: Re: [SQL] SQL-Query 2 get primary key
  In-Reply-To: <m10E4Vi-0000boC@druid.net>
  References: <36CD659A.438BE39B@atix.de> <m10E4Vi-0000boC@druid.net>

* 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