MySQL timed task

Source: Internet
Author: User

Click the link to join the group "Php/web Learning class": Https://jq.qq.com/?_wv=1027&k=5645xiw

Welcome to join us and discuss learning together.

Here we have to complete the scheduled task, relatively simple

Effect: Every x seconds some user points plus X

First step: We first create a table and insert the data

/*Create a table*/CREATE TABLE' test_name ' (' ID ')int( One) not NULLAuto_increment COMMENT'primary key, self-increment' ,`user_name`varchar(255)CHARACTER SETUTF8 COLLATE utf8_general_ciNULLCOMMENT'User name', ' Integral 'int( One)NULLCOMMENT'points', ' Is_adm 'tinyint(1)NULLCOMMENT'Admin, 1-yes, 0-not' ,PRIMARY KEY(' id ')) ENGINE=MyISAMDEFAULT CHARACTER SET=UTF8 COLLATE=Utf8_general_cichecksum=0Delay_key_write=0;/*Add Data*/INSERT  into' Test_name 'VALUES('1','Members 1','0','1');INSERT  into' Test_name 'VALUES('2','Members 2','0','0');INSERT  into' Test_name 'VALUES('3','Members 3','0','0');

Look at the effect:

?

OK, the table and the data are ready, let's talk about the requirements:

Requirement 1: Not after 2 seconds Administrator's points plus 1, the field Is_adm 1 is the administrator

The above is for adding tables and data, and presenting requirements

--------------------------------Split Line--------------------------------

Below, we will mention how to start a scheduled task to complete our needs

First of all, we have to check our database to see if the event is turned on.

 like ' Event_scheduler ' #查看状态

Run the above code, if your result, the Value field is "off", on behalf of your event is off state, then we will open it, run the following code:

SET = on;  #ON开启, off off

After running, return to the previous, check the status of the event again, whenthe result is "on", then the event is open, we continue to go down.

Since it's a timed task, we're going to encapsulate a function, and the process is this:

    1. Encapsulates a function
    2. Encapsulates an event
    3. Event starts running, satisfies condition, calls 1 (encapsulated function)

So let's wrap up a function first:

#写一个函数  begin-----------CREATEPROCEDUREbegin  updateSET integral=+1WHERE=1  End,#写一个函数   End-- ---------- 

After running the above code successfully, you do not have to worry about, not immediately modify your data, because here just encapsulates a function, we did not call it, it just exists. So we need to write an event to call it

#写一个事件  begin------------Createifnotexists  on2    On completion preserve does call Test (); #写一个事件 End -- ----------

Run the above code, after the success, we also do not worry, do not immediately run this event. Although above, we have opened the event. But we also want to run a single, specify an event to run, just like the Access interface, specify an interface ...

#开启事件 Alter  on

Here can see, open the event, specified is E_test this event name, is the same as we created above, then after running this piece of code, you will find that your table test_name, Is_adm = 1 of the line, the field is integral (integral) will be 1 per 2 seconds

So for a simple scheduled task, we're done.

Oh, yes, close a single event, using the following code:

#关闭事件 Alter  on

After running, the use of the E_test event is stopped

The above need to note:

    • The event is off by default and can be found in the MySQL configuration, if you change it to on in the configuration, it will default to ON.
    • After you close the database, if your event is off, it will be stopped because of the default off in the configuration.
    • When event is closed, your individual event is turned on, and when you turn on event, the status of the individual event is still open.
    • If you want to close an event, use the last "Shutdown event" code to close, do not use event shutdown, and affect other events
    • event is like the total gate, after shutdown, all defined events are powered off

MySQL timed task

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.