Error 1064 Mysql syntax in After Update Trigger - mysql

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.

Related

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 Trigger Error: Updating the one table's field value based upon another table updates or insertions

I want to update one table in MySQL based upon another table update and insertions. Scenario is like this, when someone change the status of the column in main table the child table's column field should be update for their matching ID.
Tables look like as below:
po_request
Id po_id status
1 E0001 Requested
2 E0002 Received
PO_LINE
Id po_id is_received
6 E0001 0
7 E0002 0
Need to update the Po_line table each time when status has changed to "Received" OR directly insert the "received" in the table. I have made trigger but it's not working. Trigger
DROP TRIGGER IF EXISTS `t1`;
DELIMITER $$
CREATE TRIGGER `t1`
AFTER UPDATE ON po_request FOR EACH ROW
BEGIN
IF NEW.`status` = 'Received'
THEN
UPDATE po_line JOIN po_request ON po_request.po_id = po_line.po_id SET is_received = '1' WHERE po_request.status = 'Received'; END IF;
END$$
DELIMITER ;
Trigger loaded into table successfully but when I update the table it throws very weird error:
Error Code: 1054. Unknown column 'date_received' in 'field list'.
I think you need to update it by avoid joining the table and match in where clause with the reference of NEW keyword. which refers to the other table id like :
UPDATE po_line
SET po_line.is_received = '1'
WHERE new.po_id = po_id
AND po_request.status = 'Received';

mySQL TRIGGER error code 1064

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.

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.

DELETE TRIGGER in MySql throwing error 1064

I get an error (1064) when attempting to run the following... (MySql 5.5.9)
query:
CREATE TRIGGER clearChat AFTER INSERT ON chat
FOR EACH ROW
BEGIN
DELETE p.* FROM chat p LEFT JOIN (SELECT t.id FROM chat t ORDER BY t.id DESC LIMIT 50) x ON x.id = p.id WHERE x.id IS NULL
END;
the error is:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 5
Any assistance would be great.
Last Edit: Updated to show the 'FOR EACH ROW' and 'BEGIN'
You're missing FOR EACH ROW before DELETE: http://dev.mysql.com/doc/refman/5.1/en/create-trigger.html
Edit: There are more issues. The correct syntax is below:
delimiter |
CREATE TRIGGER clearChat AFTER INSERT ON chat
FOR EACH ROW BEGIN
DELETE p.* FROM chat p LEFT JOIN (SELECT t.id FROM chat t ORDER BY t.id DESC LIMIT 50) x ON x.id = p.id WHERE x.id IS NULL;
END;
|
delimiter ;
Edit 2:
I don't think that query is allowed to be in a trigger at all based on this http://dev.mysql.com/doc/refman/5.1/en/faqs-triggers.html#qandaitem-B-5-1-9:
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.
Since you aren't using OLD or NEW, I don't think you can modify chat since the trigger is triggered on inserts to chat.
I had the same problem with the following statement, it ALWAYS gave me a syntax error on even this simplified delete statement (originally was DELETE FROM APP_CACHE_VIEW WHERE APP_UID = OLD.APP_UID;):
CREATE TRIGGER APPLICATION_DELETE BEFORE DELETE ON APPLICATION
FOR EACH ROW
BEGIN
DELETE FROM APP_CACHE_VIEW;
END
If I changed the SQL command to the following then it WORKED but I don't understand why:
DELIMITER $$
CREATE TRIGGER APPLICATION_DELETE BEFORE DELETE ON APPLICATION
FOR EACH ROW
BEGIN
DELETE FROM APP_CACHE_VIEW;
END$$
DELIMITER ;