Обсуждение: Can anyone explain how this works?

Поиск
Список
Период
Сортировка

Can anyone explain how this works?

От
RbrtBrn3@aol.com
Дата:
<font face="arial,helvetica"><font size="2">Hi, <br /><br />I recently posted this same question a few weeks back but
lostthe reply <br />someone kindly sent. The question again how exactly does this query work: <br /><br />it will
returnall attributes and respective data types of a given table': <br /><br />select attname, typname <br />from
pg_classc, pg_attribute a, pg_type t <br />where relname = relation_name and <br />attrelid = c.oid and <br />atttypid
=t.oid and <br />attnum > 0 <br />order by attnum; <br /><br />Many thanks, <br /><br />Rob Burne.</font></font> 

Re: Can anyone explain how this works?

От
Joel Burton
Дата:
On Mon, 2 Apr 2001 RbrtBrn3@aol.com wrote:

> Hi,
> 
> I recently posted this same question a few weeks back but lost the reply 
> someone kindly sent. The question again how exactly does this query work:
> 
> it will return all attributes and respective data types of a given table':
> 
> select attname, typname
> from pg_class c, pg_attribute a, pg_type t
> where relname = relation_name and
> attrelid = c.oid and
> atttypid = t.oid and
> attnum > 0
> order by attnum;

Understanding a few minutes' worth of the system tables hold will help a
lot here--you can find that in the Developer's Guide.

Eseentially, pg_class hold "classes" (ie tables, views, sequences,
etc.) pg_attribute holds "Attributes" (ie fields). This query joins
togetehr pg_class and pg_Attribute, showing you all attributes for a class
with name = 'relation name'. attnum > 0 is perhaps the only
odd part -- it has to do w/hiding certain system columns of tables that
ordinary users don't realize are there are don't care about.

-- 
Joel Burton   <jburton@scw.org>
Director of Information Systems, Support Center of Washington