Mysql event event usage

The company's database needs to delete data regularly, and it needs to use mysql event events to learn and sort out this knowledge.

1 Check if the event is enabled with
SHOW VARIABLES LIKE 'event_scheduler';

2 Open event
SET GLOBAL event_scheduler = 1; 

3 View all events
select * from mysql.event \G

4 delete event
drop event event_name

5语法:
CREATE
[DEFINER = { user | CURRENT_USER }]
EVENT
[IF NOT EXISTS]
event_name
ON SCHEDULE schedule
[ON COMPLETION [NOT] PRESERVE][ENABLE | DISABLE | DISABLE ON SLAVE]
[COMMENT 'comment']
DO event_body;

schedule:
AT timestamp [+ INTERVAL interval] ...
| EVERY interval
[STARTS timestamp [+ INTERVAL interval] ...][ENDS timestamp [+ INTERVAL interval] ...]

interval:
quantity {YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE |WEEK | SECOND | YEAR_MONTH | DAY_HOUR | DAY_MINUTE |DAY_SECOND | HOUR_MINUTE | HOUR_SECOND | MINUTE_SECOND}

Parameter analysis
event_name: the name of the event
schedule: the event trigger point, [AT timestamp] is generally used to execute only once, and the current time plus a delay period can be used in general use, for example: AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR. You can also define a time constant, for example: AT '2006-02-10 23:59:00'; [EVERY interval] is generally used for periodic execution, and the start time and end time can be set.
ON COMPLETION [NOT] PRESERVE: The default is to delete automatically after execution. If you want to keep the event and use ON COMPLETION PRESERVE, you can use the select query statement to query it; if you don't want to keep it, you can also set ON COMPLETION [NOT] PRESERVE.
ENABLE | DISABLE: Set to enable or disable this event.
COMMENT: Add a comment.

6 Case:
Create a test table with only one field of data type named time
Enter a piece of data in the table every 5 seconds.
delimiter |
CREATE EVENT test1
ON SCHEDULE every 5 second
ON COMPLETION PRESERVE
DO BEGIN
insert into test(time) values(now());
END |
delimiter ; Delimiter | CREATE EVENT test2 ON SCHEDULE EVERY
20 second ON COMPLETION PRESERVE DO BEGIN delete from test; END | delimiter ;







delimiter | Replace the end of the statement with |, and finally with;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325027528&siteId=291194637