What is wrong with my mySQL Trigger? - mysql

I am trying to make a simple trigger that logs update events. However I get Error near " BEGIN ...". if I use single quotes, i.e. 'update_log' and 'customer', then the error is near ' 'update_log....'
use tal;
delimiter //
CREATE TRIGGER onUpdate
BEFORE UPDATE ON customer
BEGIN
INSERT INTO update_log VALUES(user(), 'An Udpdate operation against the customer table.', now());
END//
delimiter ;
Why does this not work?

Unlike Oracle mysql needs FOR EACH ROW
delimiter //
CREATE TRIGGER onUpdate
BEFORE UPDATE ON customer
FOR EACH ROW
BEGIN
INSERT INTO update_log VALUES(user(), 'An Udpdate operation against the customer table.', now());
END//
delimiter ;

Related

MySQL Create trigger for get value from another column

i have a table which contains columns as "barkoda1,barkoda2,barkoda3,#pdfa1,#pdfa2,#pdfa3", what i'm trying to do is when a record assigned to barkoda1 copy same record to #pdfa1,for example if barkoda1 defined as "1234567890" #pdfa1 should be "1234567890.pdf".
i tried to create a trigger for it, but failed;
DELIMITER //
CREATE TRIGGER pdfdeneme AFTER INSERT
ON finyeb_ba_2369_bio_for_2__r_d_
FOR EACH ROW
BEGIN
UPDATE finyeb_ba_2369_bio_for_2__r_d_ SET `#pdfa1`=NEW.barkoda1;
END//
DELIMITER ;
when i create this trigger,it doesn't return any error but when i add record to table mysql returns "it is already used by statement which invoked this stored function/trigger".
also tried this way
DELIMITER //
CREATE TRIGGER pdfdeneme BEFORE INSERT
ON finyeb_ba_2369_bio_for_2__r_d_
FOR EACH ROW
BEGIN
INSERT INTO finyeb_ba_2369_bio_for_2__r_d_(`#pdfa1`) VALUES(NEW.barkoda1);
END//
DELIMITER ;
But same error happened, what am i doing wrong here?
You need a BEFORE INSERT TRIGGER
And then only use the SET Clause
DELIMITER //
CREATE TRIGGER pdfdeneme BEFORE INSERT
ON finyeb_ba_2369_bio_for_2__r_d_
FOR EACH ROW
BEGIN
SET NEW.`#pdfa1` = CONCAT(NEW.barkoda1,'.pdf');
END//
DELIMITER ;
But your column name is a bad choice, it leads on the first glance to false conclusions.

Error in MySQL trigger after insert

I am creating trigger for after insert but I am getting error.
DELIMITER //
CREATE TRIGGER ParticleTableInsert
AFTER INSERT
ON Particle FOR EACH ROW
BEGIN
INSERT INTO Particles_Log(Message) VALUES ('Pace_Particles_Log');
END;
DELIMITER ;
I am new to mysql, sonot understanding what is wrong with my query. Please help.
DELIMITER //
CREATE TRIGGER ParticleTableInsert
AFTER INSERT
ON Particle FOR EACH ROW
BEGIN
INSERT INTO Particles_Log(Message) VALUES ('Pace_Particles_Log');
END//
DELIMITER;

Error trigger insert into after insert

