Home All Groups Group Topic Archive Search About

finding foreign key info

Author
1 Sep 2005 9:21 PM
Ned
Hi,

Is there a way to find out if a column on a table is a foreign key, what
table it references and what column on that table it is referencing.  All of
my foreign keys are a single column, which should make things easier.

Any help would be greatly appreciated.

Regards,

Ned

Author
1 Sep 2005 11:09 PM
Trey Walpole
You can get this from a couple of INFORMATION_SCHEMA views, e.g.

select
  pk_ccu.table_name as PK_Table,
  pk_ccu.column_name as PK_Column,
  fk_ccu.table_name as FK_Table,
  fk_ccu.column_name as FK_Column
from
  information_schema.constraint_column_usage pk_ccu
  join information_schema.referential_constraints rc on
pk_ccu.constraint_name=rc.unique_constraint_name
  join information_schema.constraint_column_usage fk_ccu on
rc.constraint_name=fk_ccu.constraint_name
where
  fk_ccu.table_Name='table_in_question'
  and fk_ccu.column_name='column_in_question'


Ned wrote:
Show quote
> Hi,
>
> Is there a way to find out if a column on a table is a foreign key, what
> table it references and what column on that table it is referencing.  All of
> my foreign keys are a single column, which should make things easier.
>
> Any help would be greatly appreciated.
>
> Regards,
>
> Ned
>
>
Author
2 Sep 2005 1:32 PM
Ned
Thanks Trey,

Worked like a charm.

Regards,

Ned

Show quote
"Trey Walpole" <treyNOpole@comSPAMcast.net> wrote in message
news:exXjWn0rFHA.460@TK2MSFTNGP15.phx.gbl...
> You can get this from a couple of INFORMATION_SCHEMA views, e.g.
>
> select
>  pk_ccu.table_name as PK_Table,
>  pk_ccu.column_name as PK_Column,
>  fk_ccu.table_name as FK_Table,
>  fk_ccu.column_name as FK_Column
> from
>  information_schema.constraint_column_usage pk_ccu
>  join information_schema.referential_constraints rc on
> pk_ccu.constraint_name=rc.unique_constraint_name
>  join information_schema.constraint_column_usage fk_ccu on
> rc.constraint_name=fk_ccu.constraint_name
> where
>  fk_ccu.table_Name='table_in_question'
>  and fk_ccu.column_name='column_in_question'
>
>
> Ned wrote:
>> Hi,
>>
>> Is there a way to find out if a column on a table is a foreign key, what
>> table it references and what column on that table it is referencing.  All
>> of my foreign keys are a single column, which should make things easier.
>>
>> Any help would be greatly appreciated.
>>
>> Regards,
>>
>> Ned

AddThis Social Bookmark Button