试图找到一种方法来查找对 MySQL 对象的依赖关系。例如,函数用于视图、触发器、存储过程和其他函数。对于可以做到这一点的查询或工具有什么建议吗?
我创建了存储过程来加载每种类型、表、例程和视图的依赖项。下面是一个表:
CREATE PROCEDURE `sys_get_table_depends` (
p_object_name varchar(256)
)
BEGIN
SELECT
information_schema.routines.routine_type as `object_type`
,information_schema.routines.routine_name as `object_name`
,information_schema.routines.routine_definition as `object_definition`
FROM information_schema.tables
INNER
JOIN information_schema.routines
ON routines.routine_definition LIKE Concat('% ', tables.table_name, ' %') OR
routines.routine_definition LIKE Concat('%.', tables.table_name, ' %') OR
routines.routine_definition LIKE Concat('%`', tables.table_name, '`%')
where tables.table_name = p_object_name
UNION
SELECT
'trigger' as `object_type`
,concat(information_schema.triggers.event_object_table, information_schema.triggers.trigger_name) as `object_name`
,information_schema.triggers.ACTION_STATEMENT as `object_definition`
FROM information_schema.tables
INNER
JOIN information_schema.triggers
ON triggers.ACTION_STATEMENT LIKE Concat('% ', tables.table_name, ' %') OR
triggers.ACTION_STATEMENT LIKE Concat('%.', tables.table_name, ' %') OR
triggers.ACTION_STATEMENT LIKE Concat('%`', tables.table_name, '`%')
where tables.table_name = p_object_name
UNION
SELECT
'view' as `object_type`
,information_schema.views.table_name as `object_name`
,information_schema.views.view_definition as `object_definition`
FROM information_schema.tables
INNER
JOIN information_schema.views
ON views.view_definition LIKE Concat('% ', tables.table_name, ' %') OR
views.view_definition LIKE Concat('%.', tables.table_name, ' %') OR
views.view_definition LIKE Concat('%`', tables.table_name, '`%')
where tables.table_name = p_object_name;
END
但看起来并非所有对象定义都存储在 information_schema 中。有些有空的定义字段。我猜它在 MySQL 模式中。有任何想法吗?