Drop a MySQL event if a condition is met - mysql

I've a table (table_rne) like
id maxv curv
1 200 20
And I've created an event (increase_curv) which will increase 'curv' by 10 every 1 minute
CREATE EVENT increase_curv
ON SCHEDULE EVERY 8 SECOND
DO
UPDATE table_rne SET curv = curv + 10 WHERE id='1';
But I want to drop the event when 'curv' reaches maxv (200). How to do that using MySQL conditions?

You can try something like this;
CREATE EVENT increase_curv
ON SCHEDULE EVERY 8 SECOND
DO
IF curv < 200 THEN
UPDATE table_rne SET curv = curv + 10 WHERE id='1'
ELSE
DROP EVENT [IF EXISTS] increase_curv
END IF;

Related

Triggers - How to change another column when one is updated - MySQL

I have been given a use case where an user needs to update a certain TIME field if other of the same type changes.
I have been given two columns
horario_arribo (TIME)
horario_salida (TIME)
Now, what I need to do is the followin
Make a trigger so when I change for example horario_arribo, horario_salida is the same TIME as horario_arribo minus 1 hour, and the same if I update horario_salida , make horario_arribo plus 1 hour.
I have been thinking how to do it, first check if the value trying to be updated is horario_salida, then I just add 1 hour to horario_arribo, or if I'm updating horario_arribo, to the same to horario_salida but minus 1 hour.
DELIMITER //
CREATE TRIGGER modificar_horarioruta AFTER
UPDATE ON ruta
FOR EACH ROW
BEGIN
IF(new.horario_salida) THEN
SET AddTime(old.horario_arribo, '00:60:00')
ELSE
SET AddTime(old.horario_salida, Here I dont know how to minus 1 hour to that)
END IF;
END //
DELIMITER ;
This is the data I have
So , in short, if I update horario_salida, horario_arribo need to be 1 hour ahead, if I update horario_arribo, horario_salida needs to be 1 hour less.
Thanks
IIUC:
DELIMITER //
CREATE TRIGGER modificar_horarioruta AFTER
UPDATE ON ruta
FOR EACH ROW
BEGIN
IF (NEW.horario_salida) THEN
SET NEW.horario_arribo = OLD.horario_arribo + 1;
ELSE
SET NEW.horario_salida = OLD.horario_salida - 1;
END IF;
END //
DELIMITER ;
Let me know if this is not what you need.

MySQL events doesn't run eventhough every settings is enable

I have created simple event but it doesn't run:
DELIMITER |
CREATE EVENT myevent3
ON SCHEDULE EVERY 1 second
STARTS current_timestamp()
ENDS CURRENT_TIMESTAMP + INTERVAL 1 DAY
DO
BEGIN
UPDATE hiberante.animal SET name = name + 'a' where id = 6;
END |
DELIMITER ;
I have done:
SET GLOBAL event_scheduler = ON;
Also done:
SELECT * FROM INFORMATION_SCHEMA.EVENTS;
and
SELECT * FROM mysql.event;
And it still doesn't want to start working.
In my process list:
SHOW PROCESSLIST;

Create trigger that gives warning/error

Need help for creating trigger using phpmyadmin that will give warning before inserting data. The situation is much like this:
CREATE OR REPLACE TRIGGER secure_item
BEFORE INSERT
ON item
BEGIN
IF (TRIM(TO_CHAR (SYSDATE, 'DAY')) IN ('SATURDAY', 'SUNDAY'))
OR ( TO_NUMBER(TO_CHAR(SYSDATE, 'HH24')) NOT BETWEEN 8 AND 15 )
THEN
raise_application_error (-20500,
'You may insert into test table only during business hours.'
);
END IF;
END;
/
Replace sysdate with
NOT hour(now()) between 8 and 15 OR weekday(now()) > 4

MySQL sheduler query

I need to delete+update in one MySQL sheduler event
queries:
DELETE FROM access WHERE d > 0;
UPDATE access SET d = 1 WHERE d = 0;
how to combine them in one MySQL event?
First set the event_scheduler ON.
SET GLOBAL event_scheduler = ON;
Now create the event (The event will be scheduled in 5 Minutes
interval)
DROP EVENT IF EXISTS `deleteUpdateEvent`;
delimiter $$
CREATE EVENT `deleteUpdateEvent` ON SCHEDULE EVERY 5 MINUTE STARTS '2016-05-17 00:00:00'
ON COMPLETION PRESERVE ENABLE
DO
BEGIN
DELETE FROM access WHERE d > 0;
UPDATE access SET d = 1 WHERE d = 0;
END$$
delimiter;
Example:
Suppose you have the following data in your access table:
id d
1 66
2 71
3 -6
4 -7
5 0
When the event finishes it's execution (FIRST RUN) your access table would look like below:
id d
3 -6
4 -7
5 1
After second run:
access table:
id d
3 -6
4 -7
Note:
For the subsequent runs your table won't change unless you have new data having >= 0 value
You can try like this:
DELIMITER $$
CREATE EVENT `myEvent`
ON SCHEDULE EVERY 1 MINUTE STARTS 'yourStartTime'
ON COMPLETION NOT PRESERVE ENABLE
DO
BEGIN
DELETE FROM access WHERE d > 0;
UPDATE access
SET d = 1
WHERE d = 0;
END $$
DELIMITER ;

MYSQL: why is the following update trigger not working?

CREATE TRIGGER `add_history` AFTER UPDATE ON `meters`
FOR EACH ROW
CASE inserted.id
WHEN 2 THEN
INSERT INTO waterlog SET deciliter = 5;
WHEN 3 THEN
INSERT INTO eleklog SET kwh = 1;
END CASE
Depending on the row that is updated ( a record with id 2 or 3),i want to run a different query.
meters table looks like
id meter value
2 water 121
3 elek 344
value is regularly updated and need to trigger the creating of a log entry in another table.
Try this:
CREATE TRIGGER `add_history` AFTER UPDATE ON `meters`
FOR EACH ROW
IF (NEW.id = 2) THEN
INSERT INTO waterlog SET deciliter = 5;
ELSEIF (NEW.id = 3) THEN
INSERT INTO eleklog SET kwh = 1;
ENDIF