Foreign Keys einer Tabelle herausfinden

Hallo Liste,

meine Anwendung mu=C3=9F zur Laufzeit ein Datenbankschema analysieren (ei=
ne
Art DBIx::Class). Ich hab im Prinzip alles herausgefunden, nur komm ich
irgendwie nicht an die Fremdschl=C3=BCssel einer Tabelle ran. Hiermit bek=
omm
ich viel zu viele Datens=C3=A4tze und wei=C3=9F 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 =3D x.oid
INNER JOIN pg_class z ON c.confrelid =3D z.oid
INNER JOIN pg_attribute a ON c.conkey[1] =3D a.attnum
INNER JOIN pg_type t ON a.atttypid =3D t.oid
WHERE c.contype =3D 'f'::char AND t.typname=3D'int4';

Ich bin kein Profi in den Systemkatalogen. Wei=C3=9F hier jemand, wie man=
das
am besten angehen k=C3=B6nnte?

Viele Gr=C3=BC=C3=9Fe
Philipp Lawitschka

--
Sent via pgsql-de-allgemein mailing list (pgsql-de-allgemein [at] postgresql.o=
rg)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-de-allgemein
Philipp Lawitschka [ Mo, 07 September 2009 19:21 ] [ ID #2014915 ]

Re: Foreign Keys einer Tabelle herausfinden

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 =3D tab1.oid
INNER JOIN pg_class tab2 ON c.confrelid =3D tab2.oid
INNER JOIN pg_attribute a1 ON c.conkey[1] =3D a1.attnum
INNER JOIN pg_attribute a2 ON c.confkey[1] =3D a2.attnum
WHERE c.contype =3D 'f' AND a1.attrelid =3D tab1.oid AND a2.attrelid =3D =
tab2.oid

Gr=C3=BC=C3=9Fe


Philipp Lawitschka schrieb:
> Hallo Liste,
>
> meine Anwendung mu=C3=9F zur Laufzeit ein Datenbankschema analysieren (=
eine
> Art DBIx::Class). Ich hab im Prinzip alles herausgefunden, nur komm ich=

> irgendwie nicht an die Fremdschl=C3=BCssel einer Tabelle ran. Hiermit b=
ekomm
> ich viel zu viele Datens=C3=A4tze und wei=C3=9F nicht mehr, nach was ic=
h 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 =3D x.oid
> INNER JOIN pg_class z ON c.confrelid =3D z.oid
> INNER JOIN pg_attribute a ON c.conkey[1] =3D a.attnum
> INNER JOIN pg_type t ON a.atttypid =3D t.oid
> WHERE c.contype =3D 'f'::char AND t.typname=3D'int4';
>
> Ich bin kein Profi in den Systemkatalogen. Wei=C3=9F hier jemand, wie m=
an das
> am besten angehen k=C3=B6nnte?
>
> Viele Gr=C3=BC=C3=9Fe
> Philipp Lawitschka
>


--
Sent via pgsql-de-allgemein mailing list (pgsql-de-allgemein [at] postgresql.o=
rg)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-de-allgemein
Philipp Lawitschka [ Mo, 07 September 2009 23:22 ] [ ID #2014916 ]

Re: Foreign Keys einer Tabelle herausfinden

Hallo,

On Mon, 07 Sep 2009 23:22:19 +0200 Philipp Lawitschka wrote:

> habs schon herausgefunden...wen es interessiert:
>
> SELECT c.conname, c.conkey[1] AS MyColumnID, c.confkey[1] AS =09=09=09=

> 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 =3D tab1.oid
> INNER JOIN pg_class tab2 ON c.confrelid =3D tab2.oid
> INNER JOIN pg_attribute a1 ON c.conkey[1] =3D a1.attnum
> INNER JOIN pg_attribute a2 ON c.confkey[1] =3D a2.attnum
> WHERE c.contype =3D 'f' AND a1.attrelid =3D tab1.oid AND a2.attrelid =3D =
tab2.oid

W=C3=A4re es nicht praktischer, das portabel =C3=BCber verschiedene Version=
en
hinweg zu gestalten? Der View:

information_schema.referential_constraints

enth=C3=A4lt die Informationen, die du suchst. Die Systemtabellen
k=C3=B6nnen sich von Version zu Version =C3=A4ndern, das information_schema
bleibt gleich.


Bis dann

--
Andreas 'ads' Scherbaum
German PostgreSQL User Group
European PostgreSQL User Group - Board of Directors
Volunteer Regional Contact, Germany - PostgreSQL Project

PGDay.eu 2009 in Paris, Nov. 6/7, http://www.pgday.eu/

--
Sent via pgsql-de-allgemein mailing list (pgsql-de-allgemein [at] postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-de-allgemein
adsmail [ Mo, 07 September 2009 23:36 ] [ ID #2014917 ]
Datenbanken » gmane.comp.db.postgresql.german » Foreign Keys einer Tabelle herausfinden

Vorheriges Thema: == WöchentlicherPostgreSQL Newsletter - 06.September 2009
Nächstes Thema: == WöchentlicherPostgreSQL Newsletter - 30.August 2009