MySQL:获取引用已删除行的表列表

时间:2011-04-08 10:27:47

标签: mysql sql database innodb

假设我删除(或可能编辑)某个表中的行。我需要获取引用该特定行的所有表的列表(基本上,其他表将具有删除行的表的FK)。我该怎么做呢?我正在使用InnoDB。

2 个答案:

答案 0 :(得分:0)

看看这个查询

select c.table_schema,u.table_name,u.column_name,u.referenced_column_name
from information_schema.table_constraints as c
inner join information_schema.key_column_usage as u
using( constraint_schema, constraint_name )
where c.constraint_type = 'FOREIGN KEY'
and u.referenced_table_schema='your_db_name'
and u.referenced_table_name = 'your_table_name'
order by c.table_schema,u.table_name;

答案 1 :(得分:0)

另一个变体(例如sakila db,表actor) -

SELECT table_schema, table_name, referenced_table_schema, referenced_table_name
FROM
  information_schema.key_column_usage
WHERE
  referenced_table_schema = 'sakila' AND referenced_table_name = 'actor'
GROUP BY
  constraint_schema, constraint_name;

+--------------+------------+-------------------------+-----------------------+
| table_schema | table_name | referenced_table_schema | referenced_table_name |
+--------------+------------+-------------------------+-----------------------+
| sakila       | film_actor | sakila2                 | actor                 |
+--------------+------------+-------------------------+-----------------------+