0

I am trying to build a trigger in MySQL that will run after new rows are inserted into a table. If there is an error detected, then I want to write that trigger to a table. Otherwise, I don't want to write the error to a table. I want to try and avoid adding endless blank rows.

Here is my code:

CREATE 
TRIGGER character_validation4 AFTER INSERT 
ON address FOR EACH ROW 
Begin 
IF(New.city REGEXP '^[^a-zA-Z0-9]') 
THEN 
INSERT INTO errorlog (city, mainprocessid) Values("There is an error with the city", New.ID));
END

But I am seeing an error on the final bracket.

Can anyone help? Thanks

2 Answers 2

2
DELIMITER \\

CREATE 
TRIGGER character_validation4 AFTER INSERT 
ON address FOR EACH ROW 
Begin 
IF(New.city REGEXP '^[^a-zA-Z0-9]') 
THEN 
INSERT INTO errorlog (city, mainprocessid) Values("There is an error with the city", New.ID);
END IF;
END \\

DELIMITER ;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but now I see "Unexpected End_of_Input expecting ';'" and "unexpected end" and "unexpected end"
I noticed the phpmyadmin tag, so I assume you're typing this into there. Add DELIMITER // to the beginning on the query (before create) and add DELIMITER ; at the end. Edit: also add // after the last END. See edits to my answer.
1

It appears that you have an extra ")" right after New.ID . Try it this way

CREATE TRIGGER character_validation4 
AFTER INSERT ON address FOR EACH ROW Begin 
IF(New.city REGEXP '^[^a-zA-Z0-9]') THEN INSERT INTO errorlog (city, mainprocessid) 
Values("There is an error with the city", New.ID);
END IF;
END;

2 Comments

Thanks, but now I see "Unexpected End_of_Input expecting ';'" and "unexpected end"
Add a END IF and put a ; after the end statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.