CREATEEVENT test_event_02ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1MINUTEON COMPLETION PRESERVEDOINSERT INTO messages(message,created_at)VALUES('Test MySQL Event 2',NOW());
等待1分钟后,查看messages表,添加了另一条记录:
SELECT*FROM messages;
执行上面查询语句,得到以下结果 -
mysql>SELECT*FROM messages;+----+--------------------+---------------------+| id | message | created_at |+----+--------------------+---------------------+| 1 | Test MySQL Event1 | 2017-08-0304:23:11 || 2 | Test MySQL Event2 | 2017-08-0304:24:48 |+----+--------------------+---------------------+2rowsinset
mysql> SHOW EVENTS FROM testdb;+--------+---------------+----------------+-----------+----------+---------------------+----------------+----------------+--------+------+----------+------------+----------------------+----------------------+--------------------+
| Db | Name | Definer | Time zone | Type | Execute at | Interval value | Interval field | Starts | Ends | Status | Originator | character_set_client | collation_connection | Database Collation |
+--------+---------------+----------------+-----------+----------+---------------------+----------------+----------------+--------+------+----------+------------+----------------------+----------------------+--------------------+
| testdb | test_event_02 | root@localhost | SYSTEM | ONE TIME | 2017-08-03 04:24:48 | NULL | NULL | NULL | NULL | DISABLED | 0 | utf8 | utf8_general_ci | utf8_general_ci |
+--------+---------------+----------------+-----------+----------+---------------------+----------------+----------------+--------+------+----------+------------+----------------------+----------------------+--------------------+
1rowinset
以下语句创建一个循环的事件,每分钟执行一次,并在其创建时间的1小时内过期:
CREATEEVENT test_event_03ON SCHEDULE EVERY 1MINUTESTARTS CURRENT_TIMESTAMPENDS CURRENT_TIMESTAMP + INTERVAL 1HOURDOINSERT INTO messages(message,created_at)VALUES('Test MySQL recurring Event',NOW());
mysql>SELECT*FROM messages;+----+----------------------------+---------------------+| id | message | created_at |+----+----------------------------+---------------------+| 1 | Test MySQL Event1 | 2017-08-0304:23:11 || 2 | Test MySQL Event2 | 2017-08-0304:24:48 || 3 | Test MySQL recurring Event | 2017-08-0304:25:20 || 4 | Test MySQL recurring Event | 2017-08-0304:26:20 || 5 | Test MySQL recurring Event | 2017-08-0304:27:20 |+----+----------------------------+---------------------+5rowsinset