Create Update Trigger To Copy Changed Data In MySQL Syntax Error - mysql

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 ;

Related

Adding comments/notes

I have a trigger as follows:
DELIMITER //
create trigger highStockInsert
After insert on inventory
for each row Begin
if new.currentStock > 250 then Insert INTO higherStockList VALUES (new.prodName, new.StockCount, new.storeName, 'High Alert');
end if; end
// ;
Now when this trigger is act upon I would like mysql to give a message of some sorts, I was messing around with SQLSTATE but if this is triggered the table doesn't update it just blocks the whole insert statement from going through. I was wondering if its possible for the new row to be added but still give off a notification. I was looking if its possible to trigger a warning but found no luck either.
Thanks!

Proper use of BEGIN and END in MySQL

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 ;

Error to create a a trigger

I'm trying to create a trigger that everytime that insert a new data in my table tb_produto_parent, I need to update the columm cod_prod add one more.
Follow the trigger :
DELIMITER $$
create trigger trgAdicionaUm after insert
on tb_produto_parent
for each row
BEGIN
select cod_prod from tb_produto_parent;
update
tb_produto_parent set cod_prod = cod_prod +1;
END;
When I try to execute the code, MySQL show me a error :
Error Code: 1415 Not allowed to return a result set from a trigger.
Thanks !
There are two major problems with your code
you can't use SELECT on it own in a trigger because a trigger doesn't return a resultset to the client
you can't use DML statements (UPDATE in your case) on the same table (tb_produto_parent) on which you have your trigger in MySQL. Therefore even if you fix the first problem you still won't be able to update any row in tb_produto_parent within the trigger.
The only thing you can do in MySQL trigger is to alter values of columns of a row being inserted by using a BEFORE event for a trigger.
A possible solution is to use a stored procedure instead.
Error Code: 1415 Not allowed to return a result set from a trigger.
Looking at your trigger:
BEGIN
select cod_prod from tb_produto_parent;
update
tb_produto_parent set cod_prod = cod_prod +1;
END;
It would seem the select statement is the cause of this error. Remove it.

What is the MySQL equivalent of SQL Server's UPDATE() trigger function?

I need help converting this MS SQL update trigger to MySQL. My problem is with converting the MS SQL UPDATE() function, which identifies the specific columns that were updated:
ALTER TRIGGER [dbo].[tran_upd_action] ON [dbo].[tran_action]
FOR UPDATE AS
BEGIN
IF ##ROWCOUNT>0
BEGIN
IF update(column1) OR update(column2)
BEGIN
INSERT into tran_indexerqueue
(trankey, trkey2, tranname) SELECT tran_actionid, 0, 'tranaction' from inserted
END
END
END
This does not have the rowcount check, but the basic trigger syntax will be like this. Note that only the Super User role can create a trigger. Also, you have to change the delimiter first, which is usually a big gotcha for MSSQL DBAs.
DELIMITER $$
CREATE TRIGGER `tran_upd_action` AFTER UPDATE ON `tran_action` FOR EACH ROW BEGIN
INSERT INTO tran_indexerqueue
(trankey, trkey2, tranname)
VALUES(OLD.trankey, OLD.trkey2, OLD.tranname);
END $$
DELIMITER ;

how to use a block of commands in triggers

The following code works
CREATE
TRIGGER rebuild_course_auto_enrollment_tree_mv AFTER INSERT
ON course_auto_enrollment FOR EACH ROW
DELETE FROM
cron_event_tasks;
If I add the BEGIN ... END as written in the documentation
CREATE
TRIGGER rebuild_course_auto_enrollment_tree_mv AFTER INSERT
ON course_auto_enrollment FOR EACH ROW
BEGIN
DELETE FROM
cron_event_tasks;
END;
This is not working, hmmmm...What am I missing?
Stupid me.
I am using phpmyadmin, and it uses the ; as the delimiter to separate queries.
Solution: change in the query window of phpmyadmin the delimiter to something else and wallah...