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
Related
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;
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.
USE test;
CREATE TRIGGER AvgUpdateTrigger AFTER INSERT ON test.score
FOR EACH ROW
BEGIN
INSERT INTO test.average (test.average.TestID, test.average.TestAvg)
(SELECT test.score.TestID, avg(test.score.ScoreValue) FROM test.score GROUP BY test.score.TestID)
ON DUPLICATE KEY
UPDATE test.average.TestAvg = (SELECT avg(test.score.ScoreValue) FROM test.score WHERE test.score.TestID = test.average.TestID GROUP BY test.score.TestID);
END;
im trying to update one table(average) when another one gets changed(score)
it is telling me to add a semicolon but as you can see there is one there allready
If a trigger (or any stored procedure) contains only one statement, you don't need BEGIN and END:
CREATE TRIGGER AvgUpdateTrigger AFTER INSERT ON test.score
FOR EACH ROW
INSERT INTO test.average (TestID, TestAvg)
SELECT test.score.TestID, avg(test.score.ScoreValue)
FROM test.score
GROUP BY test.score.TestID
ON DUPLICATE KEY UPDATE
test.average.TestAvg = VALUES(TestAvg);
I also replaced the subquery with VALUES(TestAvg), since this value has already been selected.
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;
I have an issue with a trigger on a mysql database. I have a table such as follows:
id int not null auto_increment (PK)
parent_id int not null,
rank int not null
What I'm trying to do is use a trigger to update the rank to the next highest +10 when they have the same parent_id, but this doesn't seem to be working.
DELIMITER $$
DROP TRIGGER IF EXISTS after_insert $$
create trigger after_insert
after insert on mytable
FOR EACH row
BEGIN
IF EXISTS (SELECT rank FROM mytable WHERE parent_id = new.parent_id AND id != new.id ORDER BY rank DESC LIMIT 1) THEN
UPDATE mytable SET rank = 10
WHERE id = new.id;
ELSE
UPDATE mytable SET rank = 20
WHERE id = new.id;
END IF;
END
$$
I've tried setting the new rank to a variable and calling the update statement using that, and again it didn't work. I even created another table to log what values were being selected and that worked perfectly so I can't quite understand what's going on. Is it a case of, although the trigger is "AFTER INSERT" the insert hasn't actually happened so it can't update the row it's just inserted? Another reason I ask this is, I've even tried updating the rank to different values e.g 1 and 2 depending on which statement it goes to, but it always ends up being 0.
I think you're on the right track with this thought:
Is it a case of, although the trigger is "AFTER INSERT" the insert hasn't actually happened so it can't update the row it's just inserted?
From the FAQ:
B.5.9: Can triggers access tables?
A trigger can access both old and new data in its own table. A trigger can also affect other tables, but it is not permitted to modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger.
The documentation isn't clear that what you're doing won't work. OTOH, the documentation isn't clear that what you're trying to do will work either.
I think you'd be better off using a BEFORE INSERT trigger and setting NEW.rank in there. Then, the new row would have the right rank value when it is actually inserted into the table rather than patching it after. Also, you'd be able to simplify your existence check to just this:
EXISTS(SELECT rank FROM mytable WHERE parent_id = new.parent_id)
as NEW.id wouldn't have a useful value and the new row wouldn't be in the table anyway; the ORDER BY and LIMIT are also unnecessary as you're just checking if something exists so I took them out.
A BEFORE INSERT trigger seems to match your intent better anyway and that will give you correct data as soon as it is inserted into your table.
If you want the rank to be set +10 more than highest "brother's" rank, you could use:
DELIMITER $$
DROP TRIGGER IF EXISTS whatever $$
create trigger whatever
BEFORE INSERT ON mytable
FOR EACH row
BEGIN
SET NEW.rank = 10 + COALESCE(
( SELECT max(rank)
FROM mytable
WHERE parent_id = NEW.parent_id
), 0 ) ;
END
$$