-1

If you have two tables (course and teacher)

  • table course contains: C#, course_name, teacher, supervisor
  • table teacher contains:T#, teacher_name, department

Teacher and supervisor are foreign keys meaning they contain T#.

Now i want to make a trigger that checks if teacher and supervisor are both from the same department (mathematics, programming etc.), if not then an error should show. It should be an "insert" of a new course trigger.

GMB
  • 216,147
  • 25
  • 84
  • 135
Yulaw16
  • 3
  • 2

1 Answers1

0

You could use the following trigger:

delimiter //
create trigger trg_ins_course_teacher_supervisor 
before insert on course
for each row
begin
    if 
        (select t.department from teacher t where t.t# = new.teacher)
        != (select t.department from teacher t where t.t# = new.supervisor)
    then
        signal 
            sqlstate '45000' 
            set message_text = 'teacher and supervisor must belong to the same department';
    end if;
end
//
delimiter ;
GMB
  • 216,147
  • 25
  • 84
  • 135