How to DO MYSQL TRIGGER FOR DELETE Entry Before UPDATE - mysql

I have an Two table, main table is "Invoice" and sub table is "Invoice_split".
Before update on the "Invoice" table I want to delete the related data on the sub table "Invoice_split" for that I have written the below query But it's not working.
"DELIMITER $$
CREATE TRIGGER before_invoice_update
BEFORE UPDATE ON mac_invoice FOR EACH ROW
BEGIN
DELETE FROM mac_invoice_split WHERE OLD.invoice_id = id;
END$$
DELIMITER ;"
id => "Invoice" table primary key
invoice_id =>foreign key of "Invoice" in "Invoice_split" table

you should try this
WHERE OLD.id = invoice_id;

Related

Trigger updating all rows of a table

I have the date type column valid_upto in the users table which is a foreign key referring to the valid_upto column in the subscription table. I need users.valid_upto to update when a change is made to subscription.valid_upto where the corresponding user ID values match. Currently the trigger updates all rows in the users table when a single row is changed in the subscription table. It basically ignores the WHERE clause.
DROP TRIGGER IF EXISTS `valid_date` ;
CREATE DEFINER = `root`#`localhost` TRIGGER `valid_date` AFTER UPDATE ON `users`
FOR EACH ROW
BEGIN
UPDATE valid_upto SET users.valid_upto = subscription.valid_upto WHERE users.id = subscription.user_id;
END ;

Azure SQL, Before trigger?

I have a relationship between two entities in my database. So before I create a single row I need to delete the row in the relation entity, like so:
user userHous
id 1 1 2
So when I delete a user row, it should do a before trigger and delete userHous.
in Mysql this was easily done with my old trigger:
USE `db`;
DELIMITER $$
CREATE DEFINER=`user`#`ip` TRIGGER `BDEL` BEFORE DELETE ON `user`
FOR EACH ROW
begin
DELETE FROM user WHERE userid = OLD.userid;
DELETE FROM house WHERE userid = OLD.userid;
end
I'm not sure how to go about doing this with { FOR | AFTER | INSTEAD OF }
I made this
CREATE TRIGGER [dbo].[Trigger]
ON [dbo].[user]
INSTEAD OF DELETE
AS
BEGIN
DELETE FROM userHouse WHERE userid = userid;
DELETE FROM user WHERE userid = userid;
END
But this just deletes all rows in both Tables, I guess because I put userid = userid. How do I retrieve the "parameter" which was in the query that made the trigger?
Try this
CREATE TRIGGER [dbo].[Trigger]
ON [dbo].[user]
AFTER DELETE
AS
DECLARE #user_id INT
SELECT #user_id = (SELECT userid FROM DELETED)
BEGIN
DELETE FROM userHouse WHERE userid = #user_id;
END

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;

Trigger : DELETED table does not exist

I am learning SQL trigger and have read some tutorial on how to delete a row on table B when a row in table a is deleted.
I have the following trigger :
CREATE DEFINER=`root`#`localhost` TRIGGER `testDB`.`test_BEFORE_DELETE`
BEFORE DELETE ON `testDBtable` FOR EACH ROW
BEGIN
Delete from testDBtable2 where id = (select id from deleted);
END
Both testDBtable and testDBtable2 have 2 column, id and name.
When I deleted a row from testDB, I received the following error:
Error Code: 1146. Table 'testDB.deleted' doesn't exist 0.000 sec
I am using MYSQLWorkBench.
I think OLD.id is the one you need instead of select id from deleted.
Full query:
CREATE DEFINER=`root`#`localhost` TRIGGER `testDB`.`test_BEFORE_DELETE`
BEFORE DELETE ON `testDBtable` FOR EACH ROW
BEGIN
Delete from testDBtable2 where id = OLD.id;
END

trigger firing another trigger not working

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 ;