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

后端 未结 13 983
忘了有多久
忘了有多久 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:32

    If you also want to get the name of the foreign key column:

    SELECT i.TABLE_SCHEMA, i.TABLE_NAME, 
           i.CONSTRAINT_TYPE, i.CONSTRAINT_NAME, 
           k.COLUMN_NAME, k.REFERENCED_TABLE_NAME, k.REFERENCED_COLUMN_NAME 
      FROM information_schema.TABLE_CONSTRAINTS i 
      LEFT JOIN information_schema.KEY_COLUMN_USAGE k 
           ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME 
     WHERE i.TABLE_SCHEMA = '' AND i.CONSTRAINT_TYPE = 'FOREIGN KEY' 
     ORDER BY i.TABLE_NAME;
    

提交回复
热议问题