Ich habe diese Lösung auf 5.5 getestet, aber sie sollte auch ab 5.1 funktionieren.
Diese Abfrage kann Ihnen alle Verweise auf Ihre Tabelle anzeigen:
Denken Sie daran, "mydb" und "mytable" durch die benötigten zu ersetzen. Es ist auch möglich, dies als ANSICHT für zukünftige Verwendungen zu definieren.
SELECT rc.`CONSTRAINT_CATALOG` AS `ChildTable_Catalog`
, rc.`CONSTRAINT_SCHEMA` AS `ChildTable_Schema`
, rc.`TABLE_NAME` AS `ChildTable`
, rc.`CONSTRAINT_NAME` AS `ChildTable_ForeignKey`
, GROUP_CONCAT(DISTINCT fk.`COLUMN_NAME` ORDER BY fk.`ORDINAL_POSITION` ASC) AS `ChildTable_ForeignKey_Columns`
, rc.`UNIQUE_CONSTRAINT_CATALOG` AS `ParentTable_Catalog`
, rc.`UNIQUE_CONSTRAINT_SCHEMA` AS `ParentTable_Schema`
, rc.`REFERENCED_TABLE_NAME` AS `Parent_Table`
, rc.`UNIQUE_CONSTRAINT_NAME` AS `ParentTable_UniqueKey`
, GROUP_CONCAT(DISTINCT uk.`COLUMN_NAME` ORDER BY fk.`ORDINAL_POSITION` ASC) AS `ParentTable_UniqueKey_Columns`
-- constraint relation
FROM INFORMATION_SCHEMA.`REFERENTIAL_CONSTRAINTS` AS rc
-- foreign key
INNER JOIN INFORMATION_SCHEMA.`KEY_COLUMN_USAGE` AS fk
ON rc.`CONSTRAINT_CATALOG` = fk.`CONSTRAINT_CATALOG`
AND rc.`CONSTRAINT_SCHEMA` = fk.`CONSTRAINT_SCHEMA`
AND rc.`TABLE_NAME` = fk.`TABLE_NAME`
AND rc.`CONSTRAINT_NAME` = fk.`CONSTRAINT_NAME`
-- unique key
INNER JOIN INFORMATION_SCHEMA.`KEY_COLUMN_USAGE` AS uk
ON rc.`UNIQUE_CONSTRAINT_CATALOG` = uk.`CONSTRAINT_CATALOG`
AND rc.`UNIQUE_CONSTRAINT_SCHEMA` = uk.`CONSTRAINT_SCHEMA`
AND rc.`REFERENCED_TABLE_NAME` = uk.`TABLE_NAME`
AND rc.`UNIQUE_CONSTRAINT_NAME` = uk.`CONSTRAINT_NAME`
-- optional filter condition
WHERE rc.`UNIQUE_CONSTRAINT_SCHEMA` = 'mydb'
AND rc.`REFERENCED_TABLE_NAME` = 'mytable'
-- necessary grouping parameters
GROUP BY rc.`CONSTRAINT_CATALOG`
, rc.`CONSTRAINT_SCHEMA`
, rc.`TABLE_NAME`
, rc.`CONSTRAINT_NAME`
, rc.`UNIQUE_CONSTRAINT_CATALOG`
, rc.`UNIQUE_CONSTRAINT_SCHEMA`
, rc.`REFERENCED_TABLE_NAME`
, rc.`UNIQUE_CONSTRAINT_NAME`
-- optional ordering parameters
ORDER BY rc.`CONSTRAINT_CATALOG` ASC
, rc.`CONSTRAINT_SCHEMA` ASC
, rc.`REFERENCED_TABLE_NAME` ASC
, rc.`TABLE_NAME` ASC
;