I am running this CREATE EVENT query below, and it does create an event, but if I go in and look at the syntax in Edit Event I see a red x for END but no explanation. Therefore the event is not running, what do I have wrong? Thanks for the help in advance:
DELIMITER !
CREATE EVENT providers_import ON SCHEDULE EVERY 9 DAY_HOUR
ON COMPLETION PRESERVE ENABLE
COMMENT 'providers_old copied'
DO
BEGIN
DROP TABLE providers_old ;
CREATE TABLE providers_old LIKE providers ;
INSERT INTO providers_old SELECT * FROM providers GROUP BY Provider_Last_Name ;
END !
DELIMITER ;
Related
I just switched from using Apache's Derby Database to MySQL and still getting familiar with the syntax. I read the documentation about triggers and I think I followed the syntax correctly. However, I'm having problems with BEGIN and END My insert trigger below doesn't work if I put BEGIN and ENDI even tried putting DELIMITER but it doesn't fix it.
CREATE TRIGGER COPY_INSERTED_USERID_TO_ALLUSERS_PERMISSIONS
AFTER INSERT ON ALLUSERS
FOR EACH ROW
BEGIN
INSERT INTO ALLUSERS_ADMIN_PERMISSIONS(USERID)
VALUES(NEW.USERID);
END;
Removing the BEGIN and END makes it work but I'm not able to take full advantage of the compound statements.
CREATE TRIGGER COPY_INSERTED_USERID_TO_ALLUSERS_PERMISSIONS
AFTER INSERT ON ALLUSERS
FOR EACH ROW
INSERT INTO ALLUSERS_ADMIN_PERMISSIONS(USERID)
VALUES(NEW.USERID);
I'd appreciate any help.
Thanks.
Edited:
I tried to follow #Ilanatos advice which works but returns an error on first attempt. I had to refresh the phpmyadmin page to get rid of the error.
Below are the screenshots.
then if I refresh the page(both Firefox and Chrome), I see the trigger.
I don't think it should return an error message during execution of create trigger definition.
Try using the delimiter function when creating your trigger.
DELIMITER $$
CREATE TRIGGER COPY_INSERTED_USERID_TO_ALLUSERS_PERMISSIONS
AFTER INSERT ON ALLUSERS
FOR EACH ROW BEGIN
INSERT INTO ALLUSERS_ADMIN_PERMISSIONS(USERID)
VALUES(NEW.USERID);
END$$
DELIMITER ;
I have been at this for hours now, and I have tried everything I could find on stackoverflow and the internet. Nothing has worked. I have tried entering the code directly in the SQL prompt on phpmyadmin, as well as tried to create the event in the events tab.
For some odd reason, when giving it a second command, I get a syntax error. Each command on its own is accepted just fine. Together though? Syntax error.
Here's the code.
CREATE EVENT update_stats
ON SCHEDULE EVERY 15 MINUTE
ON COMPLETION PRESERVE ENABLE
DO
BEGIN
UPDATE stats JOIN temp_stats ON stats.unique_key = temp_stats.unique_key
SET stats.clicks = stats.clicks + temp_stats.clicks;
TRUNCATE temp_stats;
END
This yields a syntax error. I found a similar question on stackoverflow (see below) but none of the solutions worked. Yes, I tried setting and using a different delimiter. I even updated phpmyadmin to 4.4.1. Nothing works, just says I have a syntax error. I'm at my wits end here. MySQL 5.6.17.
phpmyadmin|How to create an event do 2 actions
I think this answer is missing an explanation as Jean-Francois was the correct answer but I didn't get it to work until I worked out what the "Delimiter" is.
When sending commands to SQL they are separated with a "Delimiter" the default is ";"
When I was making my event, inside the "CREATE EVENT" statement contains SQL queries inside the "BEGIN" and "END". SO MYSQL, parsing the query, runs into a ';' inside the BEGIN statement and correctly ends the statement there, saying "error in syntax near '' at line X" (the line it found the semi-colon on).
So to get around this you have to change the default delimiter for parsing, then run the query so you can include SQL statements inside your CREATE EVENT query with it ignoring the semi-colon and set the default delimiter back to a semi-colon:
DELIMITER $$
CREATE EVENT `snap_extcrs`
ON SCHEDULE
EVERY 1 DAY STARTS '2020-04-17 23:59:59'
ON COMPLETION PRESERVE
ENABLE
COMMENT ''
DO
BEGIN
INSERT INTO table (field, field2)
SELECT 1, 2 FROM table2
WHERE
date = DATE(NOW());
INSERT INTO table3 (field, field2)
SELECT 1, 2 FROM table2
WHERE
date = DATE(NOW());
END$$
DELIMITER ;
You forgot to put the END delimiter
DELIMITER #
CREATE EVENT update_stats
ON SCHEDULE
EVERY 15 MINUTE
ON COMPLETION PRESERVE ENABLE
DO BEGIN
UPDATE stats JOIN temp_stats ON stats.unique_key = temp_stats.unique_key
SET stats.clicks = stats.clicks + temp_stats.clicks;
TRUNCATE temp_stats;
END#
DELIMITER ;
CREATE TRIGGER backupFIDE AFTER UPDATE ON player
FOR EACH ROW
BEGIN
IF (OLD.FIDERating <> NEW.FIDERating) THEN
INSERT INTO playerbackup(PlayerName, OldFIDERating, NewFIDERating)
VALUES(OLD.PlayerName, OLD.FIDERating, NEW.NewFIDERating)
END;
Hi guys, receiving syntax error at line 6 for some reason, been looking online for solution to what might be the cause. Basically looking a simple trigger, that will copy a change change of FIDERating in Player table to a backup table containing OldFIDERating before it was changed, and the NewFIDERating along with the date change was made.
DELIMITER //
CREATE TRIGGER backupFIDE AFTER UPDATE ON player
FOR EACH ROW
BEGIN
IF NEW.FIDERating <> OLD.FIDERating
THEN
INSERT INTO playerbackup(PlayerName, OldFIDERating, NewFIDERating)
VALUES(OLD.PlayerName, OLD.FIDERating, NEW.NewFIDERating);
END IF;
END;
//
DELIMITER ;
I am working on phpmyadmin.
To write triggers i am using mysql console.
Its works well in starting. but as soon as i am writing a trigger and its gets execute successfully then every time after any query i have to give delimiter (|) to execute the query.
I am not able to understand why i have to put delimiter after a simple select query? Delimiter is for trigger rite.
Am i missing something in writing trigger?
for exmp:
after a trigger i am writing select statement than i have to write it as:
select * from tableName;|
If i am not using | its not getting execute.
Try this :
DELIMITER $$
DROP TRIGGER IF EXISTS `myTriggerName`$$
CREATE TRIGGER `myTriggerName` AFTER DELETE ON `myTableName` FOR EACH ROW BEGIN
...........
............
.............
END$$
DELIMITER ;
After the trigger is created, you need to change the delimiter back to ';'
See examples in the manual:
http://dev.mysql.com/doc/refman/5.6/en/stored-programs-defining.html
delimiter //
... your trigger here ...
delimiter ; <-- change the delimiter back
I have created an event in MySQL using this query:
delimiter $$
create event update_usability_score
on schedule every 1 day
starts '2013-01-07 18:22:00'
do
begin
insert into table_name(pk_id,name) values(1,"testing");
update table table_name set name = 'Not testing' where name like '%testing%';
end//
delimiter ;
When I run this for 1st time it's working fine and not showing any error. As I queried this event will run every day. But it is not running everyday.
When I check with this:
select name, last_executed from mysql.event;
it is showing the start date. Means it is not running everyday.
How to run this event everyday? Anything I have missed in the query?
Please help to solve this problem, thanks in advance.
You need to set ON COMPLETION PRESERVE option:
CREATE EVENT update_usability_score
ON SCHEDULE EVERY 1 DAY
STARTS '2013-01-07 18:22:00'
ON COMPLETION PRESERVE
DO
BEGIN
...
END
From the event documentation:
Normally, once an event has expired, it is immediately dropped. You can override this behavior by specifying ON COMPLETION PRESERVE. Using ON COMPLETION NOT PRESERVE merely makes the default nonpersistent behavior explicit.
This particular event would fail because the very first query in the event would fail if you would run it second time as id 1 would be already in the database.