Обсуждение: 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?

Поиск
Список
Период
Сортировка
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';

Thanks
 
Carlos Henrique Iazzetti Santos
Compels Informática
Santa Rita do Sapucaí - MG
www.compels.net



Novidade no Yahoo! Mail: receba alertas de novas mensagens no seu celular. Registre seu aparelho agora!

Re: Query a select that returns....

От
Achilleas Mantzios
Дата:
Στις Τρίτη 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