MySQL EVENT application

You need to complete a scheduled task in the MySQL database and sort out the knowledge points used.

 

1. Timer

1) Check whether the scheduled task is open before using the timer

show variables like 'event_scheduler';

or

select @@event_scheduler;

2) Enable timed tasks

set global event_scheduler=ON;

or

set global event_scheduler=1;

 

After restarting the database, the scheduled task will not be automatically enabled.

Can be added in the [mysqld] section of my.ini

Configuration for event_scheduler=1.

Or add "--event_scheduler=1" to the startup command

如 mysqld ... --event_scheduler=1

 

3) Create an event

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}

 

ON SCHEDULE is used to specify when the event should be executed.

ON COMPLETION is used to specify whether to repeat the execution after the event is executed.

ENABLE | DISABLE is used to specify whether it is available after being created in advance.

COMMENT is used to add comments to the event.

DO is used to specify the sql statement to be executed by the event.

 

4) Modify the event

ALTER EVENT event_name

[ON SCHEDULE schedule]

[RENAME TO new_event_name]

[ON COMPLETION [NOT] PRESERVE]

[COMMENT 'comment']

[ENABLE | DISABLE]

[DO sql_statement]

 

5) Delete event

DROP EVENT [IF EXISTS] event_name

 

 

2. Stored procedure

The specific syntax will not be explained in detail, just record one point: MySQL should support two sets of syntax rules like the Sybase database.

One is introduced in the official MySQL manual, using declare to explicitly declare variables, and set to set variable values.

The other is to declare variables without declare, directly use set to implicitly declare variables, and set the variable value at the same time.

The difference between the two syntax rules is that the officially advocated usage can use transaction statements such as start transaction, commit, and rollback, while the syntax for implicitly declaring variables is not supported.

 

3. Common functions

3.1. Date String Conversion

1) Convert date to string

DATE_FORMAT(date,format)

Reference: http://www.w3school.com.cn/sql/func_date_format.asp

实例:DATE_FORMAT(NOW(), '%Y%m%d%H%i%s') 

Result: 20160501010101

 

2) Convert string to date

STR_TO_DATE(string,format)

Reference: http://www.mysqltutorial.org/mysql-str_to_date/

实例:STR_TO_DATE('20160501010101','%Y%m%d%H%i%s')

Result: 2016-05-01 01:01:01 of type Date

 

3.2. Date addition and subtraction

1) Add date

DATE_ADD

DATE_ADD(date,INTERVAL expr type)

Reference: http://www.w3school.com.cn/sql/func_date_add.asp

实例:DATE_ADD(NOW(),INTERVAL 1 DAY)

Result: the next day at the current time

 

2) Subtract the date

DATE_SUB

DATE_SUB(date,INTERVAL expr type)

Reference: http://www.w3school.com.cn/sql/func_date_sub.asp

实例:DATE_SUB(NOW(), INTERVAL 1 DAY)

Result: the day before the current time

 

3.3 Date difference

DATEDIFF

DATEDIFF(date1,date2)

Reference: http://www.w3school.com.cn/sql/func_datediff_mysql.asp

Example: DATEDIFF('2016-05-01','2016-04-30')

Result: 1

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326802919&siteId=291194637