我正在创建一个触发器,当它从学生表中删除时保存历史记录,但是当我执行触发器代码来创建它时,我收到错误消息“抱歉发生意外错误!”而且字面上不知道该怎么做。
学生表:
CREATE TABLE `Student` (
`IDstudent` varchar(5) COLLATE utf32_spanish_ci NOT NULL,
`Names` varchar(40) COLLATE utf32_spanish_ci NOT NULL,
`SecNames` varchar(40) COLLATE utf32_spanish_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf32 COLLATE=utf32_spanish_ci;
ALTER TABLE `Student`
ADD PRIMARY KEY (`IDstudent`);
COMMIT;
删除学生表:
CREATE TABLE `DeletedStudents` (
`IDstudent ` varchar(5) COLLATE utf32_spanish_ci NOT NULL,
`Names ` varchar(30) COLLATE utf32_spanish_ci NOT NULL,
`SecNames ` varchar(30) COLLATE utf32_spanish_ci NOT NULL,
`DateDeleted` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf32 COLLATE=utf32_spanish_ci;
ALTER TABLE `DeletedStudents`
ADD PRIMARY KEY (`IDstudent`);
ALTER TABLE `DeletedStudents`
ADD CONSTRAINT `DeletedStudents_ibfk_1` FOREIGN KEY (`IDstudent`)
REFERENCES `Student` (`IDstudent`);
COMMIT;
触发:
DELIMITER
\\
CREATE TRIGGER TR_Delete_Student AFTER DELETE
ON
Student FOR EACH ROW
BEGIN
INSERT INTO DeletedStudents(
IDstudent,
Names,
SecNames,
DateDeleted
)
VALUES(
old.IDstudent,
old.Names,
old.SecNames,
NOW()) ;
END \\
DELIMITER
;
答案 0 :(得分:0)
我遇到了同样的错误,我知道MySql将我们的输入作为原始字符串获取,并随后将其解析为sql语法(在后端),因此使用DELIMITER \\
可能会在提供的原始字符串输入因此引发错误“对不起,发生了意外错误!”这当然是解析器的错误(我不是MySql)。因此,解决方案是将\\
以外的任何内容用作DELIMITER
。