Error in SQL syntax when creating event to delete session - mysql

I've made slight modifications to the SQL code provided in an answer to this question: How to delete a MySQL record after a certain time
However, I get a "You have an error in your SQL syntax" error each time I run the query.
create event delete_session
on schedule at current_timestamp + interval 1 day
on completion preserve
do begin
delete from session where date < DATE_SUB(NOW(), INTERVAL 7 DAYS);
end;
The code should create an event to delete entries from the session table after 7 days, but instead gives me this error. Is there actually a problem with the syntax here?

Your CREATE EVENT command and DELETE command is using the ; as delimiter. So your CREATE EVENT command is ending after the DELETE command (before END). You need to set the DELIMITER at the beginning to use another one on the CREATE EVENT command.
-- set the DELIMITER to "|"
DELIMITER |
CREATE EVENT delete_session
ON schedule AT current_timestamp + INTERVAL 1 DAY
ON COMPLETION PRESERVE
DO BEGIN
DELETE FROM session WHERE `date` < DATE_SUB(NOW(), INTERVAL 7 DAY);
END |
-- set the DELIMITER back to the default.
DELIMITER ;
... and you need to change one more thing:
remove the trailing S on DAYS.

Related

Error making an MySQL event

I've got a MySQL table that needs to be emptied every midnight due to information in there being used for sessions.
I've tried to create a MySQL event after turning on the global parameter but it gives me a Syntax error as soon as I want to create one.
This is the error I'm getting.
CREATE EVENT delete_messages_at_midnight
ON SCHEDULE EVERY 1 DAY
STARTS CURDATE() + INTERVAL 1 DAY
DO DELETE FROM t_choices;
delimeter;
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'delimeter' at line 4
Due to me being new here and having lower than 10 rep I can't post images unfortunantly.
Try to remove the semicolon after:
t_choices;
and simply try
delimiter
CREATE EVENT delete_messages_at_midnight
ON SCHEDULE EVERY 1 DAY STARTS CURDATE() + INTERVAL 1 DAY
DO DELETE FROM t_choices
delimeter ;
or like this:
delimiter
|
CREATE EVENT delete_messages_at_midnight
ON SCHEDULE EVERY 1 DAY STARTS CURDATE() + INTERVAL 1 DAY
DO DELETE FROM t_choices;
|
delimeter ; //Put a space between delimiter and semicolon

MySQL EVENT does not execute

I have an event that deletes record on a table that are a month old. But it does not seem to execute.
DELIMITER $
CREATE EVENT delete_wykofile_back
ON SCHEDULE EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP + INTERVAL 1 DAY
ENDS CURRENT_TIMESTAMP + INTERVAL 5 YEAR
ON COMPLETION PRESERVE
DO
BEGIN
DELETE FROM factory.wyko_file_backup WHERE date_inserted < DATE_SUB(NOW(), INTERVAL 1 MONTH);
END$
DELIMITER ;
What I've done so far:
I have followed and set global event_scheduler to ON based from this question
I've also check this question, but to no avail.
There's also no answer from this question.
The account im using is a superuser based from this.
SHOW EVENTS;
SHOW PROCESSLIST;
I am using MySQL Workbench 6.2. I am running the event with 1 minute interval for testing.
STARTS CURRENT_TIMESTAMP + INTERVAL 1 DAY
needs to be the current date or a past date. Right now its set up to start on 12/4/2015.

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)

MySQL Event For Deleting Old Records throws error #1064

I am trying to create an event in MySQL (my first time) that will delete all the records in the "reports" table that are older than 3 months.
The event should run every day at 3:00AM. The reports table has the field "created" of type DATETIME.
This is the code I came up with for creating the event:
CREATE
EVENT `delete_reports`
ON SCHEDULE
EVERY 3 DAY_HOUR
DO BEGIN
DELETE FROM `reports` WHERE `created` < DATE_SUB(NOW(), INTERVAL 3 MONTH)
But this throws the following error:
#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 6
I understand the 1064 error refers to a syntax problem, but I after looking up several different examples I still haven't managed to get this to work.
Any suggestions would be really appreciated!
You're missing an END and with that you should change the delimiter. Or you omit the BEGIN.
So, either like this:
DELIMITER $$
CREATE EVENT `delete_reports`
ON SCHEDULE
EVERY 3 DAY_HOUR
DO BEGIN
DELETE FROM `reports` WHERE `created` < DATE_SUB(NOW(), INTERVAL 3 MONTH);
END $$
DELIMITER ;
or like this:
CREATE EVENT `delete_reports`
ON SCHEDULE
EVERY 3 DAY_HOUR
DO
DELETE FROM `reports` WHERE `created` < DATE_SUB(NOW(), INTERVAL 3 MONTH);

Scedule Event Mysql not accepting syntax BEGIN END

I must have done something really stupid, but the following is correct:
CREATE EVENT delete_old
ON SCHEDULE
EVERY 1 DAY
COMMENT 'Clears old cache data from the DB.'
DO
DELETE FROM summoners
WHERE `date` < (NOW() - INTERVAL 7 DAY);
Where the next bit seems to throw a syntax error on the last 2 lines:
CREATE EVENT delete_old
ON SCHEDULE
EVERY 1 DAY
COMMENT 'Clears old cache data from the DB.'
DO BEGIN
DELETE FROM summoners
WHERE `date` < (NOW() - INTERVAL 7 DAY);
END;
The syntax to my knowledge is correct, however MySQL Workbench does not agree. I intend to do multiple tables inside the BEGIN - END section, that is why i need it.
I hope someone can figure out what goes wrong here, i am at a loss.
Thanks in advance,
Smiley
You have to change the DELIMITER to something that doesn't appear in your event body.
I just tried it (MySQL Workbench 6.0.6, MySQL 5.6) and it works fine. Here's a screenshot:
It's because ; in body breaks your command in the middle. Use different delimiter.
DELIMITER |
CREATE EVENT delete_old
ON SCHEDULE
EVERY 1 DAY
COMMENT 'Clears old cache data from the DB.'
DO BEGIN
DELETE FROM summoners
WHERE `date` < (NOW() - INTERVAL 7 DAY);
END;
| DELIMITER ;