重新定義mysql命令行結(jié)束符為//,,命令行創(chuàng)建存儲(chǔ)過(guò)程需要,。
delimiter // MySQL5.1.x版本中引入了一項(xiàng)新特性EVENT,,顧名思義就是事件,、定時(shí)任務(wù)機(jī)制,,在指定的時(shí)間單元內(nèi)執(zhí)行特定的任務(wù),因此今后一些對(duì)數(shù)據(jù)定時(shí)性操作不再依賴外部程序,,而直接使用數(shù)據(jù)庫(kù)本身提供的功能,。 要查看當(dāng)前是否已開(kāi)啟事件調(diào)度器,可執(zhí)行如下SQL: SHOW VARIABLES LIKE 'event_scheduler'; 或 SELECT @@event_scheduler; 或 SHOW PROCESSLIST; 若顯示: +-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | event_scheduler | OFF | +-----------------+-------+ 則可執(zhí)行 SET GLOBAL event_scheduler = 1; 或 SET GLOBAL event_scheduler = ON; 來(lái)開(kāi)啟,,也可以直接在啟動(dòng)命令加上“–event_scheduler=1”,,例如: mysqld ... --event_scheduler=1 my.ini or my.cnf 中的 [mysqld] 添加 event_scheduler=ON 創(chuàng)建事件(CREATE EVENT) 先來(lái)看一下它的語(yǔ)法: CREATE EVENT [IF NOT 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 | DAY_HOUR | DAY_MINUTE | DAY_SECOND | HOUR_MINUTE | HOUR_SECOND | MINUTE_SECOND} 1)首先來(lái)看一個(gè)簡(jiǎn)單的例子來(lái)演示每秒插入一條記錄到數(shù)據(jù)表 USE test; CREATE TABLE aaa (timeline TIMESTAMP); CREATE EVENT e_test_insert ON SCHEDULE EVERY 1 SECOND DO INSERT INTO test.aaa VALUES (CURRENT_TIMESTAMP); 等待3秒鐘后,再執(zhí)行查詢成功,。 2) 5天后清空test表: CREATE EVENT e_test ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 5 DAY DO TRUNCATE TABLE test.aaa; 3) 2007年7月20日12點(diǎn)整清空test表: CREATE EVENT e_test ON SCHEDULE AT TIMESTAMP '2007-07-20 12:00:00' DO TRUNCATE TABLE test.aaa; 4) 每天定時(shí)清空test表: CREATE EVENT e_test ON SCHEDULE EVERY 1 DAY DO TRUNCATE TABLE test.aaa; 5) 5天后開(kāi)啟每天定時(shí)清空test表: CREATE EVENT e_test ON SCHEDULE EVERY 1 DAY STARTS CURRENT_TIMESTAMP + INTERVAL 5 DAY DO TRUNCATE TABLE test.aaa; 6) 每天定時(shí)清空test表,,5天后停止執(zhí)行: CREATE EVENT e_test ON SCHEDULE EVERY 1 DAY ENDS CURRENT_TIMESTAMP + INTERVAL 5 DAY DO TRUNCATE TABLE test.aaa; 7) 5天后開(kāi)啟每天定時(shí)清空test表,一個(gè)月后停止執(zhí)行: CREATE EVENT e_test ON SCHEDULE EVERY 1 DAY STARTS CURRENT_TIMESTAMP + INTERVAL 5 DAY ENDS CURRENT_TIMESTAMP + INTERVAL 1 MONTH DO TRUNCATE TABLE test.aaa; [ON COMPLETION [NOT] PRESERVE]可以設(shè)置這個(gè)事件是執(zhí)行一次還是持久執(zhí)行,,默認(rèn)為NOT PRESERVE,。 8) 每天定時(shí)清空test表(只執(zhí)行一次,任務(wù)完成后就終止該事件): CREATE EVENT e_test ON SCHEDULE EVERY 1 DAY ON COMPLETION NOT PRESERVE DO TRUNCATE TABLE test.aaa; [ENABLE | DISABLE]可是設(shè)置該事件創(chuàng)建后狀態(tài)是否開(kāi)啟或關(guān)閉,,默認(rèn)為ENABLE,。 [COMMENT ‘comment’]可以給該事件加上注釋。 修改事件(ALTER EVENT) ALTER EVENT event_name [ON SCHEDULE schedule] [RENAME TO new_event_name] [ON COMPLETION [NOT] PRESERVE] [COMMENT 'comment'] [ENABLE | DISABLE] [DO sql_statement] 1) 臨時(shí)關(guān)閉事件 ALTER EVENT e_test DISABLE; 2) 開(kāi)啟事件 ALTER EVENT e_test ENABLE; 3) 將每天清空test表改為5天清空一次: ALTER EVENT e_test ON SCHEDULE EVERY 5 DAY; 刪除事件(DROP EVENT) 語(yǔ)法很簡(jiǎn)單,,如下所示: DROP EVENT [IF EXISTS] event_name 例如刪除前面創(chuàng)建的e_test事件 DROP EVENT e_test; 當(dāng)然前提是這個(gè)事件存在,,否則會(huì)產(chǎn)生ERROR 1513 (HY000): Unknown event錯(cuò)誤,因此最好加上IF EXISTS DROP EVENT IF EXISTS e_test; |
|
來(lái)自: ctlive > 《我的圖書(shū)館》