Re: [Fwd: Names of columns used by primary key] - Mailing list pgsql-php

From Dan Wilson
Subject Re: [Fwd: Names of columns used by primary key]
Date
Msg-id 1029510485.3d5d15554800f@webmail.acucore.com
Whole thread Raw
In response to [Fwd: Names of columns used by primary key]  (Kevin Gordon <kgordon@paradise.net.nz>)
List pgsql-php
This query should help you out:

SELECT
    ic.relname AS index_name,
    bc.relname AS tab_name,
    ta.attname AS column_name,
    i.indisunique AS unique_key,
    i.indisprimary AS primary_key
FROM
    pg_class bc,
    pg_class ic,
    pg_index i,
    pg_attribute ta,
    pg_attribute ia
WHERE
    bc.oid = i.indrelid
    AND ic.oid = i.indexrelid
    AND ia.attrelid = i.indexrelid
    AND ta.attrelid = bc.oid
    AND bc.relname = '[table_name]'
    AND ta.attrelid = i.indrelid
    AND ta.attnum = i.indkey[ia.attnum-1]
ORDER BY
    index_name, tab_name, column_name

Of course, be sure to change the "[table_name]".  Then all you have to do is
check if primary_key is 't' and then get the column name.

-Dan

Quoting Kevin Gordon <kgordon@paradise.net.nz>:

> Question for pqsql-php mail list:
> How can I determine the names of the columns that make up the primary
> key in a postgresql table?
> Your help would be appreciated.

pgsql-php by date:

Previous
From: "Brian Moon"
Date:
Subject: PostgreSQL guru needed for Phorum.
Next
From: "Brian Moon"
Date:
Subject: Re: [GENERAL] PostgreSQL with Phorum