Re: Query a select that returns.... - Mailing list pgsql-sql

From Achilleas Mantzios
Subject Re: Query a select that returns....
Date
Msg-id 200612191705.01684.achill@matrix.gatewaynet.com
Whole thread Raw
In response to Query a select that returns all the fields of an specific value of primary key without knownig the name of the single column's primary key?  (Carlos Santos <carloscompels@yahoo.com.br>)
List pgsql-sql
Στις Τρίτη 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


pgsql-sql by date:

Previous
From: Markus Schaber
Date:
Subject: Re: null values in non-nullable column
Next
From: "George Pavlov"
Date:
Subject: Re: null values in non-nullable column