Trigger to insert a row into another table on delete - mysql

I am trying to create a trigger which will insert a row with all columns into another table:
This is what I've got so far, but it isn't working:
delimiter $$
CREATE TRIGGER item_deleted_move AFTER DELETE ON item
begin
insert into item_deleted (val1, val2, ...) values (old.val1, old.val2, ...);
end$$
delimiter ;

The root of the error is that you miss required FOR EACH ROW clause for CREATE TRIGGER statement.
DELIMITER $$
CREATE TRIGGER item_deleted_moved
AFTER DELETE ON item
FOR EACH ROW -- You missed this part
BEGIN
INSERT INTO item_deleted(val1, val2, ...) VALUES (old.val1, old.val2, ...);
END$$
DELIMITER ;
Now if you don't do anything else in your trigger except for inserting you can simplify it by not using BEGIN ... END block. In that case you don't need to change delimiters.
CREATE TRIGGER item_deleted_moved
AFTER DELETE ON item
FOR EACH ROW
INSERT INTO item_deleted(val1, val2, ...) VALUES (old.val1, old.val2, ...);
Here is a SQLFiddle demo

You changed delimiter to $$ but at the end of the insert you are using ;

Related

MySQL bidirectional trigger

I have to do two triggers, first one, after insert in test1 table, to insert in test2 table another row. All right, the problem is that i have to do same trigger in reverse form, after insert row in test2 table, insert new row on test1. I know that trigger can't insert in a table when a trigger is firing, but i need resolve this. I don't have a lot experience with triggers, i hope that this could be resolved.
DELIMITER $$ CREATE TRIGGER `ai_test1_test2` AFTER INSERT ON `test1`
FOR EACH ROW
BEGIN INSERT INTO test2 (vtest2) VALUES (new.vtest1)
END$$
DELIMITER ;
Bidirectional trigger:
DELIMITER $$ CREATE TRIGGER `ai_test2_test1` AFTER INSERT ON `test2`
FOR EACH ROW
BEGIN INSERT INTO test1 (vtest1) VALUES (new.vtest2)
END$$
DELIMITER ;
Thanks.
Edit:
The infinite loop can't be resolved. Triggers can't be bidirectional... I think that i must be use a PROCEDURE...
The infinite loop can't be resolved. Triggers can't be bidirectional... I think that i must be use a PROCEDURE...
There is a posible solution for the problem:
DELIMITER $$ CREATE TRIGGER `ai_test1_test2` AFTER INSERT ON `test1`
FOR EACH ROW
BEGIN
IF NOT EXISTS( SELECT vtest2 FROM test2 WHERE vtest2 = new.vtest1 ) THEN
INSERT INTO test2 (vtest2) VALUES (new.vtest1)
END IF;
END$$
DELIMITER ;
DELIMITER $$ CREATE TRIGGER `ai_test2_test1` AFTER INSERT ON `test1`
FOR EACH ROW
BEGIN
IF NOT EXISTS( SELECT vtest1 FROM test1 WHERE vtest1 = new.vtest2 ) THEN
INSERT INTO test1 (vtest1) VALUES (new.vtest2)
END IF;
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 ...

I have error "#1363 - There is no NEW row in on DELETE trigger "

I have table "book" and "store_order" have relation
I want to make trigger(but it contain error):
DELIMITER $$
CREATE TRIGGER t1
before delete ON store_order
FOR EACH ROW
BEGIN
update book set number = number + NEW.quantity where ISBN = NEW.ISBN;
END
$$
DELIMITER ;
DELIMITER $$
CREATE
TRIGGER t2 AFTER delete
ON library.store_order
FOR EACH ROW BEGIN
update library.book
set library.book.number = (library.book.number + OLD.quantity)
where library.book.ISBN = OLD.ISBN;
END$$
DELIMITER ;
Use OLD instead of NEW when you want to get the deleted object.
for an example of my case. I'm getting the id of the newly added role by calling
NEW.id
and getting the same field's value while deleting by calling
OLD.id
Example:
DELIMITER $$
CREATE TRIGGER after_insert_role
AFTER INSERT ON role FOR EACH ROW
BEGIN
INSERT INTO `sync_mapping`
(`operation_type`, `table_name`, `oid`, `end_point`)
VALUES
('insert', 'role', NEW.id, 'new/role');
END $$
DELIMITER $$
CREATE TRIGGER after_delete_role
AFTER DELETE ON role FOR EACH ROW
BEGIN
INSERT INTO `sync_mapping` (`operation_type`, `table_name`, `oid`, `end_point`) VALUES
('delete', 'role', OLD.id, 'delete/role');
END $$
Whenever we are deleting the data
USE
OLD. instead of NEW.
CREATE TRIGGER user_history_delete AFTER DELETE ON user FOR EACH ROW
INSERT INTO user_history(action , name )
VALUES ("Delete" , OLD.name );

MySQL Insert Trigger not working with If Then Conditional

I have created the following trigger in mysql:
DELIMITER $$
CREATE TRIGGER `some_trigger` AFTER INSERT ON db1.table
FOR EACH ROW
BEGIN
IF NEW.userid = 'certain_id' THEN
INSERT INTO db2.table
SET
value1 = NEW.value,
value2 = NEW.value;
END IF;
END $$
DELIMITER ;
The above statement works if I remove the if statement. Additionally no syntax errors are encountered when this trigger is added to the db. Any idea what is wrong with the if statement that is not allowing it to insert entries with value 'certain_id' in column userid when added to db1.table??
Try this it should work..
DELIMITER $$
CREATE TRIGGER `some_trigger` AFTER INSERT ON `db1`.`table`
FOR EACH ROW
BEGIN
IF (NEW.userid = 'certain_id') THEN
INSERT INTO `db2`.`table` (value1, value2)
VALUES (NEW.value1,NEW.value2);
END IF;
END $$
DELIMITER ;

How add condition in trigger?

I have query for crete trigger:
CREATE TRIGGER `Insert` AFTER INSERT ON `HistoryBalance`
FOR EACH ROW INSERT INTO Winners (name) VALUES (new.ProductID)
But now i woul like get condition:
if (new.Status = 'user'){
CREATE TRIGGER `Insert` AFTER INSERT ON `Balance`
FOR EACH ROW INSERT INTO Winners (name) VALUES (new.ProductID)
}
Tell me please how right crete trigger with my condition ?
You could do something like this:
delimiter $$
CREATE TRIGGER balance_insert AFTER INSERT ON Balance
FOR EACH ROW
BEGIN
if (new.Status = 'user') then
INSERT INTO Winners(name) VALUES (new.ProductID);
end if;
END; $$
delimiter ;
You can read more Here