Home > OS >  Accesing the data from a row where trigger is assigned
Accesing the data from a row where trigger is assigned

Time:12-22

I have encountered a problem when I've designed this trigger:

CREATE TRIGGER StudentNewAssignment BEFORE INSERT
ON classassignment FOR EACH ROW 
insert into StudentTeacherLog(studentID, teacherID, description, reason)
values((select ID from student where classID = classassignment.classID), 1, 'Test', 'da');

Basically. When trigger hits, I want to access the data from newly inserted row in classassignment and use it in a subquery which will extract data from student table related to the 'data accesed from table where trigger hits'. Hope I've made it clear. Thanks =)

CodePudding user response:

https://dev.mysql.com/doc/refman/8.0/en/create-trigger.html says:

Within the trigger body, you can refer to columns in the subject table (the table associated with the trigger) by using the aliases OLD and NEW.

OLD.col_name refers to a column of an existing row before it is updated or deleted. NEW.col_name refers to the column of a new row to be inserted or an existing row after it is updated.

  • Related