简体   繁体   English

SQL触发器条件检查不起作用

[英]Sql Trigger condition check not working

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'. 该字段应接受两个值“ M”或“ 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. 触发器已创建,但“性别”字段将不接受任何值,包括M或F。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM