Working with Events in MySQL

前端 未结 1 1053
太阳男子
太阳男子 2021-01-16 00:30

I have a stored procedure which basically selects data from one table and insert into another table. Basically I am doing data archiving manually. Now, I want to write an ev

相关标签:
1条回答
  • 2021-01-16 00:51

    I believe you are thinking this in the oposite direction: You can't create an event in a stored procedure, but you can create a stored procedure and call it from an event.

    Example:

    delimiter $$
    create procedure myProc()
    -- Dummy procedure to move the data from mainTable to backupTable, 
    -- and then clear (truncate) mainTable
    begin
        insert into backupTable select * from mainTable;
        truncate mainTable;
    end $$
    delimiter ;
    
    -- Now, suposing that you want to execute this procedure every hour:
    delimiter $$
    create event myEvent
        on schedule every 1 hour
        do
            begin
                call myProc();
            end $$
    delimiter ;
    

    You can write this as any other query in the workbench, or directly in the command line client.


    About your concern

    After reading your comment, I believe you are a bit confused about what MySQL Workbench is.

    MySQL Workbench is only a graphical application that allows you to connect to a MySQL server and perform queries and administration tasks. But Workbench is not the core of MySQL... it is only a viewer (with steroids, maybe, but a viewer after all).

    Now, the event scheduler does not reside in Workbench, but in the MySQL server instance you are connecting to. Just as the tables, views, procedures and functions are not stored in the Workbench interface but in the server, the events are also stored in the server.

    (Yes, I believe it is a relevant SNAFU that the scheduled events don't show anywhere in the graphical interface, but... after a while, one learns to live with that kind of frustrations and to move on with life)

    Maybe your only concern is: "Hey, and what if I want to know what events are set to run in the event scheduler?" You can execute a "show events" query to show a list of the events in the current database, and you can execute "show create event yourEvent" to show the create event syntax for that event.

    I insist: Read the manual, and keep a copy at hand (download the manual for your MySQL version here).

    0 讨论(0)
提交回复
热议问题