SQL to determine Tablenames, Primarykeys & Foreignkeys - Mailing list pgsql-php

From Kevin Gordon
Subject SQL to determine Tablenames, Primarykeys & Foreignkeys
Date
Msg-id 1041650869.16560.11.camel@kg15.kgdomain.com
Whole thread Raw
Responses Re: SQL to determine Tablenames, Primarykeys & Foreignkeys  (Christopher Kings-Lynne <chriskl@familyhealth.com.au>)
List pgsql-php
Thanks to previous help I am using:
$sql = "SELECT " .
    "ic.relname AS index_name, " .
    "bc.relname AS tab_name, " .
    "ta.attname AS column_name, " .
    "i.indisunique AS unique_key, " .
    "i.indisprimary AS primary_key " .
"FROM " .
    "pg_class bc, " .
    "pg_class ic, " .
    "pg_index i, " .
    "pg_attribute ta, " .
    "pg_attribute ia " .
"WHERE " .
    "bc.oid = i.indrelid " .
    "AND ic.oid = i.indexrelid " .
    "AND ia.attrelid = i.indexrelid " .
    "AND ta.attrelid = bc.oid " .
    "AND bc.relname = '" . $tablename . "' " .
    "AND ta.attrelid = i.indrelid " .
    "AND ta.attnum = i.indkey[ia.attnum-1] " .
"ORDER BY " .
    "index_name, tab_name, column_name";
which provides primary keys 100%.
I have written the following to obtain tablenames:
$sql = "SELECT " .
    "ic.relname " .
"FROM " .
    "pg_class ic " .
"WHERE " .
    "ic.relname not like 'pg%' " .
    "AND ic.relname not like '%pk' " .
    "AND ic.relname not like '%idx' ";
which I am not certain is complete but appears to work.

Could anyone help me with the SQL to retrieve Foreign Keys for a
particular Table?

Much appreciated.
Kevin Gordon




pgsql-php by date:

Previous
From: Kevin Gordon
Date:
Subject: pg_field_size
Next
From: Christopher Kings-Lynne
Date:
Subject: Re: SQL to determine Tablenames, Primarykeys & Foreignkeys