我在user
TABLE 和school
TABLE 之间添加了触发器,对于学生转校和在校学生添加,该numbers_of_school
字段可能是问题所在。
错误消息是:
#1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 'WHERE NEW.NUMBERS_OF_STUDENT < 0; 附近使用正确的语法;END' 在第 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 ;
但是语法错误在哪里?谢谢你!