pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Dan Wilson <killroyboy@users.sourceforge.net>
To: Kevin Gordon <kgordon@paradise.net.nz>
Cc: pgsql-php@postgresql.org
Subject: Re: [Fwd: Names of columns used by primary key]
Date: Fri, 16 Aug 2002 09:08:05 -0600
Message-ID: <1029510485.3d5d15554800f@webmail.acucore.com> (raw)
In-Reply-To: <3D5D4541.3090706@paradise.net.nz>
References: <3D5D4541.3090706@paradise.net.nz>

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.



Message-ID: <1029510485.3d5d15554800f@webmail.acucore.com>
Permalink:  ../1029510485.3d5d15554800f@webmail.acucore.com/
Also on:    postgresql.org/message-id/1029510485.3d5d15554800f@webmail.acucore.com

 · 

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-php@postgresql.org
  Cc: killroyboy@users.sourceforge.net, kgordon@paradise.net.nz
  Subject: Re: [Fwd: Names of columns used by primary key]
  In-Reply-To: <1029510485.3d5d15554800f@webmail.acucore.com>
  References: <3D5D4541.3090706@paradise.net.nz> <1029510485.3d5d15554800f@webmail.acucore.com>

* 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