MySQL After Update TRIGGER with IF Conditional Statement - mysql

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 ;

Related

How to use trigger to update only one row in table with Mysql

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;

How to update a table in different database with data from current table?

I have two table in two separate databases. However, they are supposed to have the same data. I would like to make sure that whenever I make changes to data in table_a from database_a, they get reflected in table_b from database_b.
Is there any MySQL command that I can run to achieve this?
I read this question: Copy Data from a table in one Database to another separate database but it seems to insert data instead of updating it.
Thanks.
The best way to accomplish this would be with triggers. I haven't tested this, but it gives you the idea.
DELIMITER $$
CREATE
TRIGGER table_a_after_insert AFTER INSERT
ON database_a.table_a
FOR EACH ROW BEGIN
-- update to match columns in your table
INSERT INTO database_b.table_b (
id,
name
)
VALUES (
NEW.id,
NEW.name
);
END$$
CREATE
TRIGGER table_a_after_update AFTER UPDATE
ON database_a.table_a
FOR EACH ROW BEGIN
DECLARE updated_rows INT;
-- again update the column list to match your table
UPDATE database_b.table_b
SET
name = NEW.name
WHERE id = NEW.id;
-- in case the row didn't already exist in table_b, insert it
SET #updated_rows = ROW_COUNT();
IF updated_rows = 0
THEN
INSERT INTO database_b.table_b (
id,
name
)
VALUES (
NEW.id,
NEW.name
);
END IF;
END$$
CREATE
TRIGGER table_a_after_delete AFTER DELETE
ON database_a.table_a
FOR EACH ROW BEGIN
-- obviously update the column list to match the columns in your table
DELETE FROM database_b.table_b
WHERE id = OLD.id;
END$$
You'll have to make sure the user has the right privileges to database_b.table_b
You can use Database triggers (https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html) for something like this.
CREATE TRIGGER triggername AFTER INSERT ON database_a.table_a
FOR EACH ROW
BEGIN
INSERT INTO database_b.table_b (id, ...) VALUES (NEW.id, ...);
END;
You will however have to create triggers for each event. When inserting into table_a you need to insert into table_b, when deleting from table_a you will have to delete from table_b and so on.
€dit: Update for instance could look like this:
CREATE TRIGGER triggername AFTER UPDATE ON database_a.table_a FOR EACH ROW
UPDATE TABLE database_b.table_b
SET table_b.id = NEW.id,
...(SET each column here)...
WHERE table_b.id = NEW.id;

how to sum two columns and insert into total column automatically after every update

I have three columns column 1, column 2, column 3(total). i want to put the sum of column 1 and column 2 into column 3 after every update query..... note i am using code igniter.. how can i do it if i update col 1 then total column automatically update itself.
Try like this:
DELIMITER $$
CREATE TRIGGER updtrigger AFTER UPDATE ON mytable
FOR EACH ROW
BEGIN
IF NEW.col1 <> OLD.col1 OR NEW.col2 <> OLD.col2 THEN
SET NEW.col3 = NEW.col1 + New.col2;
END IF;
END $$
Here is a very good tutorial.
An AFTER UPDATE Trigger means that MySQL will fire this trigger after
the UPDATE operation is executed.
why dont you just update the db instead of creating a trigger. plus youve created the trigger after the insert, it should go before. but really you shouldn't be doing it that way
try this code:
DELIMITER $$
CREATE
TRIGGER `db_name`.`trigger_name` AFTER UPDATE
ON `db_name`.`table_name`
FOR EACH ROW BEGIN
UPDATE
`table_name`
SET
`column3` = `column1` + `column2`;
END$$
DELIMITER ;

mysql trigger to update the same table from another table

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

Same query run on both table using trigger

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