Re: fields and foreign keys - Mailing list pgsql-general

From Michael Fuhr
Subject Re: fields and foreign keys
Date
Msg-id 20050624141935.GA45212@winnie.fuhr.org
Whole thread Raw
In response to Re: fields and foreign keys  ("mrix" <gmarik@gmail.com>)
List pgsql-general
On Thu, Jun 23, 2005 at 10:11:15PM -0700, mrix wrote:
>
> But I've already tried this approach, and it's not quite i need.
> Because as i understand foreign keys are built on indexes, so i get
> *index* and corresponding foreign key definition.
> Well then i have to find out what filed this index "belongs to"
> (assuming 1 field - 1 index)

The query I posted shows the relation (table) that contains the
foreign key constraint, the constraint name (not an index name),
and the constraint definition, which includes the names of the
referring column(s) and the referred-to table and column(s).  For
example:

CREATE TABLE foo (id integer PRIMARY KEY);
CREATE TABLE bar (fooid integer NOT NULL REFERENCES foo);

SELECT conrelid::regclass, conname, pg_get_constraintdef(oid)
FROM pg_constraint
WHERE contype = 'f';
 conrelid |    conname     |          pg_get_constraintdef
----------+----------------+----------------------------------------
 bar      | bar_fooid_fkey | FOREIGN KEY (fooid) REFERENCES foo(id)
(1 row)

If this isn't what you're looking for, then please post an example
that shows what you're trying to do.  That is, something like "given
the following table definitions, I'm looking for a query that will
give me this output...."

What problem are you trying to solve?

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

pgsql-general by date:

Previous
From: Peter Eisentraut
Date:
Subject: Re: PostgreSQL Certification
Next
From: Martijn van Oosterhout
Date:
Subject: Re: Non-unique index performance