Search code examples
mysqltriggerscronrdbms

Mysql Trigger and Event together?


I have a before insert trigger:

    DELIMITER //
    DROP TRIGGER IF EXISTS product_before_insert //
    CREATE TRIGGER product_before_insert 
    BEFORE INSERT ON product
    FOR EACH ROW
     BEGIN
      IF NEW.created_on='0000-00-00 00:00:00' THEN
       SET NEW.created_on = NOW();
       SET NEW.modified_on = NOW();
       SET NEW.expires_on = ADDDATE(NOW(), INTERVAL 15 DAY);    
     END IF;
    END;//

Well now I want to take the New.created_on as a TIMESTAMP and add a 15 day interval so that one flag(a bool column) in my product table named product.new is set false which is by default true.e.g:

       new boolean NOT NULL DEFAULT 1  #table name:product

So can I do like:

    DELIMITER //
    DROP TRIGGER IF EXISTS product_before_insert //
    CREATE TRIGGER product_before_insert 
    BEFORE INSERT ON product
    FOR EACH ROW
     BEGIN
      IF NEW.created_on='0000-00-00 00:00:00' THEN
       SET NEW.created_on = NOW();
       SET NEW.modified_on = NOW();
       SET NEW.expires_on = ADDDATE(NOW(), INTERVAL 15 DAY);

       CREATE EVENT newflagsetter
       ON SCHEDULE AT NEW.created_on + INTERVAL 15 DAY
       DO
         UPDATE product SET new=0;

     END IF;
    END;//

Thanks a lot for any answers..


Solution

  • Although the documentation says:

    13.1.11. CREATE EVENT Syntax

    ...

    You can create an event as part of a stored routine, but an event cannot be created by another event.

    I think it is outdated, because we read the following:

    E.1. Restrictions on Stored Programs

    ...

    Event Scheduler Restrictions

    • ...
    • An event may not be created, altered, or dropped by a stored routine, trigger, or another event. An event also may not create, alter, or drop stored routines or triggers. (Bug #16409, Bug #18896).
    • ...