微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MySQL 常用脚本之查看视图、存储过程/函数、触发器、计划任务等信息

大家好!我是只谈技术不剪发的 Tony 老师。上一篇文章介绍了如何在 MySQL 查看数据库、表结构、约束、索引以及它们占用的磁盘空间

今天我们继续分享其他常用的 MySQL查询脚本,包括查看视图、存储过程/函数、触发器、计划任务/事件等信息。

视图

列出数据库中的所有视图

视图(View)是虚拟的表,因此许多查看表的方式也会返回视图信息:

-- 方法
show full tables where table_type = 'VIEW';

-- 方法
select table_schema as database_name,
       table_name as view_name
from information_schema.views
where table_schema not in ('sys','information_schema', 'MysqL', 'performance_schema')
-- and table_schema = 'database_name'
order by table_schema, table_name;

-- 方法
select table_schema as database_name,
       table_name as view_name
from information_schema.tables
where table_schema not in ('sys', 'performance_schema')
and table_type = 'VIEW'
-- and table_schema = 'database_name'
order by table_schema, table_name;

查看视图的定义语句

-- 方法
select vw.table_schema as database_name,
       vw.table_name as view_name,
       vw.view_deFinition as deFinition,
       tb.table_comment as description
from information_schema.views as vw
join information_schema.tables as tb
on tb.table_name = vw.table_name
-- where vw.table_schema = 'database_name'
-- and vw.table_name = 'view_name'
order by database_name, view_name;

-- 方法
show create view view_name;

列出视图中的字段信息

select col.table_schema as database_name,
       col.table_name as view_name,
       col.ordinal_position,
       col.column_name,
       col.data_type,
       case when col.character_maximum_length is not null then col.character_maximum_length
            else col.numeric_precision 
       end as max_length,
       col.is_nullable
from information_schema.columns col
join information_schema.views v 
on v.table_schema = col.table_schema and v.table_name = col.table_name
where col.table_schema not in ('sys','MysqL', 'performance_schema')
-- and v.table_schema = 'database_name'
-- and v.table_name = 'view_name'
order by col.table_schema,col.table_name,col.ordinal_position;

存储过程和函数

列出数据库中的存储过程和函数

-- 方法
select routine_schema as database_name,
       routine_name,
       routine_type as type,
       data_type as return_type,
       routine_deFinition as deFinition
from information_schema.routines
where routine_schema not in ('sys', 'information_schema', 'performance_schema')
-- and r.routine_schema = 'database_name'
order by routine_schema,routine_name;

-- 方法
SHOW PROCEDURE STATUS
    [LIKE 'pattern' | WHERE expr];

SHOW FUNCTION STATUS
    [LIKE 'pattern' | WHERE expr];

查看存储过程和函数的参数列表

select r.routine_schema as database_name,
       r.specific_name as routine_name,
       r.routine_type AS type,
       p.parameter_name,
       p.data_type,
       case when p.parameter_mode is null and p.data_type is not null
            then 'RETURN'
            else parameter_mode end as parameter_mode,
       p.character_maximum_length as char_length,
       p.numeric_precision,
       p.numeric_scale
from information_schema.routines r
left join information_schema.parameters p
on p.specific_schema = r.routine_schema and p.specific_name = r.specific_name
where r.routine_schema not in ('sys', 'performance_schema')
-- and r.routine_schema = 'database_name'
-- and r.specific_name = 'routine_name'
order by r.routine_schema,r.specific_name,p.ordinal_position;

查看存储过程和函数的定义

SHOW CREATE PROCEDURE proc_name;
SHOW CREATE FUNCTION func_name;

触发器

列出数据库中的触发器

-- 方法
SHOW TRIGGERS
    [{FROM | IN} db_name]
    [LIKE 'pattern' | WHERE expr];

-- 方法
select trigger_schema as trigger_database,
       trigger_name,
       concat(event_object_schema, '.', event_object_table)  as trigger_table,
       action_order,
       action_timing,
       event_manipulation as trigger_event,
       action_statement as 'deFinition'
from information_schema.triggers 
where trigger_schema not in ('sys','MysqL')
-- and trigger_schema = 'database_name'
order by trigger_schema,trigger_name;

查看指定表上的触发器

select event_object_schema as database_name,
       event_object_table as table_name,'MysqL')
and event_object_schema = 'database_name'
and event_object_table = 'table_name'
order by database_name,table_name;

查看触发器的定义

show create trigger trigger_name;

计划任务/事件

列出服务器中的计划事件

-- 方法
SHOW EVENTS
    [{FROM | IN} schema_name]
    [LIKE 'pattern' | WHERE expr];

-- 方法
select event_schema as database_name,
       event_name,
       event_deFinition,
       event_type,
       concat(interval_value, ' ', interval_field) as 'interval',
       case when event_type = 'RECURRING' then starts
            else execute_at 
       end as execute_time,
       ends,
       status
from information_schema.events
-- where event_schema  = 'database_name'
order by event_schema,event_name;

对象依赖

查找视图的底层基表或视图

select vtu.view_schema as database_name,
       vtu.view_name as view_name,
       vtu.table_schema as referenced_database_name,
       vtu.table_name as referenced_object_name,
       tab.table_type as object_type
from information_schema.view_table_usage vtu
join information_schema.tables tab on vtu.table_schema = tab.table_schema
                                   and vtu.table_name = tab.table_name
where view_schema not in ('sys', 'performance_schema')
-- and vtu.view_schema = 'database_name'
-- and vtu.view_name = 'view_name'
order by vtu.view_schema,vtu.view_name;

查找存储函数被引用的视图

以下查询可以找出存储函数被哪些视图所使用,不支持内置函数用户自定义函数

select table_schema as database_name,
       table_name as view_name,
       specific_schema as routine_database,
       specific_name as routine_name
from information_schema.view_routine_usage 
where table_schema not in ('performance_schema', 'sys', 'MysqL')
-- and specific_name = 'function_name'
order by database_name, view_name;

写作不易,如果你点击了收藏⭐,请不要忘了关注❤️、评论

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