Experiment 9 SQL database language - the basic operation of the trigger

Experiment 9 SQL database language - the basic operation of the trigger

When you create insert, update trigger TR_SC_IN_SNO sc in the table, asked to insert or modify records elective curriculum student number, student number to check whether the student is a student number in the table, if not then allowed to insert or modify records elective course selection table student number.
Because mysql trigger does not support multiple events at the same time trigger an action, so at this writing two triggers to achieve the function.

If the record is inserted elective school student number is not in the error:

delimiter //
create trigger TR_SC_IN_SNO after insert on sc
for each row
begin
if (new.sno not in (select sno from student))  #如果插入的元组学号不在student,报错
then
SIGNAL SQLSTATE'45000'SET MESSAGE_TEXT = 'the sno is not in stdent';;
end if;
END//

When modifying curriculum student number if the number is not in school student, then an error:

delimiter //
create trigger TR_SC_UP_SNO after update on sc
for each row
begin
if (new.sno not in (select sno from student))   #如果更新元组时修改的学号不在student,报错
then
SIGNAL SQLSTATE'45000' SET MESSAGE_TEXT = 'the sno is not in stdent';   
end if;
END//

You can view the results trigger the establishment of:
Here Insert Picture Description

Published 68 original articles · won praise 36 · views 10000 +

Guess you like

Origin blog.csdn.net/dingdingdodo/article/details/103016666