Can we have single trigger for multiple tables in MySQL - mysql

Can I have a single trigger for multiple tables in MySQL? I have to perform same task after inserting either of the table_1 or table_2 e.g :
CREATE TRIGGER trigger-1_4_task1
AFTER INSERT ON `table_1`
FOR EACH ROW
BEGIN
.....task1
END //
CREATE TRIGGER trigger-2_4_task1
AFTER INSERT ON `table_2`
FOR EACH ROW
BEGIN
.... same task as task1
END //
Can I combine two above trigger like:
CREATE TRIGGER trigger_4_task1
AFTER INSERT ON `table_1` OR `table_2`
FOR EACH ROW
BEGIN
..... task1
END//
Thanks

Can I have a single trigger for multiple tables in MySQL?
No.
But multiple triggers could invoke the same stored procedure.

Related

Count on trigger

Can we perform count operation inside a trigger?
I have a table TB1 with fields id,title,author,date. I want to create a trigger that when ever I will insert a row in tb1. A trigger should run and the total no of authors in the tb1 should show in a new table total.
I have written the code
DELIMITER $$
CREATE TRIGGER insertintonew2
AFTER
INSERT ON tb1
FOR EACH ROW
BEGIN
UPDATE total
SET total_author = (select count(*) from tb1);
END;
$$
The output I am getting a is populated with total_author column name but the count value is not populated.
Can anyone tell me why is it happening?

How to get value of last updated row in mysql?

I want to insert the last updated row into a new table using trigger ? How to fetch that row if any of the column (value ) from that table has been updated or changed ?
i am using this code
CREATE TRIGGER `database`.`tbl1_AFTER_UPDATE`
AFTER UPDATE
ON `tbl1`
FOR EACH ROW
BEGIN
INSERT INTO tbl2 (c1,c2,c3)
SELECT
c1,c2,c3 from tbl1;
END
But this is inserting all the rows from tbl1 into tbl2, i want only the updated rows to be inserted into tbl2
Within the trigger body, the OLD and NEW keywords enable you to access columns in the rows affected by a trigger. OLD and NEW are MySQL extensions to triggers; they are not case sensitive.Trigger
DELIMITER $$
CREATE TRIGGER `database`.`tbl1_AFTER_UPDATE`
AFTER UPDATE
ON `tbl1`
FOR EACH ROW
BEGIN
INSERT INTO tbl2 (c1,c2,c3) VALUES (OLD.c1,OLD.c2,OLD.c3);
END $$
DELIMITER ;

Is there a way of updating a field on a table and affecting another field on another table?

I have two tables in my database. So what i typically do is insert into tableOne (id, names) values (valueOneInt, valueOneVarChar) but i also want valueOneVarChar to be inserted into another table. Typically I do this separately, is there a shortcut for it?
You could do this with a trigger. E.g.:
delimiter //
CREATE OR REPLACE TRIGGER table_one_insert_tr
BEFORE INSERT ON table_one
FOR EACH ROW
BEGIN
INSERT INTO table_two (varchar_col)
VALUES (NEW.table_one_varchar);
END;
delimiter ;

How to insert before update query in mysql?

I had run two threads, one for insert another one for update in MySQL. The two operations are performed concurrently, my problem was the insert operation will performed first (before insert the update operation will be restricted) before update operation.
create a new trigger, you use the CREATE TRIGGER statement. The following illustrates the syntax of the CREATE TRIGGER statement:
CREATE TRIGGER trigger_name trigger_time trigger_event
ON table_name
FOR EACH ROW
BEGIN
...
END;
Create a trigger after insert
CREATE TRIGGER occupy_trig AFTER INSERT ON occupiedroom
FOR EACH ROW
BEGIN
//update query
UPDATE BookingRequest
SET status = 1
WHERE idRequest = NEW.idRequest;
END
learn more about triggers at techonthenet.com

mysql triggers to multiply two column values by using triggers

i have 4 columns in my table, vrms,irms,total,id.
i have created a trigger where i want to insert the result of the trigger in another table.
my code:
CREATE TRIGGER total_sum BEFORE INSERT ON meter5
FOR EACH ROW BEGIN
INSERT INTO hourly
SELECT vrms,irms,id,
NEW.irms * NEW.vrms AS total;
END
error:
not allowed to return a resultset from a trigger in mysql.
You need the trigger to be after insert something as
delimiter //
create trigger total_sum after insert on meter5
for each row
begin
insert into hourly (id,vrms,irms,total) values (new.id,new.vrms,new.irms,new.vrms*new.irms);
end ; //
delimiter ;