How to find all the relations between all mysql tables?

前端 未结 9 1959
甜味超标
甜味超标 2020-12-01 01:12

How to find all the relations between all MySQL tables? If for example, I want to know the relation of tables in a database of having around 100 tables.

Is there any

9条回答
  •  感情败类
    2020-12-01 01:41

    Based on xudre's answer, you can execute the following to see all the relations of a schema:

    SELECT 
      `TABLE_SCHEMA`,                          -- Foreign key schema
      `TABLE_NAME`,                            -- Foreign key table
      `COLUMN_NAME`,                           -- Foreign key column
      `REFERENCED_TABLE_SCHEMA`,               -- Origin key schema
      `REFERENCED_TABLE_NAME`,                 -- Origin key table
      `REFERENCED_COLUMN_NAME`                 -- Origin key column
    FROM `INFORMATION_SCHEMA`.`KEY_COLUMN_USAGE`
    WHERE `TABLE_SCHEMA` = 'YourSchema'
    AND   `REFERENCED_TABLE_NAME` IS NOT NULL  -- Only tables with foreign keys
    

    What I want in most cases is to know all FKs that point to a specific table. In this case I run:

    SELECT 
      `TABLE_SCHEMA`,                          -- Foreign key schema
      `TABLE_NAME`,                            -- Foreign key table
      `COLUMN_NAME`                            -- Foreign key column
    FROM `INFORMATION_SCHEMA`.`KEY_COLUMN_USAGE`  
    WHERE `TABLE_SCHEMA` = 'YourSchema'
    AND   `REFERENCED_TABLE_NAME` = 'YourTableName'
    

提交回复
热议问题