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

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.

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 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 ;

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.

MySQL trigger issue with if clause

I'have a problem trying to create a trigger. I have table_1 and table_2, the update event on table_1 should update a row in table_2 identified using the where clause:
CREATE DEFINER=`root`#`localhost` TRIGGER `update_table_2` AFTER UPDATE ON `table_1` FOR EACH ROW BEGIN
IF table_1.key=0 THEN
UPDATE table_2 SET table_2.value='10' WHERE table_2.key='2';
END IF;
END
What's my problem?!
Thanks
In a trigger, you should use the old and new keywords to reference the record that has been updated (the old values and new ones respectively).
In your case, you should do :
IF new.key=0 THEN
UPDATE table_2 SET table_2.value='10' WHERE table_2.key='2';
END IF;

MySQL Trigger cannot update table - getting ERROR 1442

I have the following trigger:
CREATE TRIGGER sum
AFTER INSERT
ON news
FOR EACH ROW
UPDATE news SET NEW.sum = (NEW.int_views + NEW.ext_views)/NEW.pageviews
It sums the int_views and ext_views column of a table and divides them by the total pageviews.
Whenever I try to add a new row to news, I get the following error:
ERROR 1442 (HY000) at line 3: Can't update table 'news' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
The trigger seems pretty simple to me. Is there a reason why the trigger fails to run?
The symptom is, that you are running an UPDATE (for all rows) inside a INSERT trigger - both modify the table, which is not allowed.
That said, if I guess the intention of your trigger correctly, you do not want to update all rows, but only the newly inserted row. You can achieve that easily with
CREATE TRIGGER sum
BEFORE INSERT
ON news
FOR EACH ROW
SET NEW.sum = (NEW.int_views + NEW.ext_views)/NEW.pageviews
Mind that this is a BEFORE INSERT trigger, as you want to change the row before it is written to the table.
If you try to update/insert on the same table that cause trigger to fire do not use the common sql command like
-> UPDATE TABLE_NAME SET COLUMN_NAME = VALUE WHERE CONDITION_LIST;
-> INSERT INTO TABLE_NAME VALUES("VALUE1","VALUE2");
This will not work. Only use set to assign the value of the column you update.
Example:
CREATE TRIGGER trigger_name BEFORE/AFTER INSERT/UPDATE ON table_name
FOR EACH ROW
SET NEW.COLUMN_NAME = "VALUE";
I was in a similar condition where I had to run two triggers:
UPDATE 3 fields on INSERTing a new ROW
UPDATE 3 fields on UPDATEing a ROW
After lot of efforts, I was finally able to write the TRIGGER in following way:
FOR updating values on INSERT
CREATE TRIGGER `INSERT_DISCOUNT_SERVICES` BEFORE INSERT ON `services`
FOR EACH ROW SET
NEW.discount_5_rate = (NEW.ndis_rate*0.05),
NEW.discount_10_rate=(NEW.ndis_rate*0.10),
NEW.discount_15_rate=(NEW.ndis_rate*0.15)
Similarly
FOR updating values on UPDATE
CREATE TRIGGER `UPDATE_DISCOUNTS_SERVICES` BEFORE UPDATE ON `services`
FOR EACH ROW SET
NEW.discount_5_rate = (NEW.ndis_rate*0.05),
NEW.discount_10_rate=(NEW.ndis_rate*0.10),
NEW.discount_15_rate=(NEW.ndis_rate*0.15)