Hi guys I am trying to make some triggers to update in one table after insert in other table but it reports me an error. Two tables have the same fields and with the same name.
Here is the code.
DELIMITER //
CREATE TRIGGER Insertar_Bares_Private AFTER INSERT ON Bares
FOR EACH ROW
BEGIN
INSERT INTO Bares_Private (nombrebar, direccion) VALUES (new.nombrebar, new.direccion);
END//
CREATE TRIGGER Insertar_Categorias_Private AFTER INSERT ON Categorias
FOR EACH ROW
BEGIN
INSERT INTO Categorias_Private (nombrecategoria) VALUES (new.nombrecategoria);
END//
CREATE TRIGGER Insertar_Productos_Private AFTER INSERT ON Productos
FOR EACH ROW
BEGIN
INSERT INTO Productos_Private (nombreproducto, idcategoria, descripcion, precio, imagen) VALUES (new.nombreproducto, new.idcategoria, new.descripcion, new.precio, new.imagen);
END//
-- Triggers al actualizar
CREATE TRIGGER Actualizar_Usuarios_Private AFTER UPDATE on Usuarios
for each ROW
BEGIN
UPDATE Usuarios_Private
SET nombreusuario=new.nombreusuario, contrasenia=new.contrasenia, email=new.email, telefono=new.telefono
where idusuario=new.idusuario;
END//
CREATE TRIGGER Actualizar_Bares_Private AFTER UPDATE on Bares
for each ROW
BEGIN
UPDATE Bares_Private
SET nombrebar=new.nombrebar, direccion=new.direccion
where idbar=new.idbar;
END//
CREATE TRIGGER Actualizar_Categorias_Private AFTER UPDATE on Categorias
for each ROW
BEGIN
UPDATE Categorias_Private
SET nombrecategoria=new.nombrecategoria
where idcategoria=new.idcategoria;
END//
CREATE TRIGGER Actualizar_Productos_Private AFTER UPDATE on Productos
for each ROW
BEGIN
UPDATE Productos_Private
SET nombreproducto=new.nombreproducto, idcategoria=new.idcategoria, descripcion=new.descripcion, precio=new.precio, imagen=new.imagen
where idproducto=new.idproducto;
END//
DELIMITER ;
And here is the error reported.
#1235 - This version of MySQL doesn't yet support 'multiple triggers with the same action time and event for one table'
Thank you in advice.
Remove the ; from all the END; and apply the delimiter END// will solve your problem.
Since you already placed the proper ; for the INSERT/UPDATE statement, so no need to place it again in the END;.
You only need to change the delimiter before defining a trigger if the trigger contains more than one query. This is because you need to use the default delimiter ; in this case to separate the queries within the trigger, but the MySQL client will incorrectly think you mean to terminate the trigger definition here. This also requires you to use BEGIN ... END around the block of queries in the trigger.
DELIMITER //
CREATE TRIGGER Insertar_Usuarios_Private AFTER INSERT ON Usuarios
FOR EACH ROW
BEGIN
INSERT INTO Usuarios_Private (nombreusuario, contrasenia, email,telefono) VALUES (new.nombreusuario, new.contrasenia, new.email, new.telefono);
END//
CREATE TRIGGER Insertar_Bares_Private AFTER INSERT ON Bares
FOR EACH ROW
BEGIN
INSERT INTO Bares_Private (nombrebar, direccion) VALUES (new.nombrebar, new.direccion);
END//
-- and so on ...
DELIMITER ;
However, your triggers each only contain one query. Therefore you can dispense with the delimiter and the BEGIN ... END block entirely.
CREATE TRIGGER Insertar_Usuarios_Private AFTER INSERT ON Usuarios
FOR EACH ROW
INSERT INTO Usuarios_Private (nombreusuario, contrasenia, email,telefono) VALUES (new.nombreusuario, new.contrasenia, new.email, new.telefono);
CREATE TRIGGER Insertar_Bares_Private AFTER INSERT ON Bares
FOR EACH ROW
INSERT INTO Bares_Private (nombrebar, direccion) VALUES (new.nombrebar, new.direccion);
-- and so on ...

Trigger with definer error

I'm trying to create a trigger with a definer and some simple action inside, but doesn't get accepted by mysql and freezes the phpMyadmin without giving an error. Can someone help me to find the mistake
delimiter //
CREATE TRIGGER log_items after insert on Profiles
FOR EACH ROW
IF USER() LIKE 'admin#%' THEN
begin
INSERT INTO ItemsLog (`record_id`, `record_time`) VALUES (NEW.id, now());
end; //
END IF;
delimiter ;
Here is full trigger code that is accepted by phpmyadmin. It was generated when using the sql ide: SQLyog.
DELIMITER //
USE `testmysql`//
DROP TRIGGER /*!50032 IF EXISTS */ `log_items`//
CREATE
DEFINER = 'test'#'localhost'
TRIGGER `log_items` AFTER INSERT ON `profiles`
FOR EACH ROW IF USER() LIKE 'admin#%' THEN
BEGIN
INSERT INTO ItemsLog (`record_id`, `record_time`) VALUES (NEW.id, NOW());
END;
END IF;
//
DELIMITER ;
DELIMITER ;

Mysql Trigger Insert record into another table on specific column update

Alright, I am trying to create a trigger in MySQL that will insert the old date in the table into another table to track the changes that occur. The following tables is what I have. I cannot change the users table but I can change the user_changes table. I created this trigger below but it keeps giving me an error on the line with the insert statement.
users: id, StoreNbr, UserNbr, LvlNbr, Paid1, Paid2
user_changes: id, store_nbr, user_number, level_number, OldPaid1, OldPaid2, create
DROP TRIGGER IF EXISTS usersToChanges;
CREATE TRIGGER `usersToChanges` AFTER UPDATE ON users FOR EACH ROW
BEGIN
IF LENGTH(CONV(NEW.Status, 10, 2)) NOT IN (5,6,8) AND (NOT(NEW.Paid1 <=> OLD.Paid1) OR NOT(NEW.Paid2 <=> OLD.Paid2))
THEN
INSERT INTO user_changes (store_nbr, user_number, level_number, OldPaid1, OldPaid2, created)
VALUES (OLD.StoreNbr, OLD.UserNbr, OLD.LvlNbr, OLD.Paid1, OLD.Paid2, NOW());
END IF;
END;
You need to use the DELIMTER command to define procedural code:
DROP TRIGGER IF EXISTS usersToChanges;
DELIMITER $$
CREATE TRIGGER `usersToChanges` AFTER UPDATE ON users FOR EACH ROW
BEGIN
IF LENGTH(CONV(NEW.Status, 10, 2)) NOT IN (5,6,8) AND (NOT(NEW.Paid1 <=> OLD.Paid1) OR NOT(NEW.Paid2 <=> OLD.Paid2))
THEN
INSERT INTO user_changes (store_nbr, user_number, level_number, OldPaid1, OldPaid2, created)
VALUES (OLD.StoreNbr, OLD.UserNbr, OLD.LvlNbr, OLD.Paid1, OLD.Paid2, NOW());
END IF;
END $$
DELIMITER ;
The semicolon usually delimits a single command, not procedural code. Notice how I changed the END; to END $$ to finish making the trigger. Then, I switched back to the default delimiter of semicolon.