0

i would like to ask you how to get relations between tables ? I don't have access to script that creates all tables in database. So is it any SQL query that let me get relations and type of relations ? Is it possible or not, i have access to database but not to creating script ?

Marcin Warzybok
  • 357
  • 4
  • 16

1 Answers1

0

You can use INFORMATION_SCHEMA KEY_COLUMN_USAGE Table.

It takes some time to run, but gives you all foreign keys in your database

SELECT 
     `TABLE_NAME`,
     `COLUMN_NAME`,
     `REFERENCED_TABLE_NAME`,
     `REFERENCED_COLUMN_NAME` 
FROM
     `information_schema`.`KEY_COLUMN_USAGE` 
WHERE `CONSTRAINT_SCHEMA` = 'your_database_name' 
     AND `REFERENCED_TABLE_SCHEMA` IS NOT NULL 
     AND `REFERENCED_TABLE_NAME` IS NOT NULL 
     AND `REFERENCED_COLUMN_NAME` IS NOT NULL 
ORDER BY TABLE_NAME ;

"The KEY_COLUMN_USAGE table describes which key columns have constraints."

https://dev.mysql.com/doc/refman/8.0/en/key-column-usage-table.html