Updating of NEW row is not allowed in after trigger - mysql

Why do I get an error using this trigger?
CREATE TRIGGER save_Assignee AFTER INSERT ON changeitem
FOR EACH ROW
BEGIN
SET new.assignee = (
SELECT assignee
FROM jiraissue INNER JOIN changegroup ON jiraissue.ID = changegroup.issueid
)
END
Error message:
#1362 - Updating of NEW row is not allowed in after trigger

That is correct. You need a before insert trigger if you want to modify the data:
create TRIGGER save_Assignee BEFORE INSERT ON changeitem FOR EACH ROW
BEGIN
SET new.assignee = (select assignee
from jiraissue INNER JOIN
changegroup
ON jiraissue.ID = changegroup.issueid
)
END
As suggested by the name, the AFTER insert trigger is run after the data has been updated. So, if you want to update the same row, use a before trigger.
Your subquery looks suspicious because it is not correlated with new and it looks like it could return more than one row.

Related

Error 1064 Mysql syntax in After Update Trigger

I have an error in my After Update Trigger. I can't figure out where I went wrong. Do you have any suggestions? The trigger is supposed to change the status of another related table (users) when I change the main table I applied my trigger (sites)
CREATE DEFINER=`root`#`localhost` TRIGGER `update_sites_contacts` AFTER UPDATE ON `sites`
FOR EACH ROW
IF NEW.is_mlgu_handled = 1 AND OLD.is_mlgu_handled != NEW.is_mlgu_handled THEN
UPDATE commons_db.users
SET commons_db.users.status = 0
WHERE user_id in (
SELECT user_id FROM
commons_db.sites
LEFT JOIN commons_db.user_organizations using (site_id)
LEFT JOIN commons_db.users using (user_id)
WHERE ewi_recipient = 1
AND org_name in ('lewc','blgu','mlgu')
);
END IF;
Compound statements such as IF...THEN...END IF can only be used inside a BEGIN...END block.

Problem creating a new trigger with a subquery

I'm trying to make a trigger that will update a column whenever there is an insert on another table. In my case, whenever I insert a new like in the table student_likes_post, I want the table forum_post to update its likes column accordingly. This is my query:
use mydb;
DELIMITER //
CREATE TRIGGER update_likes
after INSERT
ON student_likes_post FOR EACH ROW
BEGIN
UPDATE forum_post
SET forum_post.likes = (
select count(*)
FROM student_likes_post
WHERE student_likes_post.post_id = forum_post.id
);
END;
DELIMITER;
However, when I run it, it just keeps running forever, nothing is happening. The subquery is working though individually. I tried other triggers on the same table student_likes_post that have the same issue. Any idea how I can get this to work? Do you think is a problem with the table itself or with the code?
You probably want to update only the row in forum_post for the post that the student liked, not all the forum_posts, right?
Your trigger is currently updating all the rows in forum_post, and running the subquery once for each row in forum_post.
Here's another way to write the trigger, that updates only the single respective forum_post row:
CREATE TRIGGER update_likes
after INSERT
ON student_likes_post FOR EACH ROW
BEGIN
DECLARE like_count INT;
SELECT COUNT(*) INTO like_count
FROM student_likes_post WHERE post_id = NEW.post_id;
UPDATE forum_post
SET likes = like_count
WHERE id = NEW.post_id;
END

mysql double trigger two updates together

