0

仍然允许插入零值..

    CREATE TRIGGER check_my_constraint BEFORE insert ON `personal_details`
         FOR EACH ROW 
         BEGIN
         DECLARE msg varchar(255);
         IF (NEW.studentid<0)
         THEN
            SET msg = concat('Constraint my_constraint violated: studentid must not be zero ', cast(new.studentid as char));
            SIGNAL sqlstate '45000' SET message_text = msg;
         END IF; 
         END ;
4

2 回答 2

1

改变条件——

IF (NEW.studentid<0)=>IF (NEW.studentid <= 0)

于 2013-04-10T09:49:11.217 回答
1

您只检查小于零。您必须检查是否小于或等于零:

IF (NEW.studentid <= 0)
于 2013-04-10T09:49:15.827 回答