Mysql 存储过程+事件 定时备份表
转自:https://blog.csdn.net/xlxxcc/article/details/52487617
1.定义存储过程bakOpLog
CREATEDEFINER=`sa`@`%`PROCEDURE`bakOpLog`()
BEGIN
createtableoperationlog_templikeoperationlog;
set@i=current_date();
--执行renametableoperationlogtooperationlog_yyyy-mm-dd
set@sqlstr=CONCAT('renametableoperationlogto`operationlog_',cast(@iaschar),'`');
select@sqlstr;
PREPARErenameOpLogFROM@sqlstr;
EXECUTErenameOpLog;
renametableoperationlog_temptooperationlog;
END;
2.存储过程用到的一些语法
set@i=current_date();//将全局变量i赋值为当前日期
set@sqlstr=CONCAT('renametableoperationlogto`operationlog_',cast(@iaschar),'`');//sqlstr=renametableoperationlogtooperationlog_yyyy-mm-dd
PREPARErenameOpLogFROM@sqlstr;//定义预处理语句
EXECUTErenameOpLog;//执行预处理语句
查看创建的事件
SHOWEVENTS;
也可以在mysql库中产看event表
1)临时关闭事件
ALTEREVENTe_testDISABLE;
2)开启事件
ALTEREVENTe_testENABLE;
3)将每天清空test表改为5天清空一次:
ALTEREVENTe_test
ONSCHEDULEEVERY5DAY;
4)删除事件(DROPEVENT)
DROPEVENT[IFEXISTS]event_name
例如删除前面创建的e_test事件
DROPEVENTe_test;
当然前提是这个事件存在,否则会产生ERROR1513(HY000):Unknownevent错误,因此最好加上IFEXISTS
DROPEVENTIFEXISTSe_test;