Mysql event schedule run evry month - mysql

DELIMITER $$
ALTER DEFINER=`root`#`localhost` EVENT `update_start_date` ON SCHEDULE EVERY 1 MONTH
DO
UPDATE categories SET start_date=now() where status = 1 $$
DELIMITER ;
The above event runs only one time at the month beginning but next month begining not run, Le me know how run event every month begining.

I think you should use "cron" for this

I would personally advise that you use cron or some other external schedule management system.
IF you must to mySQL to handle schedules:
DELIMITER $$
ALTER DEFINER=`root`#`localhost` EVENT `update_start_date` ON SCHEDULE EVERY 1 MONTH START NOW()
DO
UPDATE categories SET start_date=now() where status = 1 $$
END$$
DELIMITER ;

You will need to see http://dev.mysql.com/doc/refman/5.1/en/events.html for more details, but here is a simple version.
First you need to enabled the scheduler ->
SET GLOBAL event_scheduler = ON;
and then schedule the actual thing
DELIMITER $$
CREATE EVENT IF NOT EXISTS event_name ON SCHEDULE EVERY MONTH
STARTS CURRENT_TIMESTAMP + INTERVAL 1 DAY
ON COMPLETION PRESERVE
ENABLE
COMMENT 'comment'
DO BEGIN
//SQL statements here.
END $$
DELIMITER ;
Please note, that this requires superuser access to your MySQL database (CRON would require super user access to the server ! )

Related

MYSQL: Create row in table with expiration date

Hello is possible INSERT row to table with expiration date and when the expiration date is reached so row are automatically deleted? Or i only must create one column with expiration date and when sorting checking this value for ignore?
Your need looks more like an Event
IF you want not want to add expiration date column than
CREATE EVENT delete_event ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
ON COMPLETION PRESERVE
DO BEGIN
DELETE FROM Your_table WHERE date>= logic for expiration_date ;
END;
otherwise
DELIMITER $$
CREATE EVENT Delete ON SCHEDULE EVERY 1 DAY
DO
BEGIN
DELETE FROM TABLE WHERE `date_expires` < CURRENT_TIMESTAMP();
END$$
DELIMITER
;
NOTE that MySQL Event Scheduler need to be enabled on your server:
SET GLOBAL event_scheduler = ON;
You cannot do this directly in the database. You can do this by scheduling an event or job that does the delete. You can schedule an event for each deletion.
I think a better approach, though, is to use a view:
create view v_t as
select t.*
from t
where expiration_date > now();
This ensures that the row is not available at the instant the expiration date is no longer in the future. You can then remove the rows at your leisure. Deleting rows can put a load on the database, so you might want to wait until the load is lighter.

MySql Event not working

I have the following script written for event schedule. This is my first time with schedule. I am not getting any errors but it doesn't perform the required operation.
CREATE EVENT checkBlack ON SCHEDULE EVERY 5 SECOND DO
DELETE FROM BlackList WHERE id=13;
It displays this message <n>Query: Create EVENT checkBlack2 on Schedule every 5 second DO Delete from BlackList where id=13
0 row(s) affected
But I have a row with id 13.
Help!!
DELIMITER $$
CREATE /*[DEFINER = { user | CURRENT_USER }]*/ EVENT `checkBlack`
ON SCHEDULE EVERY 5 SECOND
DO
BEGIN
DELETE FROM `BlackList` WHERE id=13;
END$$
DELIMITER ;

Display result of MYSQL event scheduler in an interface

I have a database containing the name and the associated birthday. I have set an event scheduler to periodically check the database for bithdays within two days from the current date. This is my code-
DELIMITER $$ CREATE EVENT `Reminder` ON SCHEDULE
EVERY 1 Day
ON COMPLETION PRESERVE
ENABLE
DO BEGIN
select * from reminder where date_format(date_sub(Birthday,interval 2 day),'%m-%d') = Date_Format(curdate(),'%m-%d');
END $$
DELIMITER ;
Apparently the sql query does not display any information. I would like to display the result of the query in an interface, whenever the query returns at least one row.

How to create a trigger to find first day of the year?

I have a table reservation details which has columns and one of the column is creation date.
I want to create a trigger which checks whether the creation date is first day of the year. If it is then, insert values into to master_ids table. I have created a trigger but don't know whether its correct or not. My project is in php.
CREATE TRIGGER upd_check BEFORE INSERT ON ColdStorage.ReservationDetails
FOR EACH ROW
BEGIN
IF NEW.creationdate = DATE_FORMAT(NOW() ,'%Y-01-01') then
UPDATE master_ids SET nextOccId="1",nextResId="1",nextAgrnoId="1",nextRecNo="1";
END if;
END;
Please suggest me some solutions. I also want that if the date is 01-01-yy then.. its should not insert the row which i m inserting.
You need to change the delimiter. MySQL sees the first ; as the end of the CREATE TRIGGER statement, and thinks the whole statement ends there. So you have to change the delimiter like this:
/* Change the delimiter */
DELIMITER $$
CREATE TRIGGER upd_check BEFORE INSERT ON ColdStorage.ReservationDetails
FOR EACH ROW
BEGIN
IF NEW.creationdate = DATE_FORMAT(NOW() ,'%Y-01-01') then
insert into master_ids values ('0','0','0','0');
END if;
END$$
/* the CREATE TRIGGER statement ends with new delimiter */
/* change the delimiter back to ; */
DELIMITER ;
I haven't tested it, but it seems fine. What bothers me is that this will work just for one day during the year, the other 364 (365) days it is completely useless, and looks like an overhead.

first mysql trigger failing

Ok, I've started writing my first trigger in mysql, it doesn't give an errors, but it doesn't work either...
DELIMITER $$
DROP TRIGGER `cc`.`update_expires_date_trig`$$
CREATE TRIGGER `update_expires_date_trig` BEFORE INSERT ON `credit_test_acc`
FOR EACH ROW BEGIN
UPDATE credit_test_acc SET date_expires_acc = DATE_ADD(CURDATE(), INTERVAL 6 MONTH) WHERE type_acc = 'init'
END;
$$
DELIMITER ;
I have 2 problems:
Can't update table 'credit_test_acc' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Is the trigger as defined going to update JUST the JUST inserted row, or EVERY row in the database?
As far as I know, it must be rewritten like this to work as you expect it to work:
DELIMITER $$
DROP TRIGGER `cc`.`update_expires_date_trig`$$
CREATE TRIGGER `update_expires_date_trig` BEFORE INSERT ON `credit_test_acc`
FOR EACH ROW BEGIN
SET NEW.date_expires_acc = DATE_ADD(CURDATE(), INTERVAL 6 MONTH)
END;
$$
DELIMITER ;
Where NEW refers to the row that is about to be inserted into the table. You didn't give any explanation as to what role 'type_acc' might play here (I can think of more than one way it could be interpreted), so I've left that out. If it is what I think it is, you can apply it like this:
IF NEW.type_acc = 'init' THEN # do whatever you want here
A Trigger cannot change the table that triggered it.
Either directly or indirectly.
You can only change values in a BEFORE trigger by SET new.field = newvalue.
And this can only effect the 'current' row that pulled the trigger (so to speak).