MySQL Event

MYSQL event was 5.1 new addition, if you want to experience, the proposed upgrade.
As for the syntax I do not say, speaking of the manual is very detailed, me say a few points as well as some examples.
Note:
1, EVENT privilege is (that is, the level of the library in the MYSQL) for the model, not to give permission to a separate table.
2, must be turned on globally.
3, a performance penalty must have taken into account.

MySQL> Show Variables like '%% Event';
+ ----------------- + ------- +
| variable_name | the Value |
+ ------ + ------- + -----------
| event_scheduler | OFF |
+ ----------------- + ------ - +
. 1 in Row SET (0.00 sec)

MySQL> SET ON = Global event_scheduler;
Query the OK, 0 rows affected (0.00 sec)

MySQL> Event use;
Database changed MySQL> insert test data. . . We have to create a stored procedure.
例子:
我们来创建一个简单的文章表:
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> 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 ;

Creating EVENT;
execute the stored procedure in a minute. MySQL> Show processlist; | 7 | event_scheduler | localhost | NULL | Daemon | 5 | Waiting for the Next Activation | NULL | MySQL> the SELECT * from Report; Empty the SET (0.00 sec) look now EVENT MySQL> Show the Create Event report_dawn \ G *************************** 1. row ******************** *******                the Event: report_dawn             the sql_mode:            TIME_ZONE: the SYSTEM         the Create the Event: the CREATE `report_dawn` the EVENT the AT the ON the SCHEDULE '2008-03-21 15:46:57' Call the ON COMPLETION the PRESERVE the DISABLE sp_report the DO () character_set_client: latin1
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)
















collation_connection: latin1_swedish_ci
  utf8_general_ci: Database Collation
1 Row in the SET (0.00 sec)

MySQL>


results after we check for updates:
MySQL> Report from the SELECT *;
+ ---- + ------------ + ------- + ----- +
| the above mentioned id | r_date | the 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 the SET (0.00 sec)
now look at the state of the EVENT,
MySQL> SELECT event_schema, EVENT_NAME, from information_schema.events WHERE event_schema status = 'Event';
+ -------------- + --- + ---------- + ----------
| event_schema | EVENT_NAME | Status |
+ -------------- + ----- + ---------- + --------
| Event | report_dawn | DISABLED |
+ ------- + -------------- + ---------- + ------
1 Row in the SET (0.00 sec)
has stopped working.


MySQL> Report from the SELECT *;
+ ---- + ----- + ------- + ------------ +
| the above mentioned id | r_date | the 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 is |. 1 |
| 18 is | 2008-04-13 | 13 is | 2 |
+ ----- + ---- + ----- + ------- + -------
18 rows in the SET (0.00 sec)


more than nine records,
but after modification time by default.
Do not save it finished running.
Because time has passed.
MySQL> SELECT event_schema, EVENT_NAME, from information_schema.events WHERE event_schema Status = 'Event';
Empty SET (0.00 sec)

the ON COMPLETION [the NOT] the PRESERVE
This option is used to confirm whether the event is stored after the implementation of its definition.

This article comes from " God, let there or be square! " Blog, reproduced please contact the author!

Reproduced in: https: //my.oschina.net/u/585111/blog/219486

Guess you like

Origin blog.csdn.net/weixin_34021089/article/details/92008323