大家好!我是只谈技术不剪发的 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','information_schema', 'mysql', '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','information_schema','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', 'mysql', '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', 'information_schema', 'mysql', '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,
trigger_name,
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 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','information_schema', 'mysql', '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', 'information_schema', 'mysql')
-- and specific_name = 'function_name'
order by database_name, view_name;
写作不易,如果你点击了收藏⭐,请不要忘了关注❤️、评论、点赞!