pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Achilleas Mantzios <achill@matrix.gatewaynet.com>
To: Carlos Santos <carlos@compels.net>
To: PostgreSQL SQL <pgsql-sql@postgresql.org>
Subject: Re: Query a select that returns....
Date: Tue, 19 Dec 2006 17:05:01 +0200
Message-ID: <200612191705.01684.achill@matrix.gatewaynet.com> (raw)
In-Reply-To: <20061219140105.74337.qmail@web57103.mail.re3.yahoo.com>
References: <20061219140105.74337.qmail@web57103.mail.re3.yahoo.com>

Στις Τρίτη 19 Δεκέμβριος 2006 16:01, ο/η Carlos Santos έγραψε:
> Hi!
> I need to query a select that returns all the fields of an specific primary
> key, but I don't have the  single column's name that is constrained as
> primary key. How can I do that?
> Something like:
> SELECT * FROM myTable WHERE myTable.pkey = 'foo';

First by
SELECT pgc.conkey from pg_class pgcl,pg_constraint pgc where 
pgcl.relname='your table name' and pgcl.oid=pgc.conrelid and pgc.contype='p';
you get the attribute numbers of the primary key.
Then you have to lookup pg_attribute to find the column names.

In the simplified case where the primary key is consisted of only one
attribute (column), then
SELECT pgat.attname from pg_class pgcl,pg_constraint pgc,pg_attribute pgat 
where pgcl.relname='your table name' and pgcl.oid=pgc.conrelid and 
pgc.contype='p' and pgat.attrelid=pgcl.oid and attnum=pgc.conkey[1];

should give you the attribute name of the primary key.
Then you build your query from your program accordingly.
>
> Thanks
>
> Carlos Henrique Iazzetti Santos
> Compels Informática
>  Santa Rita do Sapucaí - MG
> www.compels.net
>
>
>
>
>
>
> _______________________________________________________
> O Yahoo! está de cara nova. Venha conferir!
> http://br.yahoo.com

-- 
Achilleas Mantzios



Message-ID: <200612191705.01684.achill@matrix.gatewaynet.com>
Permalink:  ../200612191705.01684.achill@matrix.gatewaynet.com/
Also on:    postgresql.org/message-id/200612191705.01684.achill@matrix.gatewaynet.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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: achill@matrix.gatewaynet.com, carlos@compels.net
  Subject: Re: Query a select that returns....
  In-Reply-To: <200612191705.01684.achill@matrix.gatewaynet.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