MYSQL在插入触发器之前没有触发

时间:2017-04-22 20:05:48

标签: mysql triggers

出于某种原因,我在MYSQL Workbench中的数据库允许我将超出范围的数据插入“占用列”,但它不应该。

CREATE TABLE IF NOT EXISTS `cecsproject`.`room` (
  `type` VARCHAR(45) NOT NULL,
  `occupancy` INT NOT NULL,
  `number-beds` INT NULL,
  `type-beds` VARCHAR(45) NULL,
  `price` INT NULL,
  PRIMARY KEY (`type`))
ENGINE = InnoDB;

DELIMITER $$
CREATE TRIGGER occupancy_check 
    BEFORE INSERT ON room
    FOR EACH ROW 
BEGIN
    IF (new.occupancy > 5) 
        THEN SET new.occupancy = NULL;
    ELSEIF (new.occupancy < 1)
        THEN SET new.occupancy = NULL;
    END IF;
END$$

DELIMITER ;

这是我运行的查询:

INSERT INTO room
VALUES ('regular', 0, 2, 'full', 100);

select * from room;

结果如下:

regular 0   2   full    100

非常感谢任何帮助!

0 个答案:

没有答案