Re: Foreign Keys einer Tabelle herausfinden

From: Philipp Lawitschka <office(at)lawitschka(dot)eu>
To: postgres-liste <pgsql-de-allgemein(at)postgresql(dot)org>
Subject: Re: Foreign Keys einer Tabelle herausfinden
Date: 2009-09-07 21:22:19
Message-ID: 4AA5798B.3030901@lawitschka.eu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-de-allgemein

Hi nochmal,

habs schon herausgefunden...wen es interessiert:

SELECT c.conname, c.conkey[1] AS MyColumnID, c.confkey[1] AS
ForeignColumnID, tab1.relname AS MyTable,
tab2.relname AS ForeignTable, a1.attname AS MyColumn,
a2.attname AS ForeignColumn
FROM pg_constraint c
INNER JOIN pg_class tab1 ON c.conrelid = tab1.oid
INNER JOIN pg_class tab2 ON c.confrelid = tab2.oid
INNER JOIN pg_attribute a1 ON c.conkey[1] = a1.attnum
INNER JOIN pg_attribute a2 ON c.confkey[1] = a2.attnum
WHERE c.contype = 'f' AND a1.attrelid = tab1.oid AND a2.attrelid = tab2.oid

Grüße

Philipp Lawitschka schrieb:
> Hallo Liste,
>
> meine Anwendung muß zur Laufzeit ein Datenbankschema analysieren (eine
> Art DBIx::Class). Ich hab im Prinzip alles herausgefunden, nur komm ich
> irgendwie nicht an die Fremdschlüssel einer Tabelle ran. Hiermit bekomm
> ich viel zu viele Datensätze und weiß nicht mehr, nach was ich noch
> filtern soll:
>
> SELECT c.conname, c.contype, c.conrelid, c.conkey, c.confkey,
> a.attname, a.attnum, a.atttypid, x.relname AS MyTable, z.relname
> AS ForeignTable, t.typname
> FROM pg_constraint c
> INNER JOIN pg_class x ON c.conrelid = x.oid
> INNER JOIN pg_class z ON c.confrelid = z.oid
> INNER JOIN pg_attribute a ON c.conkey[1] = a.attnum
> INNER JOIN pg_type t ON a.atttypid = t.oid
> WHERE c.contype = 'f'::char AND t.typname='int4';
>
> Ich bin kein Profi in den Systemkatalogen. Weiß hier jemand, wie man das
> am besten angehen könnte?
>
> Viele Grüße
> Philipp Lawitschka
>

In response to

Responses

Browse pgsql-de-allgemein by date

  From Date Subject
Next Message Andreas 'ads' Scherbaum 2009-09-07 21:36:48 Re: Foreign Keys einer Tabelle herausfinden
Previous Message Philipp Lawitschka 2009-09-07 17:21:11 Foreign Keys einer Tabelle herausfinden