Issue setting event to clear columns in my sql - mysql

I am trying to create an event (ClearTimes) in a mysql database (edata), to run every 24 hrs at 11pm, that clears 2 columns (Shift, Lunchtime). This is what I typed to create the event:
DELIMITER $$
CREATE
EVENT `ClearTimes`
ON SCHEDULE EVERY 1 DAY STARTS '2015-12-05 23:00:00'
DO BEGIN
-- clear "Shift" column
UPDATE edata SET shift = '';
-- clear "Lunchtimes" column
UPDATE edata SET Lunchtime = '';
END $$
DELIMITER ;
The issue is that the event ran on the 5th at 11pm, and then never ran again. I of course want the event to run every 24 hours # 11pm. Any Ideas?

Finally after all this time I figured out the issue, some one was running a restart on the computer for updates so the pc was never on when it was trying to clear... changed the time and it has been working great.
Sorry for the confusion.

Related

MySql Event scheduler is not running

Below is the Event which I have written to be executed every day. I ran the SET GLOBAL event_scheduler = ON; before creating the event still it excutes only first day but from next day onwards it will not run.
Is there anything wrong in this query?
DELIMITER $$
ALTER DEFINER=`root`#`localhost` EVENT `Event1` ON SCHEDULE EVERY 1 DAY STARTS '2017-03-15 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO CALL UpdateLeaves()$$
DELIMITER ;
...ON COMPLETION NOT PRESERVE...
change to
...ON COMPLETION PRESERVE...

MySQL Trigger before update - set dateformat from string

i have a site where i can update the date of a row, by clicking on the table, when i change the value and press enter its updated in the database.
what i want: in the date field. just to type mm/dd (e.g. 05-20) press enter.
then my trigger should:
CREATE TRIGGER before_transactions_update
BEFORE UPDATE
ON Transactions FOR EACH ROW
BEGIN
-- variable declarations
declare inputDate integer;
-- trigger code
-- get month and day
-- LEFT(inputDate, 2); RIGHT(inputDate, 2)
-- STR_TO_DATE('2015,MONTH(LEFT),DAY(RIGHT)','%Y,%m,%d');
SET INSERTED.date = 2015-month-day-00-00-00;
END;
this is what i've "tried" or at least as far as i have come on my own. MySQL workbench stops me at the declaration of my var, it tells me i need a semi colon after declaration, which i dont seem to need
So i've been googling for hours and i've met a dead end as a newbie in MySQL so could anyone fill out the blanks or point me in the right direction,
that would be great
You can not pass the input parameter directly into the trigger unless that input is somewhere in the update statement
Also you need to specify a delimiter in the trigger.
If your input values are always in the form m-d as you shown in the example and the year is the current year then the following trigger should do the job.
delimiter //
CREATE TRIGGER before_transactions_update BEFORE UPDATE
ON Transactions
FOR EACH ROW
BEGIN
if length(new.date) = 5 then
set new.date = concat(year(curdate()),'-',new.date);
end if ;
END;//
delimiter ;

Delete record with Event Scheduler

Just wanted to ask if this Event Scheduler works, also if once ran, it will continue to run as long as mySQL is running as a service?
SET GLOBAL event_scheduler = ON;
CREATE EVENT deleteVistors
ON SCHEDULE EVERY 1 DAY STARTS'2013-08-13 04:00:00'
DO
DELETE FROM tblwhitelist WHERE description = 'Vistors';
Also would this need a delimiter? I'm still unsure to what it actually is!
Hope you can help!
http://dev.mysql.com/doc/refman/5.1/en/create-event.html
Not using ENDS means that the event continues executing indefinitely.
CREATE EVENT deleteVistors
ON SCHEDULE EVERY 1 DAY STARTS '2013-08-13 04:00:00'
-- !!! no *END*: will continue until you explicitly drop the event
DO
DELETE FROM tblwhitelist WHERE description = 'Vistors';
For the second question:
Also would this need a delimiter?
MySQL use the semi-colon as statement delimiter. For multi-line statements (BEGIN ... END, etc.) this could be confusing for your MySQL client as the ; might appears inside those multi-line statements.
Here, you have only one ; so you don't have to bother with that.

MySql Schedule event

I want to insert data in a table at the beginning of every month, I though to use mysql event. But someone told me that if I miss that event (Mean if my server is down in scheduled date/time then mysql will no rerun that event after getting online, not sure) then mysql will not rerun it again.
So I have created a table celled event_balance where event will be logged. Then I created a event called balance_update_check which will run every hour and check if event was executed for this month, if not then it will call a procedure which will create rows for this month and then update table event_balance with event_type and date.
But I am not satisfied with this, please check my codes and tell me is it OK? if possible provide a better solution. Thanks for you time.
Event balance_update_check
CREATE
EVENT `balance_update_check`
ON SCHEDULE EVERY 1 Hour
DO BEGIN
set #check_if_cont_doc:= (SELECT count(*) FROM event_balance WHERE event_type = 'doctor_contract' and monthname(event_balance.date) = monthname(CURDATE()));
set #check_if_cont_other:= (SELECT count(*) FROM event_balance WHERE event_type = 'other_contract' and monthname(event_balance.date) = monthname(CURDATE()));
IF (#check_if_cont_doc = 0) Then
call proc_leave_bal_doc_cont();
Insert into event_balance (event_type,`date`) values('doctor_contract',CURDATE())
END IF;
IF (#check_if_cont_other = 0) Then
call proc_leave_bal_other_cont();
Insert into event_balance (event_type,`date`) values('other_contract',CURDATE())
END IF;
END //
You want to make sure that the event_scheduler is set to ON in your my.cnf file. Head over to your my.cnf file (or my.ini file if you are using Windows) and either comment out the event_scheduler=DISABLED or set event_scheduler=ON. Then restart. This is make sure that your events run after a server reboot. There is some more info about it here:dev.mysql.com/doc/refman/5.5/en/events-configuration.html

MySQL event is not running after an interval

I have created an event in MySQL using this query:
delimiter $$
create event update_usability_score
on schedule every 1 day
starts '2013-01-07 18:22:00'
do
begin
insert into table_name(pk_id,name) values(1,"testing");
update table table_name set name = 'Not testing' where name like '%testing%';
end//
delimiter ;
When I run this for 1st time it's working fine and not showing any error. As I queried this event will run every day. But it is not running everyday.
When I check with this:
select name, last_executed from mysql.event;
it is showing the start date. Means it is not running everyday.
How to run this event everyday? Anything I have missed in the query?
Please help to solve this problem, thanks in advance.
You need to set ON COMPLETION PRESERVE option:
CREATE EVENT update_usability_score
ON SCHEDULE EVERY 1 DAY
STARTS '2013-01-07 18:22:00'
ON COMPLETION PRESERVE
DO
BEGIN
...
END
From the event documentation:
Normally, once an event has expired, it is immediately dropped. You can override this behavior by specifying ON COMPLETION PRESERVE. Using ON COMPLETION NOT PRESERVE merely makes the default nonpersistent behavior explicit.
This particular event would fail because the very first query in the event would fail if you would run it second time as id 1 would be already in the database.