Reputation: 938
I am creating a checking condition for a field in my data base. In the table called student, I have a field for Gender. This field should accept one of two values 'M' or 'F'. I have created a trigger like so:
DELIMITER $$
CREATE TRIGGER `test_before_insert` BEFORE INSERT ON `student`
FOR EACH ROW
BEGIN
IF NEW.Gender<>'M' OR NEW.Gender<>'F' THEN
SIGNAL SQLSTATE '12345'
SET MESSAGE_TEXT = 'should be M or F';
END IF;
END$$
DELIMITER ;
The trigger is created but the field 'Gender' won't accept any values including M or F.
Upvotes: 0
Views: 555
Reputation: 204746
Your condition is wrong. It is always true. use
IF NEW.Gender not in ('M', 'F') THEN
or use
IF NEW.Gender<>'M' AND NEW.Gender<>'F' THEN
Upvotes: 1