Using trigger, when I run update query on query browser for one
table, then I would like this to also update another table that contains the same field.
CREATE DEFINER=`root`#`localhost` TRIGGER `abcd`.`library_AFTER_UPDATE`
AFTER UPDATE ON `library` FOR EACH ROW
begin
-- here i want that same query with same value
-- that i have run on the query browser.
end
What code belongs where I have the comment above?
Use the values from the UPDATE query like below
CREATE DEFINER=`root`#`localhost` TRIGGER `abcd`.`library_AFTER_UPDATE`
AFTER UPDATE ON `library` FOR EACH ROW
begin
UPDATE another_table SET same_field = NEW.same_field
WHERE some_filter_condition;
end
Related
I have an after insert trigger that is supposed to update the field total in my table "test" where the id_cart is equal to new.id_cart. However my trigger is updating every single row in the table not only the one desired. I would like to know how can I modify my trigger so it only updates the row that I want.
This is my trigger.
CREATE DEFINER=`root`#`localhost` TRIGGER `update_total_test`
AFTER INSERT ON `test_product_quantity_cart`
FOR EACH ROW BEGIN
UPDATE test set total= (select sum(price_product) from test_product_quantity_cart where id_cart=new.id_cart);
END
So if the new row inserted in table "test_product_quantity_cart" has an new.id_cart=1, then only the row in table "test" with id_cart=1 should be uptated.
I think I am missing a "where" clause to indicate the update statement which rows it is suppossed to upate. However I do not know how to add that clause.
Thank you!
CREATE DEFINER=`root`#`localhost` TRIGGER `update_total_test`
AFTER INSERT ON `test_product_quantity_cart`
FOR EACH ROW
UPDATE test
JOIN ( SELECT id_cart, SUM(price_product) total
FROM test_product_quantity_cart
WHERE id_cart=NEW.id_cart ) value_for_update USING (id_cart)
SET test.total = value_for_update.total;
I need to create a trigger to update the data on another table when an update statement is made on the present table.
I have two tables, a book table, and a borrowed-book table. On the book table, I have an available copies column and on the borrowed-book table, I have a copy and book-status column.
So presently the trigger I am creating is meant to update the book table available-copies column by adding the values with the data on copies column after an update on the borrowed-book, if the book-status on the update is equal to 'returned'.
what do I do:
BEGIN
DEFAULT #status TEXT
SELECT new.status as status from borrowed_book where borrowed_ID=new.borrowed_ID
if status=="returned"
UPDATE books b
set
b.availableCopies=b.availableCopies + New.copies where b.book_ID=new.book_ID
END
You don't need a query to get the status, just use new.status in the IF statement.
DELIMITER $$
CREATE TRIGGER book_returned AFTER UPDATE ON borrowed_books
FOR EACH ROW
IF new.status = 'returned'
THEN
UPDATE books b
SET b.availableCopies=b.availableCopies + New.copies
WHERE b.book_ID=new.book_ID;
END IF;
$$
DELIMITER ;
I'm trying to set up a trigger which will update same row by inserting some additional data from another table. Field receive.iccid is blank, and I want it to be updated on every insert. However this trigger doesn't work
delimiter //
CREATE TRIGGER ins_iccid
AFTER INSERT ON receive
FOR EACH ROW
BEGIN
UPDATE receive SET NEW.iccid = (SELECT goip.iccid FROM goip WHERE NEW.goipname=goip.name);
END//
delimiter ;
turns out that I need to use 'BEFORE INSERT' to achieve what i was trying to
CREATE TRIGGER `ins_iccid` BEFORE INSERT ON `receive`
FOR EACH ROW
BEGIN
SET NEW.iccid = (SELECT goip.iccid FROM goip WHERE NEW.goipname=goip.name);
END
On MySQL 5.5, I am trying to write a trigger to do the following:
Checks a condition for the insert (is group_id between 8-20?)
If the condition is true, then update the user with the same user_id on another table, called phpbb_users.
The update on this other table is to set user_rank to NEW.group_id from the insert on the table that has the trigger.
Else, do nothing.
Not experienced at all, and stuck. It's not working when I run the query. Here is what I've got so far.
DELIMITER $$
CREATE OR REPLACE TRIGGER upd_rank AFTER INSERT ON `phpbb_user_group`
FOR EACH ROW BEGIN
IF (NEW.group_id BETWEEN 8 AND 20) THEN
UPDATE phpbb_users
SET user_rank=NEW.group_id
WHERE user_id=NEW.user_id;
ELSE
UPDATE phpbb_users
SET user_rank='0'
WHERE user_id=NEW.user_id;
END IF;
END$$;
DELIMITER;
(Note, the "else" clause is unnecessary really - happy for any suggestions for improvement of this code!)
if you only want to get rid of the else try
CREATE TRIGGER upd_rank AFTER INSERT ON `phpbb_user_group`
FOR EACH ROW
UPDATE phpbb_users
SET user_rank=IF(NEW.group_id BETWEEN 8 AND 20,NEW.group_id,"0")
WHERE user_id=NEW.user_id;
MySql trigger is very interesting, but very tricky.
I have some problem, I want to run a trigger once after insert on .
I want to run my trigger once after rows inserted, is there anything like for each table``???
how to make this trigger run only once, but not for each row created.
CREATE
DEFINER=`root`#`localhost`
TRIGGER `mydb`.`leave_taken_trigger`
AFTER INSERT ON `mydb`.`leave`
FOR EACH ROW
BEGIN
set #lt:= (SELECT count(*) FROM `leave` where (staff_leave_application_staff_id_staff = new.staff_leave_application_staff_id_staff and leave_type_id_leave_type = new.leave_type_id_leave_type) and active = 1 );
INSERT INTO `leave_taken`(leave_type_id_leave_type, staff_id_staff, taken_days, updated)
VALUES (new.leave_type_id_leave_type, new.staff_leave_application_staff_id_staff, IFNULL(#lt,0), CURDATE())
ON DUPLICATE KEY UPDATE taken_days=IFNULL(#lt,0), updated = CURDATE();
END$$
I think you can't achieve your requirement with trigger.
Your trigger will run in every row created.
I suggest you to using stored procedure from your code after the INSERT has successfully completed.