mysql定时调用存储过程,对表数据集表结构进行备份
存储过程实例:
BEGIN
DECLARE tname varchar(64);
set @tname = CONCAT('RENAME TABLE sms_accpet TO sms_accpet',DATE_FORMAT(NOW(),'%Y%m'));
PREPARE pre_tname from @tname;
EXECUTE pre_tname; set @newtable = CONCAT('create table sms_accpet select * from sms_accpet_template where 1=2');
PREPARE pre_newtable from @newtable;
EXECUTE pre_newtable; alter table sms_accpet add primary key (id);
alter table sms_accpet modify column id int unsigned not null auto_increment;
END
定时器实例
CREATE EVENT EVENT_SMS
ON SCHEDULE EVERY 1 MONTH STARTS DATE_ADD(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL DAY(CURDATE())-1 DAY), INTERVAL 1 MONTH),INTERVAL 1 HOUR)
ON COMPLETION PRESERVE ENABLE
DO CALL backUpSms();
參考站点:
存储过程:
http://blog.****.net/youngqj/article/details/6936632
http://blog.****.net/sun886/article/details/7992935
定时器:
http://www.cnblogs.com/gaizai/archive/2012/12/24/2831315.html
http://lobert.iteye.com/blog/1953827
1.复制表结构及数据到新表 CREATE TABLE 新表
SELECT * FROM 旧表 2.仅仅复制表结构到新表 CREATE TABLE 新表
SELECT * FROM 旧表 WHERE 1=2
即:让WHERE条件不成立.
方法二:(低版本号的mysql不支持,mysql4.0.25 不支持,mysql5已经支持了)
CREATE TABLE 新表
LIKE 旧表 3.复制旧表的数据到新表(如果两个表结构一样) INSERT INTO 新表
SELECT * FROM 旧表 4.复制旧表的数据到新表(如果两个表结构不一样) INSERT INTO 新表(字段1,字段2,…….)
SELECT 字段1,字段2,…… FROM 旧表
例子抽选
每天凌晨三点运行
create event event_call_defer
on schedule every 1 day starts date_add(date(curdate() + 1),interval 3 hour)
on completion preserve enable
do
begin
call test.warn();
end 每一个月的一号凌晨1 点运行
CREATE EVENT EVENT2
ON SCHEDULE EVERY 1 MONTH STARTS DATE_ADD(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL DAY(CURDATE())-1 DAY), INTERVAL 1 MONTH),INTERVAL 1 HOUR)
ON COMPLETION PRESERVE ENABLE
DO
BEGIN
CALL STAT();
END 每一个季度一号的凌晨2点运行
CREATE EVENT TOTAL_SEASON_EVENT
ON SCHEDULE EVERY 1 QUARTER STARTS DATE_ADD(DATE_ADD(DATE( CONCAT(YEAR(CURDATE()),'-',ELT(QUARTER(CURDATE()),1,4,7,10),'-',1)),INTERVAL 1 QUARTER),INTERVAL 2
HOUR)
ON COMPLETION PRESERVE ENABLE
DO
BEGIN
CALL SEASON_STAT();
END 每年1月1号凌晨四点运行
CREATE EVENT TOTAL_YEAR_EVENT
ON SCHEDULE EVERY 1 YEAR STARTS DATE_ADD(DATE(CONCAT(YEAR(CURDATE()) + 1,'-',1,'-',1)),INTERVAL 4 HOUR)
ON COMPLETION PRESERVE ENABLE
DO
BEGIN
CALL YEAR_STAT();
END