SQL Query to get Column constraints

Started by Steveabout 23 years ago2 messageshackers
Jump to latest
#1Steve
svanspal@bigpond.com

Hi there,

I have been through the postgres faq's, and read through the describe.c
file.

I can't seem to solve my problem.

I am building an automatic file writer, that gives me information about each
of my tables.

What I need to know, is how to find out if a column is a primary, or part of
a primary key. I need to do this using sql.

I am used to oracle, whose structure is different. Can anyone help me.

Whiel I am at it, If I can dynamically find out if a column has a sequence
attached to it, that would also be useful.

Regards

Steve

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Steve (#1)
Re: SQL Query to get Column constraints

"Steve" <svanspal@bigpond.com> writes:

What I need to know, is how to find out if a column is a primary, or part of
a primary key. I need to do this using sql.

In 7.3 this is pretty easy: look in the pg_constraint table for a 'p'
constraint for the table. If you find one, 'conkey' lists the column
numbers of the key columns. In prior versions you'd need to look in
pg_index instead. (Actually the pg_index method still works, but it's
messier.)

See the "System Catalogs" chapter of the documentation.

regards, tom lane