[英]MySQL Trigger Insert Before
I'm trying to write a trigger that will insert a default value in another table 我正在尝试编写一个触发器,该触发器将在另一个表中插入默认值
After inserting a Record into my Students Table I want to insert a new record into my Student_Chapter Table. 在将记录插入到学生表中之后,我想将新记录插入到学生表中。
The Student Table has field called S_ID which is auto incremented and the Primary Key for that table. 学生表具有一个称为S_ID的字段,该字段会自动递增,并且具有该表的主键。
The Student_Chapter table has two fields Student_ID Int Chapter_ID Int Student_Chapter表具有两个字段Student_ID Int Chapter_ID Int
I tried to create this trigger to fire when you enter a new Student Record. 我尝试创建此触发器以在您输入新的学生记录时触发。 However I get an error trying to create it...and I've tried several things...and looks at various examples on this site. 但是我在尝试创建它时遇到了错误...并且我尝试了几件事...并查看了该站点上的各种示例。
Create Trigger AssignDefaultChapter_trigger
Before INSERT On Students
FOR EACH ROW
BEGIN
DECLARE Default_ID INT;
SET Default_ID =(SELECT C_ID FROM Chapter WHERE ChName = "UNASSIGNED";
Insert into Student_Chapter(Chapter_ID,Student_ID) values (Default_ID,New.S_ID);
END;
As far as I can see there is nothing wrong with that insert. 据我所知,该插入没有任何问题。
The Error I get is #1064 - You have an error in your SQL syntax; 我得到的错误是#1064-您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '; 检查与您的MySQL服务器版本相对应的手册,以在'附近使用正确的语法; Insert into Student_Chapter (Chapter_ID, Student_ID) values (Default_ID,NEW.' at line 6 在第6行中将其值(Default_ID,NEW。)插入到Student_Chapter(Chapter_ID,Student_ID)中。
A missing )
? 失踪)
吗? after the "UNASSIGNED" Also, shouldnt you prefix your parameters with a @
在“ UNASSIGNED”之后,也不要在参数前面加上@
Create Trigger AssignDefaultChapter_trigger
Before INSERT On Students
FOR EACH ROW
BEGIN
DECLARE @Default_ID INT;
SELECT @Default_ID = C_ID FROM Chapter WHERE ChName = "UNASSIGNED";
Insert into Student_Chapter(Chapter_ID,Student_ID) values (@Default_ID,New.S_ID);
END;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.