至于语法我就不多说了,手册上讲的很详细,我来说说几个要点以及一些实例。
注意事项:
1、EVENT权限是针对模式的(在MYSQL中也就是库的级别),不能对单独表来赋予权限。
2、必须在全局开启。
3、性能上的损失一定得考虑到。
mysql> show variables like '%event%';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| event_scheduler | OFF |
+-----------------+-------+
1 row in set (0.00 sec)
mysql> set global event_scheduler = on;
Query OK, 0 rows affected (0.00 sec)
mysql> use event;
Database changed
例子:
我们来创建一个简单的文章表:
mysql> create table article (id serial,title varchar(64) not null, author_name varchar(64),content mediumtext not null, create_time datetime not null,update_time datetime not null);
Query OK, 0 rows affected (0.01 sec)
以及统计表:
mysql> create table report (id int not null auto_increment primary key, r_date date not null,aid int not null,total int not null);
Query OK, 0 rows affected (0.01 sec)
mysql> 插入测试数据。。。
我们来建立一个存储过程。
mysql> delimiter ||
mysql> create procedure sp_report()
-> begin
-> insert into report(r_date,aid,total) select date(update_time) as r_date, id,count(1) from article group by date(create_time) order by r_date asc;
-> end||
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
创建EVENT;
在一分钟后执行这个存储过程。
mysql> create event report_dawn on schedule at date_add(now(),interval 1 minute) on completion preserve do call sp_report();
Query OK, 0 rows affected (0.00 sec)
mysql> show processlist;
| 7 | event_scheduler | localhost | NULL | Daemon | 5 | Waiting for next activation | NULL |
mysql> select * from report;
Empty set (0.00 sec)
察看现在的EVENT
mysql> show create event report_dawn/G
*************************** 1. row ***************************
Event: report_dawn
sql_mode:
time_zone: SYSTEM
Create Event: CREATE EVENT `report_dawn` ON SCHEDULE AT '2008-03-21 15:46:57' ON COMPLETION PRESERVE DISABLE DO call sp_report()
character_set_client: latin1
collation_connection: latin1_swedish_ci
Database Collation: utf8_general_ci
1 row in set (0.00 sec)
mysql>
我们来查看更新后的结果:
mysql> select * from report;
+----+------------+-----+-------+
| id | r_date | aid | total |
+----+------------+-----+-------+
| 1 | 2008-03-21 | 1 | 3 |
| 2 | 2008-03-22 | 16 | 1 |
| 3 | 2008-03-23 | 4 | 2 |
| 4 | 2008-03-23 | 6 | 2 |
| 5 | 2008-03-23 | 7 | 1 |
| 6 | 2008-03-23 | 8 | 2 |
| 7 | 2008-03-23 | 10 | 2 |
| 8 | 2008-04-13 | 12 | 1 |
| 9 | 2008-04-13 | 13 | 2 |
+----+------------+-----+-------+
9 rows in set (0.00 sec)
现在看看这个EVENT的状态,
mysql> select event_schema,event_name,status from information_schema.events where event_schema = 'event';
+--------------+-------------+----------+
| event_schema | event_name | status |
+--------------+-------------+----------+
| event | report_dawn | DISABLED |
+--------------+-------------+----------+
1 row in set (0.00 sec)
已经停止运行了。
mysql> select * from report;
+----+------------+-----+-------+
| id | r_date | aid | total |
+----+------------+-----+-------+
| 1 | 2008-03-21 | 1 | 3 |
| 2 | 2008-03-22 | 16 | 1 |
| 3 | 2008-03-23 | 4 | 2 |
| 4 | 2008-03-23 | 6 | 2 |
| 5 | 2008-03-23 | 7 | 1 |
| 6 | 2008-03-23 | 8 | 2 |
| 7 | 2008-03-23 | 10 | 2 |
| 8 | 2008-04-13 | 12 | 1 |
| 9 | 2008-04-13 | 13 | 2 |
| 10 | 2008-03-21 | 1 | 3 |
| 11 | 2008-03-22 | 16 | 1 |
| 12 | 2008-03-23 | 4 | 2 |
| 13 | 2008-03-23 | 6 | 2 |
| 14 | 2008-03-23 | 7 | 1 |
| 15 | 2008-03-23 | 8 | 2 |
| 16 | 2008-03-23 | 10 | 2 |
| 17 | 2008-04-13 | 12 | 1 |
| 18 | 2008-04-13 | 13 | 2 |
+----+------------+-----+-------+
18 rows in set (0.00 sec)
多了9条记录,
不过默认修改时间后。
在运行完毕后没有保存它。
因为时间已经过去了。
mysql> select event_schema,event_name,status from information_schema.events where event_schema = 'event';
Empty set (0.00 sec)
ON COMPLETION [NOT]PRESERVE
这个选项用来确认事件在执行完毕后是否保存其定义。