Syntax error with MySql Event - mysql

I'm trying to create this event but I can't find the syntax error.
CREATE
EVENT respaldoRegRawInformeAperturaLocal
ON SCHEDULE EVERY 1 DAY STARTS '2017-08-03 22:00:00'
DO BEGIN
-- INSERT INTO BACKUP TABLE
INSERT INTO regRawInformeAperturaLocalBACKUP (regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId)
-- GET DATA
SELECT regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId
FROM regRawInformeAperturaLocal
WHERE regRawInformeAperturaLocal.date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
-- DELETE DATA FROM ORIGINAL TABLE
DELETE FROM regRawInformeAperturaLocal WHERE regRawInformeAperturaLocal.date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
END;
[CODE EDIT 1]
CREATE
EVENT respaldoRegRawInformeAperturaLocal
ON SCHEDULE EVERY 1 DAY STARTS '2017-08-03 22:00:00'
DO BEGIN
-- INSERT INTO BACKUP TABLE
INSERT INTO regRawInformeAperturaLocalBACKUP (regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId)
-- GET DATA
SELECT regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId
FROM regRawInformeAperturaLocal
WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
-- DELETE DATA FROM ORIGINAL TABLE
DELETE FROM regRawInformeAperturaLocal WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
END;
[EDIT]
This is the syntax error message:
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 11

I could make it work:
I have to add the DELIMITER and change the syntaxis from END; to END|
Anyway here's the code:
DELIMITER |
CREATE
EVENT IF NOT EXISTS respaldoRegRawInformeAperturaLocal
ON SCHEDULE EVERY 1 DAY STARTS '2017-08-03 22:00:00'
DO BEGIN
-- INSERT INTO BACKUP TABLE
INSERT INTO regRawInformeAperturaLocalBACKUP (regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId)
-- GET DATA
SELECT regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId
FROM regRawInformeAperturaLocal
WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
-- DELETE DATA FROM ORIGINAL TABLE
DELETE FROM regRawInformeAperturaLocal WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
END|
DELIMITER ;
Now I have to test it but I could create it so I guess It's fine.
[EDIT]
The event works like a charm.

Related

Automatically update columns values after 6 days

I created a new table in MySQL, I want to make changes in ( column1 and column2 ) columns values automatically in sql.
I tried this code, but their is no changes in table after 6 days.
CREATE EVENT weekly
ON SCHEDULE
EVERY 6 days
STARTS DATE(NOW()) + INTERVAL (6 DAYS(NOW())+1) DAY
ON COMPLETION PRESERVE
DO
UPDATE INTO `MyTable` (`value1`, `value2`)
SELECT `column1` , `column2`)
WHERE `column1` >= NOW() - INTERVAL 6*24 HOUR;
Your UPDATE statement is not correct. It should be like below -
UPDATE `MyTable`
SET `column1` = ???
,`column2` = ???
WHERE `column1` >= NOW() - INTERVAL 6*24 HOUR;
Replace ??? with the the value you want to update in col1 and col2
Not sure how you want to update the value for column1 and 2. But the event should look like this:
DELIMITER //
CREATE EVENT IF NOT EXISTS daily_update
ON SCHEDULE
EVERY 1 DAY STARTS DATE(NOW())
ON COMPLETION PRESERVE
COMMENT 'Update myTable after 6 days'
DO
BEGIN
UPDATE MyTable
SET column1='value1',
column2='value2'
WHERE column1>= (NOW() - INTERVAL 6*24 HOUR);
END
//

AUTO-DELETE ROW 1 Day After Inserted (MySQL)

How to create event Auto-Delete some row 1 day after inserting that row?
I have 3 fields:
id varchar
name varchar
timestamp current_timestamp()
Please take a look at Event: https://dev.mysql.com/doc/refman/5.7/en/events-syntax.html
in your case:
you can try this one:
-- create a timer to update this table automatically
DROP EVENT IF EXISTS `et_update_your_trigger_name`;
CREATE EVENT `et_update_your_trigger_name` ON SCHEDULE EVERY 1 MINUTE
STARTS '2010-01-01 00:00:00'
DO
DELETE FROM `DB_NAME`.`table_name` where DATEDIFF(now(),`timestamp`) > 1;
ALTER EVENT `et_update_your_trigger_name` ON COMPLETION PRESERVE ENABLE;
this sql code create a trigger, and execute every minutes.
Here's your scripts.
Insert into table1 (id, name, timestamp) values (1, 'test', now())
after insert
Delete from table1 WHERE timestamp < now() - interval 1 day;
Please try like this:
DELETE FROM table1
WHERE date < DATE_SUB(NOW(), INTERVAL 1 DAY)

