How do I see all foreign keys to a table or column?

后端 未结 13 1053
忘了有多久
忘了有多久 2020-11-22 09:14

In MySQL, how do I get a list of all foreign key constraints pointing to a particular table? a particular column? This is the same thing as this Oracle question, but for MyS

13条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 09:24

    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;
    

    This query does assume that the constraints and all referenced and referencing tables are in the same schema.

    Add your own comment.

    Source: the official mysql manual.

提交回复
热议问题