How to find all the relations between all mysql tables?

前端 未结 9 1956
甜味超标
甜味超标 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

    
    SELECT 
        count(1) totalrelationships ,
        c.table_name tablename,
        CONCAT(' ',GROUP_CONCAT(c.column_name ORDER BY ordinal_position SEPARATOR ', ')) columnname,
        CONCAT(' ',GROUP_CONCAT(c.column_type ORDER BY ordinal_position SEPARATOR ', ')) columntype    
    FROM
        information_schema.columns c RIGHT JOIN
        (SELECT column_name , column_type FROM information_schema.columns WHERE 
        -- column_key in ('PRI','MUL') AND  -- uncomment this line if you want to see relations only with indexes
        table_schema = DATABASE() AND table_name = 'YourTableName') AS p
        USING (column_name,column_type)
    WHERE
        c.table_schema = DATABASE()
        -- AND c.table_name != 'YourTableName'
        GROUP BY tablename
        -- HAVING (locate(' YourColumnName',columnname) > 0) -- uncomment this line to search for specific column 
        ORDER BY totalrelationships desc, columnname
    ;
    

提交回复
热议问题