Thread: How does this query work.....?

How does this query work.....?

From
RbrtBrn3@aol.com
Date:
<font face="arial,helvetica"><font size="2">Hi, <br /><br />I am using the following query to find the attributes of a
giventable, and <br />their datatypes: <br /><br />select typname,attname                            <br />from
pg_classc, pg_attribute a,pg_type t <br />where relname = 'table_name' and <br />attrelid = c.oid and <br />atttypid =
t.oidand <br />attnum > 0; <br /><br />Can anybody explain how this query actually works - I cannot figure it. <br
/>Thanksin advance. <br /><br />Rob Burne.</font></font> 

Re: How does this query work.....?

From
Stephan Szabo
Date:
pg_class holds the relation information (tables, etc)
pg_attribute holds attribute information (attname), it  keeps the oid of the relation it's on in attrelid and the oid
ofthe type as atttypid
 
pg_type holds type information (typname)

The attnum>0 is to limit the check to user attributes.
There are additional attributes (oid, xmin, etc...) 
defined on the tables that have attnum<0 and you usually
don't care about that.

On Fri, 9 Mar 2001 RbrtBrn3@aol.com wrote:

> Hi,
> 
> I am using the following query to find the attributes of a given table, and 
> their datatypes:
> 
> select typname,attname                           
> from pg_class c, pg_attribute a,pg_type t
> where relname = 'table_name' and
> attrelid = c.oid and
> atttypid = t.oid and
> attnum > 0;
> 
> Can anybody explain how this query actually works - I cannot figure it. 
> Thanks in advance.
> 
> Rob Burne.
>