How to get value of last updated row in mysql? - 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 ;

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;

MySQL After Update TRIGGER with IF Conditional Statement

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 ;

MySQL simple trigger can't find the column in another table

I'm new with MySQL. I have this trigger:
DROP TRIGGER IF EXISTS `increment`;
TRIGGER `increment`
AFTER INSERT ON `table1`
FOR EACH ROW
BEGIN
UPDATE table2
SET table2.mycolumn = table2.mycolumn+1
WHERE table2.id = table1.id;
END
It gives error:
Unknown column 'table1.id'
Why? (The column exists). If I use this the trigger works fine:
WHERE table2.id = 1;
Help me understand this, please.
Use NEW or OLD to refer to the columns of the row that caused the trigger to be executed. But not columns of a table you subsequently change in a SQL statement in the body of the trigger.
Example:
TRIGGER `increment`
AFTER INSERT ON `table1`
FOR EACH ROW
BEGIN
UPDATE table2
SET table2.mycolumn = table2.mycolumn+1
WHERE table2.id = NEW.id;
END
In this example, NEW.id refers to the id column of the current row inserted to the increment table.
There is no OLD version of the current row in an insert trigger, because it's a new row.

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?

MySQL trigger INSERT all NEW columns

Is it possible to trigger the insert of all the columns for the NEW row? Basically, I want to make a copy of the newly insert record in another table but what having to specify all the columns.
Something like
DELIMITER $$
CREATE TRIGGER TestTrigger
AFTER INSERT
ON Table1 FOR EACH ROW
BEGIN
INSERT INTO `Table2` SELECT * FROM NEW;
END$$
DELIMITER ;
However this returns Table 'Database.NEW' doesn't exist whenever I try to insert a new row in Table1.
From MySQL documentation:
Within the trigger body, you can refer to columns in the subject table (the table associated with the trigger) by using the aliases OLD and NEW. OLD.col_name refers to a column of an existing row before it is updated or deleted. NEW.col_name refers to the column of a new row to be inserted or an existing row after it is updated.
Despite the time this question have been unanswered, you can SELECT all fields using primary key in source table if you have one.
CREATE TRIGGER replicated_table AFTER INSERT ON source_table FOR EACH ROW
BEGIN
INSERT INTO replicated_table SELECT * FROM source_table WHERE id=NEW.id;
END
Also, maybe you can prefer the use of REPLACE instead of INSERT to ensure the table will not stay out of sincronization.