事件排程器(event_scheduler):可以定時執行某些特定任務,可以看做基於時間的觸發器.
show variables like
'event_scheduler'
;select @@event_scheduler
;set
global event_scheduler=1;
show
create event insert_tdemo;
show events;
create event [
ifnot
exists
] event_name
on schedule schedule
[on completion [
not] preserve]
-- 事件執行完成後是否被刪除
[enable
|disable][
comment
'comment'
]do sql_statement;
schedule:
at timestamp[+
interval
interval
]| every interval
[starts timestamp
][ends timestamp
]interval:
quantity {year
| quarter |
month
|day
|hour
|minute
| week |
second
| year_month
create
table tdemo(
timeline timestamp);
-- 定期間隔執行某件事情 每隔一秒插入一條資料
drop event if
exists e_insert_tdemo;
create event if
notexists e_insert_tdemo
on schedule every 1
second
doinsert tdemo values
(current_timestamp);
-- 一分鐘後清空資料表tdemo中的所有資料
drop event if
exists e_truncate_tdemo;
create event if
notexists e_truncate_tdemo
on schedule at current_timestamp
+interval
1minute
dotruncate
table tdemo;
-- 定時執行在2018-02-01 16:40:00 執行
create event e_drop_tdemo
on schedule at timestamp
'2018-02-01 16:40:00'
dodrop
table tdemo;
-- 定時定期任務 xx時間後,每隔一段時間執行一次 (5秒之後,每3秒定時清空tdemo表)
create event e_clear_after_interval
on schedule every 3
second
starts current_timestamp
+interval
5second
dotruncate
table tdemo;
-- 間隔執行定時終止 每隔一秒鐘執行一次,10秒後終止執行
drop event if
exists e_clear_interval_end;
create event e_clear_interval_end
on schedule every 1
second
ends current_timestamp
+interval
10second
dotruncate
table tdemo;
-- 定時開啟間隔執行定時結束 --5秒開啟,每秒執行,10秒鐘停止執行
create event e_clear_interval_end
on schedule every 1
second
starts current_timestamp
+interval
5second
ends current_timestamp
+interval
15second
doinsert tdemo values
(current_timestamp);
-- 每天定時1點執行
create event e_clear_interval_end
on schedule every 1
day
starts '2010-12-18 01:00:00'
doinsert tdemo values
(current_timestamp
);
alter event event_name
[on schedule schedule]
[rename
to new_event_name]
[on completion [
not] preserve]
[comment
'comment'][
enable
|disable][
do sql_statement]
alter event e_test disable
;
mysql 事件排程器 MySQL事件排程器
一 建立排程器 將資料庫按自定義的時間週期觸發某種操作,類似linux下的crontab 例項 建立排程器後的每個1小時觸發一次 create event myevent on schedule at current timestamp interval 1 hour doupdate mysche...
MYSQL 事件排程器
要檢視當前是否已開啟事件排程器 show variables like event scheduler 開啟事件高度器 set global event scheduler 1 建立事件 語法 create event if not exists event name on schedule sch...
MySQL事件排程器 CREATE EVENT
mysql事件排程器 create event mysql從5.1開始支援event功能,有點類似於mssql的job,可以定時自動執行資料彙總。語法 create event if notexists event name onschedule schedule oncompletion not ...