1.查询数据库某表的外键
select
CONCAT(table_name,'.',column_name) as 'foreign key',
CONCAT(referenced_table_name,'.',referenced_column_name) as 're key',
CONSTRAINT_name as 'c name'
from information_schema.KEY_COLUMN_USAGE
where referenced_table_name is not null
and CONSTRAINT_SCHEMA = '数据库名称'
and referenced_table_name = '表的名称'
select * from information_schema.KEY_COLUMN_USAGE
2.查询数据库所有的的外键
select
CONCAT(table_name,'.',column_name) as 'foreign key',
CONCAT(referenced_table_name,'.',referenced_column_name) as 're key',
CONSTRAINT_name as 'c name'
from information_schema.KEY_COLUMN_USAGE
where referenced_table_name is not null
and CONSTRAINT_SCHEMA = '数据库名称'