I’d like to create a before insert trigger that uses new values to insert to check if the trigger should let the insert happend or stop it following some conditions. I can’t really find the right syntax for that trigger but here is the pseudo code that is explicit enough :
CREATE TRIGGER my_first_trigger BEFORE INSERT COMMENT VALUES(comment, #commentPIID, #commentUserID)
FOR EACH ROW
BEGIN
IF
(SELECT USER.ID
FROM USER LEFT JOIN PI_REMINDER ON PI_REMINDER.userID == USER.ID
WHERE PI_REMINDER.ID == commentPIID && USER.ID == commentUserID) IS NOT NULL)
OR
((SELECT userID
FROM pishared
WHERE PIID == commentPIID && userID ==commentUserID && acceot == 1) IS NOT NULL)
THEN
INSERT INTO COMMENT VALUES(comment, #commentPIID, #commentUserID)
END IF;
END;
Could someone tell me the right syntax to use in that pseudo code case?
As I understand the trigger should restrict some insertions, in this case you can check the condition and then throw an error to escape from inseкtion. The errors in MySQL can be generated using SIGNAL statement in MySQL 5.5 or calling unknown procedure (e.g. CALL proc100).
I have used COUNT(*) function to know if there are corresponding records in another tables. And one more thing – there were some syntax errors in your trigger, I have correct them.
So, the trigger may look like this –