Error in SQL Trigger - mysql

I have 2 tables, Portfolio and Transactions. When you insert Transaction_Amount value into the Transactions, the Portfolio should be updated too. Below is the table structure.
I created a Trigger so whenever I insert a value into the Transactions, the Portfolio will be updated too.
Below is my MYSQL Trigger
USE `custom_sample`;
DELIMITER $$
CREATE TRIGGER `Transactions_AINS` AFTER INSERT ON `Transactions` FOR EACH ROW
UPDATE Portfolio
SET Invest_Amount = Invest_Amount+Transactions.Transaction_Amount
where
Portfolio.idPortfolio = Transactions.idPortfolio
However this didn't work. It says Unknown column Transactions.idPortfolio in where clause
What is wrong with my script?

The record of the triggered table can be referenced by NEW (or OLD for the values before in case of an update) instead of the table name.
CREATE TRIGGER `Transactions_AINS` AFTER INSERT ON `Transactions`
FOR EACH ROW
UPDATE Portfolio
SET Invest_Amount = Invest_Amount + NEW.Transaction_Amount
WHERE idPortfolio = NEW.idPortfolio

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 - Create trigger to insert a new row based on a row inserted in another table

Hi I have two tables called users and userrewards. When a new row gets inserted into the users table, I want to take the userid and create a new row in the userrewards table, only if the usertype is "premium". In the userrewards table I just need to insert the userid - the rest of the fields are set to default values until the user makes changes themselves. How can I do this via a trigger?
You need to create an "after instert" trigger on user: that's the table you're watching. Now, if I guessed your column names correctly, the sql for creating the trigger should look like this:
CREATE TRIGGER user_ai AFTER INSERT ON user
FOR EACH ROW
BEGIN
IF new.usertype = 'premium' THEN
INSERT INTO userrewards (user_id) VALUES new.id;
END IF;
END;
Another example of a trigger with if statements and :old / :new values can be found here. MySQL documentatation for create trigger here. MySQL documentation for the if-syntax here.

How could I do this trigger delete with validations

I'm new to the forum, i have the following problem, I want after I delete a lease table locations I go in the table films and have the column situation becomes as available, but if I have the code of the movie in the table location in another register, I still leave the column state of the table film as leased, how would I do that?
that's what I've done
TRIGGER `tguDelete` AFTER DELETE ON `locations` FOR EACH ROW UPDATE movies SET situation = 'available' WHERE code_location = OLD.code_location
I wanted to post pictures of the tables, but I'm new to the forum
Please help me!
I use phpmyadmin, and I am new to mysql
I think your trigger definition required some work. For instance - you'd want to declare the "trigger function" which should execute AFTER the DELETE operations on locations table. This is how you can do it:
DROP TRIGGER IF EXISTS `tguDelete`;
DELIMITER $$
CREATE TRIGGER `tguDelete` AFTER DELETE ON `locations`
FOR EACH ROW
-- condition for rows for which the trigger would fire WHEN (OLD.code_location <> 0)
-- DECLARE
-- if you had things you wanted to declare
BEGIN
-- here you do the updating of the movies table:
UPDATE movies SET situation = 'available' WHERE code_location = OLD.code_location;
END;$$
DELIMITER ;
If you don't need DELIMITERS (ie: PHPMyAdmin) then skip them such as
DROP TRIGGER IF EXISTS `tguDelete`;
CREATE TRIGGER `tguDelete` AFTER DELETE ON `locations`
FOR EACH ROW
-- condition for rows for which the trigger would fire WHEN (OLD.code_location <> 0)
-- DECLARE
-- if you had things you wanted to declare
BEGIN
-- here you do the updating of the movies table:
UPDATE movies SET situation = 'available' WHERE code_location = OLD.code_location;
END;

mysql trigger - is there anything like FOR EACH TABLE

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.

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)