Andy
Andy

Reputation: 381

How to get name parent table by name foreign field in child table?

I need to write a php script to work with tables in MySql database. I know table name and I need know name parent table by name foreign field in this child table. How can I do it with SQL query?

UPDATE

SELECT referenced_table_name, referenced_column_name
FROM information_schema.key_column_usage
WHERE table_name = '[child_table_name]'
AND column_name = '[foreign_key_field_name]'

Upvotes: 0

Views: 1875

Answers (1)

a1ex07
a1ex07

Reputation: 37354

select referenced_table_name 
from information_schema.REFERENTIAL_CONSTRAINTS 
where  table_name ='[child_table_name]' 
 --and constraint_name ='[foreign_key_constraint_name]'

UPDATE

select referenced_table_name,
referenced_column_name 
from information_schema.key_column_usage where table_name ='[child_table_name]' 
and column_name='[foreign_key_constraint_name]';

Upvotes: 3

Related Questions