Re: A Table's Primary Key Listing

From: Roger Tannous <roger77_lb(at)yahoo(dot)com>
To: "D'Arcy J(dot)M(dot) Cain" <darcy(at)druid(dot)net>
Cc: pgsql-sql(at)postgresql(dot)org, tgl(at)sss(dot)pgh(dot)pa(dot)us, roger77_lb(at)hotmail(dot)com
Subject: Re: A Table's Primary Key Listing
Date: 2005-08-22 10:23:29
Message-ID: 20050822102329.75393.qmail@web51901.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

So, D'Arcy's solution, although described as 'unsatisfactory' (ref.:
D'Arcy's message), seem to be the only solution.

So I noticed I was trying to play the wise man, trying to do things in a
better way, but nothing was found than D'Arcy's query:

SELECT pg_namespace.nspname, pg_class.relname,pg_attribute.attname
FROM pg_class
JOIN pg_namespace ON pg_namespace.oid=pg_class.relnamespace AND
pg_namespace.nspname NOT LIKE 'pg_%' AND pg_class.relname like 'sip_%'
JOIN pg_attribute ON pg_attribute.attrelid=pg_class.oid AND
pg_attribute.attisdropped='f'
JOIN pg_index ON pg_index.indrelid=pg_class.oid AND
pg_index.indisprimary='t' AND
(
pg_index.indkey[0]=pg_attribute.attnum OR
pg_index.indkey[1]=pg_attribute.attnum OR
pg_index.indkey[2]=pg_attribute.attnum OR
pg_index.indkey[3]=pg_attribute.attnum OR
pg_index.indkey[4]=pg_attribute.attnum OR
pg_index.indkey[5]=pg_attribute.attnum OR
pg_index.indkey[6]=pg_attribute.attnum OR
pg_index.indkey[7]=pg_attribute.attnum OR
pg_index.indkey[8]=pg_attribute.attnum OR
pg_index.indkey[9]=pg_attribute.attnum
)
ORDER BY pg_namespace.nspname, pg_class.relname,pg_attribute.attname;

Regards,
Roger Tannous.

--- "D'Arcy J.M. Cain" <darcy(at)druid(dot)net> wrote:

> On Thu, 18 Aug 2005 09:40:57 -0700 (PDT)
> Roger Tannous <roger77_lb(at)yahoo(dot)com> wrote:
> > Thanks for your query :)
> >
> > But it only shows the first of the primary keys of tables having
> multiple
> > primary keys :)
> >
> > This is apparently because of the pg_index.indkey[0] thing, so how can
> we
> > manage this query in order to get all of the keys :)
>
> That's a good question. The following query does this in a very
> unsatisfactory way. Anyone know what the general solution would be?
>
> SELECT pg_namespace.nspname, pg_class.relname,pg_attribute.attname
> FROM pg_class
> JOIN pg_namespace ON pg_namespace.oid=pg_class.relnamespace AND
> pg_namespace.nspname NOT LIKE 'pg_%'
> JOIN pg_attribute ON pg_attribute.attrelid=pg_class.oid AND
> pg_attribute.attisdropped='f'
> JOIN pg_index ON pg_index.indrelid=pg_class.oid AND
> pg_index.indisprimary='t' AND
> (
> pg_index.indkey[0]=pg_attribute.attnum OR
> pg_index.indkey[1]=pg_attribute.attnum OR
> pg_index.indkey[2]=pg_attribute.attnum OR
> pg_index.indkey[3]=pg_attribute.attnum OR
> pg_index.indkey[4]=pg_attribute.attnum OR
> pg_index.indkey[5]=pg_attribute.attnum OR
> pg_index.indkey[6]=pg_attribute.attnum OR
> pg_index.indkey[7]=pg_attribute.attnum OR
> pg_index.indkey[8]=pg_attribute.attnum OR
> pg_index.indkey[9]=pg_attribute.attnum
> )
> ORDER BY pg_namespace.nspname, pg_class.relname,pg_attribute.attname;
>
> --
> D'Arcy J.M. Cain <darcy(at)druid(dot)net> | Democracy is three wolves
> http://www.druid.net/darcy/ | and a sheep voting on
> +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
>

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message neil.saunders 2005-08-22 13:17:02 Problem calling stored procedure
Previous Message Michael Schmidt 2005-08-22 00:43:33 Numerical variables in pqsql statements