Mysql計時器定時更新功能

create table Status(

statusId int not null auto_increment COMMENT '狀態id',
statusName varchar(50) not null COMMENT '狀態名稱',
PRIMARY KEY ( `statusId` )

)ENGINE=InnoDB DEFAULT CHARSET=utf8;



create table Activity(

actId int not null auto_increment COMMENT '活動id',
actName varchar(100) not null COMMENT '活動名稱',
actImg varchar(255) not null COMMENT '活動海報',
actStartDate datetime not null COMMENT '活動開始日期',
actDeadLine datetime not null COMMENT '活動截止日期',
actRegDate datetime not null COMMENT '報名開始時間',
actRegDeadline datetime not null COMMENT '報名截止時間',
actPlace varchar(100) not null COMMENT '地點',
actNumber int not null  COMMENT'人數限制',
actRequire varchar(100) not null COMMENT '報名要求',
actSlogan varchar(100) not null COMMENT '宣傳口號',
actScore int not null COMMENT '活動積分',
actProfile text not null COMMENT '活動簡介',
actStatus int not null COMMENT '活動狀態',
actTag int not null COMMENT '標籤',
actIsCheck int not null COMMENT '活動是否通過檢驗',
actIsCancel int not null COMMENT '活動取消',
actPromoter int not null COMMENT '活動發起人Id',
PRIMARY KEY ( `actId` ),
FOREIGN KEY (actPromoter) REFERENCES User (userId)

)ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 對這兩個表進行瞎搞(主要針對活動表)

-- 查看是否開啓定時器
SHOW VARIABLES LIKE 'event_scheduler';
-- 開啓定時器 0:off 1:on
SET GLOBAL event_scheduler = 1; 


-- 開啓event_scheduler SQL指令
SET GLOBAL event_scheduler = ON;  
SET @@global.event_scheduler = ON;  
SET GLOBAL event_scheduler = 1;  
SET @@global.event_scheduler = 1;


-- 定義存儲過程
DELIMITER |  
DROP PROCEDURE IF EXISTS update_remind_status |  
CREATE PROCEDURE update_remind_status()  
BEGIN  
    IF exists (select actId from activity where SYSDATE()<actRegDate) THEN  
            update activity set `actStatus`=1 where SYSDATE()<remind_time and `actStatus`!=1;  
    END IF; 
		IF exists (select actId from activity where SYSDATE()>=actRegDate and  SYSDATE()<=actRegDeadline) THEN  
            update activity set `actStatus`=2 where SYSDATE()>=actRegDate and  SYSDATE()<=actRegDeadline and `actStatus`!=2;  
    END IF;  
		IF exists (select actId from activity where SYSDATE()>actRegDeadline and SYSDATE()<actStartDate) THEN  
            update activity set `actStatus`=3 where SYSDATE()>actRegDeadline and SYSDATE()<actStartDate and `actStatus`!=3;  
    END IF;  
		IF exists (select actId from activity where SYSDATE()>=actStartDate and SYSDATE()<=actDeadLine) THEN  
            update activity set `actStatus`=4 where SYSDATE()>=actStartDate and SYSDATE()<=actDeadLine and `actStatus`!=4;  
    END IF;  
		IF exists (select actId from activity where SYSDATE()>actDeadLine) THEN  
            update activity set `actStatus`=5 where SYSDATE()>actDeadLine and `actStatus`!=5;  
    END IF;  
END   
 |  
DELIMITER;  


--創建定時器,每間隔一秒調用一次存儲過程。
DELIMITER //  
CREATE EVENT  event_remind_status  
ON SCHEDULE EVERY 1 second  do  
begin  
call update_remind_status();  
end //  
DELIMITER;  

-- 啓動定時器
ALTER EVENT event_remind_status ON   
COMPLETION PRESERVE ENABLE; 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章