How can I get the foreign keys of a table in mysql

后端 未结 4 1460
小蘑菇
小蘑菇 2021-01-02 22:40

I am creating a class which, takes a table from a database, and displays it to a web-page, with as much functionality as possible. One of the things I would like to support,

4条回答
  •  借酒劲吻你
    2021-01-02 23:29

    based on Bill Karwin answer in this other thread, I used this solution to get all the info I needed, included on_delete and on_update rules:

    SELECT kcu.referenced_table_schema, kcu.constraint_name, kcu.table_name, kcu.column_name, kcu.referenced_table_name, kcu.referenced_column_name, 
       rc.update_rule, rc.delete_rule 
    FROM INFORMATION_SCHEMA.key_column_usage kcu
    JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc on kcu.constraint_name = rc.constraint_name
    WHERE kcu.referenced_table_schema = 'db_name' 
    AND kcu.referenced_table_name IS NOT NULL 
    ORDER BY kcu.table_name, kcu.column_name
    

提交回复
热议问题