trigger firing another trigger not working - mysql

I have four table and two triggers.
Table A tirggerA , when before delete === > insert deleted data into Tabele AAAAA
Table B tirggerB , when before delete === > insert deleted data into Tabele BBBBB
the problem is triggerB works well when delete some data in Table B.
I want something like when deleting data from TableA,
deleted data of Table A inserted into Table AAAAA
deleted data of Table B (related with Table A data) also inserted into Table BBBBB.
I use cascade Delete on Table B and Table A relationship.
The problem is TriggerB never seems to fire when some data is deleted in TableA. Actually it should works because I have Cascade Delete relationship between those tables. Any ideas?

The answer is to just put the delete command in the trigger for table A.
Also you need to put the code in the after delete trigger, in the before delete, the delete has not happened yet and might be reversed.
DELIMITER $$
CREATE TRIGGER ad_tableA_each AFTER DELETE ON tableA FOR EACH ROW
BEGIN
INSERT INTO tableAAAA (f1,f2,f3) VALUES (old.f1, old.f2, old.f3);
DELETE FROM tableB b WHERE b.a_id = old.id; /*use this instead of foreign key*/
END $$
CREATE TRIGGER ad_tableB_each AFTER DELETE ON tableB FOR EACH ROW
BEGIN
INSERT INTO tableBBBB (f1,f2,f3) VALUES (old.f1, old.f2, old.f3);
END $$
DELIMITER ;

Related

MYSQL trigger: delete row from another table

I've got a table that I'm trying to set up for auto-processing updates to a linking table.
When a row is added to the auto-processing table (and there's logic in place for when that happens or doesn't happen), it adds a row that table and then a MySQL Trigger takes over.
The trigger is:
delimiter //
create trigger trg_auto_processing after insert on link_auto_processing
for each row
BEGIN
-- pause
do sleep(2);
-- update link_table
insert ignore into link_table (col_1, col_2) VALUES (NEW.col_1, NEW.col_2);
delete from link_table where col_1 = NEW.col_1 and col_2 != NEW.col_2;
END
//
DELIMITER ;
When testing this, the row is added to link_auto_processing table just fine, the trigger activates and it does the insert into link_table, but it doesn't do the delete.
To my understanding, it can't be a table-lock because it did the insert, So what am I missing here? I've tried putting the delete in a "before insert" trigger on the link_auto_processing table, but that doesn't work either.
I've even tried just updating the link_table with the trigger but that also doesn't work.
Insert works.
Delete doesn't work.
Update doesn't work.
Can't figure out why. Any thoughts?

MySQL select statement result in after delete trigger always null

I'm having a little issue with a trigger in a MySQL database. I have a DB with two tables: "tasks" and "files". The "tasks" table have a field which is a foreign key of the primary key from the "files" table. It also sometimes may be null.
What I'm trying to acomplish is to delete in the first place a row in the "tasks" table, and after that delete the corresponding row in the "files" table using a trigger.
This is the trigger I'm using right now:
DELIMITER //
CREATE TRIGGER after_delete_file AFTER DELETE ON tasks
FOR EACH ROW
BEGIN
DECLARE fileId int;
SELECT file INTO fileId FROM tasks WHERE id=old.id;
DELETE FROM files WHERE id=fileId;
END;//
DELIMITER ;
The field "file" in the "tasks" table is the one containing the foreign key. In the examples I've been running, that field has never been null.
The problem is that the select statement always returns null. The delete statement that triggers this trigger goes fine, but the row in the "files" table is never deleted. I've tried to insert the "fieldId" variable on a testing table, and it's always saving a null value.
Is there any problem on that trigger? Maybe I'm trying to do something merely impossible?
All the help is much appreciated :)
Since it should be looping over each deleted row, why would this not work?
DELIMITER //
CREATE TRIGGER after_delete_file AFTER DELETE ON tasks
FOR EACH ROW
BEGIN
DELETE FROM files WHERE id=old.file;
END;//
DELIMITER ;
If that doesn't work, could try this:
DELIMITER //
CREATE TRIGGER after_delete_file AFTER DELETE ON tasks
FOR EACH ROW
BEGIN
DELETE FROM files INNER JOIN tasks ON files.id=tasks.file WHERE tasks.id=old.id;
END;//
DELIMITER ;
but I don't think that should be necessary.
AFTER delete means that the data is deleted, of course you can't find it. Try creating the trigger for BEFORE delete.
You could also more carefully use all of the old values rather than selecting from the table that was deleted from.

What is MySQL's correct syntax for before delete trigger?

I'd like to create a before delete trigger that deletes rows from two different tables. But I can't figure out which parameters to use.
I got a house table, and when I delete a row, I'd like to delete every row in my two other tables: user_house and firm_house, which contains same house id as the one triggering the event.
What does FOR EACH ROW mean? And how can I properly set my trigger up?
USE `mydb`;
DELIMITER $$
CREATE TRIGGER `deleteUnions` BEFORE DELETE ON `house`
FOR EACH ROW
BEGIN
DELETE FROM user_house WHERE ?? = ??;
DELETE FROM firm_house WHERE ?? = ??;
END
Some details about the structure:
user_house is joined by user_id and house_id;
firm_houise is joined by firm_id and house_id.
Refer to the record that gets deleted in the trigger with OLD. Then use the id to delete from the other tables.
DELETE FROM user_house WHERE house_id = OLD.house_id;
DELETE FROM firm_house WHERE house_id = OLD.house_id;

Deleting A specific table in msql

I would like to create a trigger that will delete all database records from the survey_responses for a given survey_responder when delete a record from survey_responder.
In other words, when I delete a survey_responder I want to delete their responses from the database so that there are no orphan records.
The thing is I keep getting an error. Any help?
DELIMITER $$
CREATE TRIGGER delete_log AFTER DELETE on survey_responders
FOR EACH ROW
BEGIN
delete * from survey_responses
where survey_responders.id = survey_responses.survey_responder_id;
END$$
DELIMITER ;
You might want to remove * from delete query.

How to structure this IF condition in MySQL trigger

I am trying to write a MySQL trigger. I have two tables that look like this:
When a customer makes a purchase a new record is added to each table. I have added column ‘sku_copy’ to Table B, so it does not get populated when a new record is created.
When a new record is created, I want my trigger to copy the ‘sku’ field in Table A to the ‘sku_copy’ field in Table B. However, the problem I am having is how to structure the following condition in the trigger.
IF: ‘order_id’ in Table A matches ‘order_id’ in Table B. THEN: copy ‘sku’ from that Table A record to the record in Table B with the matching ‘order_id’. The data should be added to Table B ‘sku_copy'.
ELSE: don’t do anything.
Can someone show me how to write this into my trigger?
Thanks for any help you can give.
Try this but why do u think of using trigger for this ?
DELIMITER $$
CREATE TRIGGER trigger_name
AFTER INSERT ON tableA
FOR EACH ROW BEGIN
INSERT INTO tableB
SET sku_copy = OLD.sku,
order_id = OLD.order_id,
order = OLD.order;
END$$
DELIMITER ;
I want to post the trigger that was constructed with examples offered here and on another forum. Thanks to everyone who helped with this.
DELIMITER $$
CREATE TRIGGER sku_AfterInsert
AFTER INSERT ON uau3h_virtuemart_order_items
FOR EACH ROW BEGIN
UPDATE uau3h_virtuemart_orders
SET order_item_sku_copy = NEW.order_item_sku
WHERE virtuemart_order_id = NEW.virtuemart_order_id;
END$$;
DELIMITER ;