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','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;

寫作不易,如果你點擊了收藏⭐,請不要忘了關注❤️、評論📝、點贊👍!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章