After trying to create a new trigger in invoices table to UPDATE `invoices` SET invoices.`owes` = (`owes` - `paid`);
I get an error because I already that another trigger in payments that is updating. (see below)
I'm looking to keep the existing trigger below, but how to modify it to also update owes to (owes - paid) in the invoices table.
CREATE TRIGGER `after_payment_update` AFTER UPDATE
ON `payments`
FOR EACH ROW UPDATE `invoices`
SET invoices.`paid` = (SELECT SUM(payments .`payment`)
FROM payments WHERE payments.`invoice` = invoices.`invoice`)
You can't create a second trigger that "triggers" on the same action as another trigger. Instead you would use a DELIMITER $$ statement like below and fill your trigger with all the relevant code you want executed.
DELIMITER $$
CREATE TRIGGER after_update_payments
AFTER UPDATE ON payments
FOR EACH ROW BEGIN
UPDATE invoices
SET NEW.paid = (SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice),
NEW.owes = (owes -(SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice));
END $$
DELIMITER ;
You don't actually need a DELIMITER in the trigger above, so I will show you an example where you would need to use it:
DELIMITER $$
CREATE TRIGGER after_update_payments
AFTER UPDATE ON payments
FOR EACH ROW BEGIN
IF (some condition here) THEN
UPDATE invoices
SET NEW.paid = (SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice),
NEW.owes = (owes -(SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice));
END IF;
END $$
DELIMITER ;
As a general rule, if you need to execute multiple statements that need a ; at the end of them, you need to use a DELIMITER. If this still doesn't make sense, a great explanation for delimiters can be found here.
Now, on a side-note, I don't think this approach is the most optimal one. What I would do in your situation is create a view that combines these tables. For example:
CREATE
ALGORITHM = UNDEFINED
DEFINER = `root`#`localhost`
SQL SECURITY DEFINER
VIEW invoice_payments_view AS (
SELECT
t1.*,
SUM(t2.payment) as amount_paid,
SUM(t2.owes - SUM(t2.payment)) as amount_owed
FROM invoices t1
JOIN payments t2 ON (t1.invoice=t2.invoice)
GROUP BY t1.invoice
)
Then to access the amount_paid and amount_owed columns, you would simply query the following:
SELECT invoice, amount_paid, amount_owed FROM invoice_payments_view
WHERE invoice=1;
Note that I am by no means an expert on this topic, and I am only showing you how I would approach this situation. Also, I didn't test any of this code, so you might need to modify it slightly. If you have any issues let me know and I can update.

MYSql Trigger not making any changes to db

I have a SQL statement that doesn't appear to want to run as a trigger, it allowed it to be stored as a trigger but it doesn't make any changes, its been added to the DB with phpmyadmin on table 'ebaylinked' to fire after update
CREATE TRIGGER `Update` AFTER UPDATE ON `ebaylinked`
FOR EACH ROW UPDATE product
INNER JOIN ebaylinked e ON ebaylinked.ebay_ID = product.eBay_ID
SET product.product_stock =product.product_stock - ebaylinked.QuantitySold,
ebaylinked.Processed=0
WHERE
ebaylinked.Processed = 1
Does anyone know whats wrong?
Not sure why you have the inner join but could you try the following, this should update product_stock and processed in your product table where product's id = the product's id that was updated:
CREATE TRIGGER `Update` AFTER UPDATE ON `ebaylinked`
FOR EACH ROW UPDATE product
SET product.product_stock =product.product_stock - ebaylinked.QuantitySold,
ebaylinked.Processed=0
WHERE
ebaylinked.Processed = 1 and product.eBay_ID = new.eBay_ID

Update MySQL Row When Updating Using Another Table

I'm trying to use a trigger to update a column on my database when a row gets updated.
This is the trigger
CREATE
DEFINER=`root`#`localhost`
TRIGGER `mysql_development`.`update_translated_position`
BEFORE UPDATE ON `mysql_development`.`players_to_teams`
FOR EACH ROW
BEGIN
UPDATE players_to_teams
INNER JOIN position_translator
ON NEW.position = position_translator.RawPosition
SET NEW.translated_position = position_translator.NCAAposAbbrev1;
END$$
I need to "calculate" the translated_position from the raw position input (in case someone gives me a non-standard position).
I think this is locking the row, because I am getting a 1096, no table used error.
I need to update the players_to_teams row being updated using the external position_translator table.
Use SET directly rather than UPDATE (and therefore avoid the join altogether):
SET NEW.translated_position := (
SELECT NCAAposAbbrev1 FROM position_translator WHERE RawPosition = NEW.position
);