在MySQL中,如何获取指向特定表的所有外键约束的列表?特定的列?这与该Oracle问题相同,但适用于MySQL。
对于表:
SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = '<database>' AND REFERENCED_TABLE_NAME = '<table>';
对于列:
SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = '<database>' AND REFERENCED_TABLE_NAME = '<table>' AND REFERENCED_COLUMN_NAME = '<column>';
基本上,我们在where子句中将REFERENCED_TABLE_NAME更改为REFERENCED_COLUMN_NAME。