Re: Finding the primary key of tables

From: George Silva <georger(dot)silva(at)gmail(dot)com>
To: Devrim GÜNDÜZ <devrim(at)gunduz(dot)org>
Cc: PGSQL Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Re: Finding the primary key of tables
Date: 2010-08-03 19:38:04
Message-ID: AANLkTinSAiTH_O9s9oXq46NNOH2TRRvCJqGv9SbiWMJX@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I'm going for Merlin's solution. Its the easiest one :P

But I'm also having a problem:

SELECT column_name FROM information_schema.key_column_usage k
LEFT OUTER JOIN information_schema.table_constraints ON (k.table_name =
table_constraints.table_name)
WHERE
table_constraints.constraint_type = 'PRIMARY KEY'
AND k.table_name = 'acidentes'
AND k.table_schema = 'public'

this still returns me multiple columns. Did I forgot something?

2010/8/3 Devrim GÜNDÜZ <devrim(at)gunduz(dot)org>

> On Tue, 2010-08-03 at 16:13 -0300, George Silva wrote:
> > I'm building a function which needs to know what is the primary key of
> > a
> > certain table (all in pgplsql).
> >
> > I was using select * from information_schema.key_column_usage where
> > table_schema='foo' and table_name = 'aaa'; but that will give me
> > multiple
> > results in case of additional keys in the table.
> >
> > Any suggestions?
>
> See pg_index.indisprimary column. If it is true, then the it is the PK
> of given table.
>
> Regards,
> --
> Devrim GÜNDÜZ
> PostgreSQL Danışmanı/Consultant, Red Hat Certified Engineer
> PostgreSQL RPM Repository: http://yum.pgrpms.org
> Community: devrim~PostgreSQL.org, devrim.gunduz~linux.org.tr
> http://www.gunduz.org Twitter: http://twitter.com/devrimgunduz
>

--
George R. C. Silva

Desenvolvimento em GIS
http://blog.geoprocessamento.net

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2010-08-03 19:49:57 Re: Question about Idle in TX
Previous Message Devrim GÜNDÜZ 2010-08-03 19:33:40 Re: Finding the primary key of tables