delimiter //
CREATE TRIGGER `range` BEFORE INSERT ON touristCompany.hotels
FOR EACH ROW
BEGIN
IF NEW.star >5 THEN
SET NEW.star = 5;
ELSEIF NEW.star < 1 THEN
SET NEW.star = 1;
END IF;
END;//
delimiter ;
解决方法:
您需要在发生插入操作的同一模式/数据库中创建触发器.从触发器定义来看,触发操作显然是在touristCompany数据库下.确保在同一数据库下创建触发器.
修改您的触发器定义,如下所示
delimiter //
CREATE TRIGGER `touristCompany`.`range`
BEFORE INSERT ON `touristCompany`.`hotels`
FOR EACH ROW
BEGIN
IF NEW.star > 5 THEN
SET NEW.star = 5;
ELSEIF NEW.star < 1 THEN
SET NEW.star = 1;
END IF;
END;//
delimiter ;
(或)在创建触发器之前选择数据库
USE `touristCompany`;
delimiter //
CREATE TRIGGER `range` ...
<rest of code here>