How to know relations between tables

2020-07-02 01:08发布

I have a database in MySQL created by someone. I don't have any documentation of the database.

How can I know the relationship between the tables?

Is there any query or a procedure to generate a report so that it's easy to find the relations?

I can look into Schema information and manually figure it out, but it would be great if I could generate a relationship report.

8条回答
倾城 Initia
2楼-- · 2020-07-02 01:38

Do you have the SELECTs that use the database? That may be the best source of the relationships.

查看更多
Summer. ? 凉城
3楼-- · 2020-07-02 01:42

You may take a look at information_scheme.KEY_COLUMN_USAGE table

As it is suggested there a quick way to list your FKs (Foreign Key references) using the KEY_COLUMN_USAGE view:

SELECT CONCAT( table_name, '.',
column_name, ' -> ',
referenced_table_name, '.',
referenced_column_name ) AS list_of_fks
FROM information_schema.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_SCHEMA = (your schema name here)
AND REFERENCED_TABLE_NAME is not null
ORDER BY TABLE_NAME, COLUMN_NAME;
查看更多
登录 后发表回答