Alter mysql event to run only on weekends - mysql

My event runs every 2 hours righ now. But I want to run it only on Saturday and Sundays.
Tried using the weekday() function in alter statement, but I am not sure of the correct syntax to alter the event.
Current functionality: The db event triggers a stored procedure and kicks of every 2 hours.
Any help would appreciated. Thanks!

Related

How to create auto update trigger for MySQL DB?

I've got table with products (changes dynamically) and table for data export. I need a trigger which once a day will add new products from table products into table export. Mine code is:
CREATE TRIGGER auto_update
BEFORE UPDATE
FOR EACH ROW
SET export.name = product.prod_name, export.link=product.prod_link, export.price=product.prod_price
WHERE product.prod_type='product';
But this code don't works. How to fix it? Any ideas?
Since you want this code to execute on scheduled basis, you may want to use the Event Scheduler instead.
Please see here: https://dev.mysql.com/doc/refman/5.7/en/event-scheduler.html
From this you can do things like:
CREATE EVENT e_store_ts
ON SCHEDULE
EVERY 10 SECOND
DO
INSERT INTO myschema.mytable VALUES (UNIX_TIMESTAMP());
This will create a new event to execute every 10 seconds and insert a unix time stamp into table mytable. You can alter events with ALTER and you can drop them with DROP. Have fun!
So in your case create an event that executes every day, that performs the expression you wish to execute (DO clause).

mysql clean a log table when there are too much records

I have a mysql database with a table storing logs. This table receives records every 10 second.
I would like to truncate my table every week, that means after 80.000 records. I'd like to do a stored procedure which checks at every insert if there are more than 80.00. If there are more, the table is truncated.
I can't find how to use the if statement with SQL.
I know it would be better to do a script in a crontab to connect to the db and delete the table once a week, but i have the constraint to do it directly by SQL stored procedure.
Thank you in advance for any help !
You can do this with MySQL Event Scheduler, which is basically like cron, but runs as a thread within the MySQL Server. With it you can schedule the execution of an SQL statement or procedure.
First you have to turn it on:
SET GLOBAL event_scheduler = ON;
Then you can create an event:
CREATE EVENT MyEvent
ON SCHEDULE EVERY 1 WEEK
DO
TRUNCATE TABLE MyTable;
Read more about it here:
https://dev.mysql.com/doc/refman/5.7/en/event-scheduler.html
https://dev.mysql.com/doc/refman/5.7/en/create-event.html

Using mysql event scheduler and php

I would like to delete records from my database every 2 minutes.
I have a user table where I would like to delete users who are active after 2 minutes. I have read a little about using mysql event scheduler but unsure if I can achieve it?
wanted to ask if anybody has previously done anything similar who could help me start ?
You Can Create An Event SCHEDULE on your Mysql Server
First thing You must switch the event SCHEDULE to on that is because its Always off by default Run this Sql Query
SET GLOBAL event_scheduler = ON;
After That You can create an Event Schedule to delete your records from the table every 2 min You can use a query like this
DELIMITER $$
CREATE EVENT IF NOT EXISTS EventName
ON SCHEDULE EVERY 2 MINUTE
DO
BEGIN
DELETE FROM Your Table WHERE Your Conditions if Exists;
END$$
DELIMITER ;
This Event will automatically deletes your specific records every 2 min

Trigger that delete row if currentdatetime > datetime+5

I'm creating a database where i want to create a trigger.
This trigger will delete a row if the current time is 5 min over the requesttime in the table.
The requesttime is a datetime attribute in the table.
How can i make a trigger which do this?
You don't want to do this. First, triggers only run when something is happening inside the database (such as an insert or update). They are not the same as scheduled jobs. Second, you generally don't want to do this in a scheduled job. Instead, just create a view:
create view v_table as
select t.*
from table t
where requesttime >= now() - interval 5 minute;
This will return valid requests.
Then, at your leisure, delete the old rows -- if you really need to. There is a good chance you might want to keep them around to know what happened in the past. But you can delete them on Sunday morning at 3:00 a.m. or some other slow time, if you want.
You may want to consider Event Scheduler added in MySQL 5.1.6. Events can be seen as an alternative to Cronjob.
Their main advantages are:
Platform independent; directly written in MySsql
Ability to list all events SELECT * FROM INFORMATION_SCHEMA.EVENTS;
Built in error logging option
Syntax is something similar to:
CREATE EVENT my_event
ON SCHEDULE
EVERY 1 MINUTE
STARTS '2015-01-01 00:15:00'
COMMENT 'Removes forgotten password tokens older thank 1 week.'
DO
DELETE FROM my_table WHERE some_timestamp > NOW();

How to call a Stored Procedure from a Scheduled Event in MySql using PhpMyAdmin?

I am using PhpMyAdmin console for my MySQL DB.
I have a Routine in DB and I want to call the routine on the last date of every month.
I set up an event and for testing purpose I have set it for 1 min interval:
CREATE EVENT `monthly_report` ON SCHEDULE EVERY 1 MINUTE STARTS '2014-02-05 07:50:00' ON COMPLETION PRESERVE ENABLE DO CALL Test_Proc()
But it is never been called.
Can anyone help me?
Regards,
Surodip