MYSql Trigger not making any changes to db - mysql

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

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 Update on Update trigger

I have a Denormalized database where I have the total points of other table into the driver table. I want to update the total points of the table driver when I update the table where the points are.
Something like this:
CREATE TRIGGER sanciones_trigger BEFORE UPDATE ON points
FOR EACH ROW
UPDATE drivers,
( SELECT pID,SUM(numpoints) AS total_points
FROM drivers
INNER JOIN points ON points.driverID = drivers.pID
GROUP BY drivers.pID
) sum
SET drivers.total_points= sum.total_points
WHERE drivers.pID = sum.pID;
But I cant Update inside the trigger. I also tried with a procedure, but I'm don't really know how to do it.
How can I resolve this?
If the 'numpoints' is available on 'points' table you can just add the delta into 'drivers' on each update through a trigger. However this won't work if you are changing the 'driverID' as well. Please try the following.
here 'NEW' keyword gives you the new value that you are changing to and 'OLD' keyword gives you the value that exists on the table.
CREATE TRIGGER sanciones_trigger BEFORE UPDATE ON points
FOR EACH ROW
UPDATE drivers
SET total_points= drivers.total_points + (NEW.numpoints - OLD.numpoints)
WHERE pID = NEW.driverID;

SQL Trigger - Update table A with sum(tableB.col) WHERE tableA.userID = tableB.userID

I'm a pretty new database student...
I have two tables. User, and Trip.
Whenever a new Trip is added to the database, I want it to trigger an update on the User table in the "totalMiles" column. I want it to take the Trip.userID and change the value in the "totalMiles" column of only the user with the same User.userID as the Trip.userID entry.
I want it to take the sum from "tripMiles" where the Trip.userID = User.userID. userID is a foreign key in the Trip table from the User table.
right now I just have:
CREATE DEFINER='root'#'localhost'TRIGGER 'DATABASE'.'Trip_AFTER_INSERT'
AFTER INSERT ON 'Trip' FOR EACH ROW
BEGIN
UPDATE User
SET totalMiles=sum(tripMiles)
WHERE Trip.userID = User.userID;
END
The error I get is "unknown column 'Trip.userID' in 'where clause'.
I googled and searched through a couple of posts here but I didn't really find anything.
Just change de "User.userID" on where clausule to "NEW.userID"
The "New" object means your new TRIP added...
Something like this:
CREATE TRIGGER 'Trip_AFTER_INSERT'
AFTER INSERT ON 'Trip' FOR EACH ROW
BEGIN
UPDATE User
SET totalMiles=sum(tripMiles)
WHERE User.userID = NEW.userID;
END
If you want more, see documentation here:
https://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html
and here
How to program a MySQL trigger to insert row into another table?
You have multiple problems with your statement, starting with the single quotes. One method is to recalculate the sum:
CREATE TRIGGER DATABASE.Trip_AFTER_INSERT
AFTER INSERT ON Trip FOR EACH ROW
BEGIN
UPDATE User u
SET totalMiles = (SELECT sum(t.tripMiles) FROM Trip t WHERE t.userId = u.userId)
WHERE u.userID = new.userID;
END;
The is overkill, though. If totalMiles is previously correct, then just adjust the value:
CREATE TRIGGER DATABASE.Trip_AFTER_INSERT
AFTER INSERT ON Trip FOR EACH ROW
BEGIN
UPDATE User u
SET totalMiles = u.totalMile + new.tripMiles - old.tripMailes;
WHERE u.userID = new.userID;
END;

Updating of NEW row is not allowed in after trigger

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.