douweng5420 2016-10-20 13:16
浏览 44

如何使用mysql安排事件和创建触发器

I have 3 tables in my database, tbl_events, tbl_exceptions, tbl_archived_events

tbl_events stores a list of events, this contains the following fields

**eventID** => int(4)Key, AutoIncrement
eventREF => VARCHAR (4) ( will remain the same for each version of the event and will be used to check for an exception)  
eventName => VARCHAR (30) (Name of the event)
eventType => int(4) will determine the type of event it is
eventLocation => VARCHAR (30) hold the event location data
eventDate => DATETIME hold the date of the event
eventStart => DATETIME hold the start time of the event
eventEnd => DATETIME hold the end time of the event
isReoccuring => int(2) Default to 1 which means it is reoccurring 
frequency => VARCHAR (10) will be either Daily/Weekly/Monthly/Yearly
eventLink => VARCHAR (30) will contain a link to the event page if there is one
eventValid => int(2) Will be set to 1 if the event is on and 0 if there is an exception

tbl_exceptions stores a set of events and the dates they are not held. There might be certain times when a reoccurring event might not be held. This table will hold the following field information

**exceptionID** KeyField, AutoIncrement => int(4)
eventREF => VARCHAR (4) Holds the event ref number
exceptionDate => DATETIME , Hold the date of the exception

tbl_archive_events will store the past events that have expired. this table will store the same data as the tbl_events table but for past events only

**eventID** => int(4)Key, AutoIncrement
eventREF => VARCHAR (4) ( will remain the same for each version of the event and will be used to check for an exception)  
eventName => VARCHAR (30) (Name of the event)
eventType => int(4) will determine the type of event it is
eventLocation => VARCHAR (30) hold the event location data
eventDate => DATETIME hold the date of the event
eventStart => DATETIME hold the start time of the event
eventEnd => DATETIME hold the end time of the event
isReoccuring => int(2) Default to 1 which means it is reoccurring 
frequency => VARCHAR (10) will be either Daily/Weekly/Monthly/Yearly
eventLink => VARCHAR (30) will contain a link to the event page if there is one
eventValid => int(2) Will be set to 1 if the event is on and 0 if there is an exception[/CODE]

So once an event has expired i would like mysql to do the following:

  • 1/ if the event is a reoccurring event create a new event and set the eventDate date relative to the length of time specified in the frequently field relative to the existing event.
  • 2/ if the new event eventREF is found in the eventException table then set the eventValid to 0
  • 3/ once the event has expired copy the event to eventsArchive table
  • 4/ delete expired event from tbl_events table

i have this so far

DELIMITER $$
CREATE 
EVENT `new_event` 
ON SCHEDULE EVERY 1 DAY STARTS '2016-07-24 03:00:00' 
DO BEGIN

       -- create new event
       SELECT eventID, eventREF, eventTitle, eventLocation, eventDate, eventStart, eventEnd, isReoccuring, frequency, eventType, eventLink, eventValid
       FROM tbl_events
       WHERE eventDate < now()

       --- for each event found create a new event
       INSERT INTO tbl_events (eventID, eventREF, eventTitle, eventLocation, eventDate, eventStart, eventEnd, isReoccuring, frequency, eventType, eventLink, eventValid) 


       -- copy expired events to tbl_archived_events
       INSERT INTO tbh_archived_events (eventID, eventREF, eventTitle, eventLocation, eventDate, eventStart, eventEnd, isReoccuring, frequency, eventType, eventLink, eventValid) 
       SELECT eventID, eventREF, eventTitle, eventLocation, eventDate, eventStart, eventEnd, isReoccuring, frequency, eventType, eventLink, eventValid
       FROM tbl_events
       WHERE eventDate < now();

       -- delete expired events from tbl_events
       DELETE FROM tbl_events WHERE eventDate < now();

END */$$[/PHP]

obviously the above is not correct and I'm not really sure what i am doing, would appreciate some help please Thanks

Luke

  • 写回答

1条回答 默认 最新

  • drm16022 2016-10-20 21:23
    关注

    You can use mysql scheduler to run it each 5 seconds or every 1 one day.

    http://dev.mysql.com/doc/refman/5.1/en/create-event.html

    Nobody uses this thing


    CREATE EVENT yourevent
    ON SCHEDULE EVERY 1 (SECOND,DAY)
    DO
    CALL YOURSTROREPROCEDURE();


    Create your SP separetly

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?