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

From: Hannu Krosing <hannu(at)trust(dot)ee>
To: "D'Arcy J(dot)M(dot) Cain" <darcy(at)druid(dot)net>
Cc: pgsql-hackers <pgsql-hackers(at)postgreSQL(dot)org>, Bruce Momjian <maillist(at)candle(dot)pha(dot)pa(dot)us>
Subject: Re: [HACKERS] Re: [SQL] SQL-Query 2 get primary key
Date: 1999-02-20 12:39:44
Message-ID: 36CEAD10.A4582464@trust.ee
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-sql

"D'Arcy J.M. Cain" wrote:
>
> 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';

Should it work in 6.4.0 ?

It gives an empty table for me ;(

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

You probably mean "pg_class.relname = 'test'" ?

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

Actually you can declare multi_field PK as
(Bruce: this probably should be added to \h create table):

hannu=> create table test(
hannu-> id1 int,
hannu-> id2 int,
hannu-> meat text,
hannu-> primary key (id1,id2)
hannu-> );
NOTICE: CREATE TABLE/PRIMARY KEY will create implicit index test_pkey
for table test
CREATE

-------------------------
Hannu

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Hannu Krosing 1999-02-20 16:09:44 A few questions (was: SQL-Query 2 get primary key)
Previous Message Thomas G. Lockhart 1999-02-20 06:17:05 Re: [HACKERS] lower() broken?

Browse pgsql-sql by date

  From Date Subject
Next Message D'Arcy J.M. Cain 1999-02-20 13:18:52 Re: [HACKERS] Re: [SQL] SQL-Query 2 get primary key
Previous Message Martin Möderndorfer 1999-02-20 11:33:57 Re: [SQL] Triggers to create Tables