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
Related
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.
iam using MYSQL maria DB and run script
CREATE EVENT 'delete_expire_sku_permonth'
ON SCHEDULE EVERY 1 MONTH STARTS '2017-11-23 00:00:00'
DO BEGIN
DELETE FROM tablestok WHERE system_update < DATE_SUB(NOW(), INTERVAL 30 DAY
END
and have error
#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 ''delete_expire_sku_permonth'
ON SCHEDULE EVERY 1 MONTH STARTS '2017-11-23 00:00' at line 1
What the correct answer ?
I think it's because of the single quotes you put in this line CREATE EVENT 'delete_expire_sku_permonth'. Please try running the same without the single quotes.
Error while creating MySql event scheduling for a specific period of time everyday with an interval of 1 hour.
My code are
CREATE EVENT your_event ON SCHEDULE
EVERY 30 MINUTE
STARTS '2013-10-01 10:00:00'
DO
IF time(CURRENT_TIMESTAMP) between time('10:00:00') and time('18:00:00')
then
INSERT INTO andrew (name) values ('test')
end if
and the error is:
#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 'end if' at line 8
Try this;)
DELIMITER $$
CREATE EVENT your_event ON SCHEDULE
EVERY 30 MINUTE
STARTS '2013-10-01 10:00:00'
DO
IF time(CURRENT_TIMESTAMP) BETWEEN time('10:00:00') AND time('18:00:00') THEN
INSERT INTO andrew (name) VALUES ('test');
END IF $$
DELIMITER ;
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);
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 ;