How to validate an inserted record before a delete on a Mysql Event

I have the following MySql event that works perfect but I would like to validate if the record was inserted before the delete just to make sure that I won't loose the record.
This is the MySql event:
DELIMITER |
CREATE
EVENT IF NOT EXISTS respaldoRegRawInformeAperturaLocal
ON SCHEDULE EVERY 1 DAY STARTS '2017-08-03 22:00:00'
DO BEGIN
-- INSERT INTO BACKUP TABLE
INSERT INTO regRawInformeAperturaLocalBACKUP (regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId)
-- GET DATA
SELECT regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId
FROM regRawInformeAperturaLocal
WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
-- DELETE DATA FROM ORIGINAL TABLE
DELETE FROM regRawInformeAperturaLocal WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
END|
DELIMITER ;
Is there any way to validate if the records were inserted before the delete?

mysql trigger function using date add function

I am trying to create a trigger using the date add function in mysql using phpadmin and getting a syntax error; my goal is to automatically add a date in the estimated finish date column ie 'fin_dt' which is 14 days from the date the order is placed. Ie current date plus 14 days. I have come up with the following mysql state but get an error:
DELIMITER $$
CREATE TRIGGER before_insert_orderin BEFORE INSERT ON order_in
BEGIN
SET NEW.fin_dt = SELECT DATE_ADD(CURDATE(), INTERVAL 14 DAY) ;
END $$
Your trigger syntax is wrong. You are missing for each row and also you do not need select to set the value. It should be as
delimiter //
create trigger before_insert_orderin before insert on order_in
for each row
begin
set new.fin_dt = date_add(curdate(),interval 14 day);
end ; //
delimiter ;
Your Trigger syntax near " SET NEW.fin_dt = SELECT DATE_ADD(CURDATE(), INTERVAL 14 DAY)"
SELECT DATE_ADD is Wrong. It should be as
CREATE TRIGGER `before_insert_orderin`
BEFORE INSERT ON `order_in`
FOR EACH ROW
SET NEW.fin_dt=DATE_ADD(NOW(), INTERVAL 14 DAY)

Create Trigger to delete rows that are >90 days old

Trying to create a trigger that will delete any record that is 90 days old. I used a trigger statement from stackoverflow that I have found and changed the statement a bit, but in my MySQL Workbench, I am getting a syntax error. I cannot seem to figure what is wrong.
Below is my query:
create trigger user_connections_dump after insert on user_connections
for each row
begin
delete from connection_time where Date('2014-06-09') > Date('now','-90 days')
end;
Your need looks more like an Event than a Trigger.
CREATE EVENT IF NOT EXISTS `Clean_Older_Than_90_days_logs`
ON SCHEDULE
EVERY 1 DAY_HOUR
COMMENT 'Clean up log connections at 1 AM.'
DO
DELETE FROM log
WHERE log_date < DATE_SUB(NOW(), INTERVAL 90 DAY)
References:
MySQL Event Scheduler on a specific time everyday
CREATE TRIGGER user_connections_dump
AFTER INSERT ON user_connections
FOR EACH ROW
DELETE FROM log
WHERE log_date < DATE_SUB(NOW(), INTERVAL 90 DAY)
You should be comparing the date column in the log table, not a literal date. Then you use DATE_SUB to subtract dates, not the Date function.
CREATE EVENT IF NOT EXISTS `Delete_Older_Than_90_Days`
ON SCHEDULE EVERY 1 DAY
STARTS STR_TO_DATE(DATE_FORMAT(NOW(),'%Y%m%d 0100'),'%Y%m%d %H%i') + INTERVAL 1 DAY
DO
DELETE LOW_PRIORITY FROM log WHERE log_date < DATE_SUB(NOW(),INTERVAL 90 DAY)