mysql where子句错误触发器

时间:2012-12-21 20:32:32

标签: mysql

我在user TABLE和school TABLE之间添加了触发器,对于学生转学和在学校添加学生,numbers_of_school字段可能是问题所在。

错误消息是:

#1064 - 您的SQL语法出错;检查与您的MySQL服务器版本对应的手册,以便在'WHERE NEW.NUMBERS_OF_STUDENT<附近使用正确的语法。 0; 结束'第4行

CREATE TABLE IF NOT EXISTS `school` (
  `school_id` int(11) NOT NULL,
  `school_name` varchar(45) NOT NULL,
  `location` varchar(45) NOT NULL,
  `master` varchar(45) NOT NULL,
  `numbers_of_student` int(11) NOT NULL,
  PRIMARY KEY (`school_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- TABLE `school`
--
DROP TRIGGER IF EXISTS `tg_school_insert`; 
DELIMITER //
CREATE TRIGGER `tg_school_insert` BEFORE INSERT ON `school`
 FOR EACH ROW BEGIN
    IF NEW.NUMBERS_OF_STUDENT = NULL THEN
        SET NEW.NUMBERS_OF_STUDENT = 0;
    END IF;
END
//
DELIMITER ;

DROP TRIGGER IF EXISTS `tg_school_delete`; 
DELIMITER //
CREATE TRIGGER `tg_school_delete` AFTER DELETE ON `school`
 FOR EACH ROW BEGIN
DELETE FROM DEPARTMENT
WHERE SCHOOL_ID=OLD.SCHOOL_ID;
UPDATE USER
SET SCHOOL_ID = NULL
WHERE SCHOOL_ID = OLD.SCHOOL_ID;
END
//
DELIMITER ;

DROP TRIGGER IF EXISTS `tg_school_update`; 
DELIMITER //
CREATE TRIGGER `tg_school_update` BEFORE UPDATE ON `school`
 FOR EACH ROW BEGIN
SET NEW.NUMBERS_OF_STUDENT = 0
WHERE NEW.NUMBERS_OF_STUDENT < 0;
END
//
DELIMITER ;

和我的user表格如下:

CREATE TABLE IF NOT EXISTS `user` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `department_id` int(11) DEFAULT NULL,
  `school_id` int(11) DEFAULT NULL,
  `password` varchar(45) DEFAULT NULL,
  `realname` varchar(45) DEFAULT NULL,
  `familyname` varchar(45) DEFAULT NULL,
  `birthdate` varchar(45) DEFAULT NULL,
  `gender` tinyint(1) NOT NULL,
  `photo` varchar(45) DEFAULT NULL,
  `city` varchar(45) DEFAULT NULL,
  `school_enteryear` int(11) DEFAULT NULL,
  `email` varchar(45) DEFAULT NULL,
  `activestat` varchar(45) DEFAULT NULL,
  `onlinestat` varchar(45) DEFAULT NULL,
  `regtime` datetime NOT NULL,
  `avatar` varchar(45) DEFAULT NULL,
  `status` varchar(45) DEFAULT NULL,
  `desc` varchar(45) DEFAULT NULL,
  `self_comment` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`user_id`),
  UNIQUE KEY `uk_email` (`email`),
  KEY `fk_user_department_id_idx` (`department_id`),
  KEY `fk_user_school_id_idx` (`school_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

--
-- TRIGGER `user`
--
DROP TRIGGER IF EXISTS `tg_user_delete`; 
DELIMITER //
CREATE TRIGGER `tg_user_delete` AFTER DELETE ON `user`
 FOR EACH ROW BEGIN
DELETE FROM HOBBY WHERE USER_ID = OLD.USER_ID;
DELETE FROM USER_ALBUM_PHOTO WHERE USER_ID = OLD.USER_ID;
DELETE FROM USER_DIARY WHERE USER_ID = OLD.USER_ID;
DELETE FROM USER_MSG WHERE FROM_ID = OLD.USER_ID OR TO_ID = OLD.USER_ID;
DELETE FROM USER_NOTIFICATION WHERE USER_ID = OLD.USER_ID;
DELETE FROM USER_OP WHERE USER_ID = OLD.USER_ID;
DELETE FROM USER_FEED WHERE USER_ID = OLD.USER_ID;
DELETE FROM USER_RELATION WHERE USER_ID = OLD.USER_ID OR USER2_ID = OLD.USER_ID;
DELETE FROM USER_SPECIAL WHERE USER_ID = OLD.USER_ID;
DELETE FROM USER_STATUS WHERE USER_ID = OLD.USER_ID;
DELETE FROM USER_DIARY_COMMENT WHERE USER_ID = OLD.USER_ID;
DELETE FROM USER_STATUS_COMMENT WHERE USER_ID = OLD.USER_ID;
END
//
DELIMITER ;
DROP TRIGGER IF EXISTS `tg_user_bf_insert`;
DELIMITER //
CREATE TRIGGER `tg_user_bf_insert` BEFORE INSERT ON `user`
 FOR EACH ROW BEGIN
    IF NEW.regtime = '0000-00-00 00:00:00' THEN
        SET NEW.regtime = NOW();
    END IF;
END
//
DELIMITER ;

DROP TRIGGER IF EXISTS `tg_user_insert`;
DELIMITER //
CREATE TRIGGER `tg_user_insert` AFTER INSERT ON `user`
 FOR EACH ROW BEGIN
UPDATE SCHOOL SET NUMBERS_OF_STUDENT=NUMBERS_OF_STUDENT+1
WHERE SCHOOL_ID = NEW.SCHOOL_ID;
INSERT INTO HOBBY(`user_id`) VALUES(NEW.USER_ID);
END
//
DELIMITER ;


DROP TRIGGER IF EXISTS `tg_user_update_school`;
DELIMITER //
CREATE TRIGGER `tg_user_update_school` AFTER UPDATE ON `user`
 FOR EACH ROW BEGIN
UPDATE SCHOOL
SET NUMBERS_OF_STUDENT=NUMBERS_OF_STUDENT-1
WHERE OLD.SCHOOL_ID <> NEW.SCHOOL_ID AND SCHOOL_ID = OLD.SCHOOL_ID;
UPDATE SCHOOL
SET NUMBERS_OF_STUDENT=NUMBERS_OF_STUDENT+1
WHERE OLD.SCHOOL_ID <> NEW.SCHOOL_ID AND SCHOOL_ID = NEW.SCHOOL_ID;
END
//
DELIMITER ;

但是语法错误在哪里? 谢谢!

4 个答案:

答案 0 :(得分:1)

我发布这个作为答案,因为评论太长了。我刚刚运行了所有脚本,错误就在于这段代码:

DROP TRIGGER IF EXISTS `tg_school_update`; 
DELIMITER //
CREATE TRIGGER `tg_school_update` BEFORE UPDATE ON `school`
 FOR EACH ROW BEGIN
SET NEW.NUMBERS_OF_STUDENT = 0
WHERE NEW.NUMBERS_OF_STUDENT < 0;
END
//
DELIMITER ;

它不喜欢这条线:

WHERE NEW.NUMBERS_OF_STUDENT < 0;

如果删除此项,则会创建触发器。所以我的问题是,是否有可能为负数学生?

或者将代码更改为:

DELIMITER //
CREATE TRIGGER `tg_school_update` BEFORE UPDATE ON `school`
 FOR EACH ROW BEGIN
SET NEW.NUMBERS_OF_STUDENT = case when NEW.NUMBERS_OF_STUDENT < 0 then 0 else NEW.NUMBERS_OF_STUDENT end;
END
//
DELIMITER ;

答案 1 :(得分:1)

根据我的评论:试试这个,而不是你使用的当前代码。

SET NEW.NUMBERS_OF_STUDENT = 
( CASE WHEN NEW.NUMBERS_OF_STUDENT < 0 THEN 0 
  ELSE NEW.NUMBERS_OF_STUDENT 
END )

答案 2 :(得分:1)

改变这个:

SET NEW.NUMBERS_OF_STUDENT = 0
WHERE NEW.NUMBERS_OF_STUDENT < 0;

对此:

SET NEW.NUMBERS_OF_STUDENT 
    = GREATEST(NEW.NUMBERS_OF_STUDENT, 0);


你也有一个bug。这一行:

IF NEW.NUMBERS_OF_STUDENT = NULL THEN

应该是:

IF NEW.NUMBERS_OF_STUDENT IS NULL THEN

column = null的比较永远不会成立 - 您必须使用特殊的is null语法来测试null。

答案 3 :(得分:1)

CREATE TRIGGER `tg_school_update` BEFORE UPDATE ON `school`
 FOR EACH ROW BEGIN
   IF NEW.NUMBERS_OF_STUDENT < 0
   THEN SET NEW.NUMBERS_OF_STUDENT = 0;
   END IF;
END;