How to detect primary key of a table

Started by Sergey Karinabout 20 years ago4 messagesgeneral
Jump to latest
#1Sergey Karin
sergey.karin@gmail.com

Hi, List!

I use PG8.1

Are there any abilities to detect primary key of a table?

Sergey Karin

#2Sergey Karin
sergey.karin@gmail.com
In reply to: Sergey Karin (#1)
Re: How to detect primary key of a table

Yes, it is not a problem via psql.

But I want to detect primary key via libpq (e.g using select commands)

2006/3/30, Michael Kleiser <Michael.Kleiser@combots.com>:

Show quoted text

\d your_table
will show it

create table foobar ( foo int primary key, bar int );

NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index
"foobar_pkey" for table "foobar"
CREATE TABLE

\d foobar

Table "public.foobar"
Column | Type | Modifiers
--------+---------+-----------
foo | integer | not null
bar | integer |
Indexes:
"foobar_pkey" PRIMARY KEY, btree (foo)

________________________________

From: pgsql-general-owner@postgresql.org
[mailto:pgsql-general-owner@postgresql.org] On Behalf Of Sergey Karin
Sent: Thursday, March 30, 2006 1:13 PM
To: pgsql-general@postgresql.org
Subject: [GENERAL] How to detect primary key of a table

Hi, List!

I use PG8.1

Are there any abilities to detect primary key of a table?

Sergey Karin

#3Tino Wildenhain
tino@wildenhain.de
In reply to: Sergey Karin (#2)
Re: How to detect primary key of a table

Sergey Karin schrieb:

Yes, it is not a problem via psql.

But I want to detect primary key via libpq (e.g using select commands)

Look into informational-schema
or start psql with the option -e
then use \d+ and see what it prints :-)

Regards
Tino

#4Michael Fuhr
mike@fuhr.org
In reply to: Sergey Karin (#1)
Re: How to detect primary key of a table

On Thu, Mar 30, 2006 at 03:13:12PM +0400, Sergey Karin wrote:

Are there any abilities to detect primary key of a table?

In psql you could use "\d tablename". To see the queries that psql
makes, execute "\set ECHO_HIDDEN" and then "\d tablename". Here's
the documentation for the system catalogs that psql queries:

http://www.postgresql.org/docs/8.1/interactive/catalogs.html

You could also query the Information Schema; table_constraints
joined with key_column_usage or constraint_column_usage should
yield a table's primary key columns.

http://www.postgresql.org/docs/8.1/interactive/information-schema.html

--
Michael Fuhr