Why does your design require an ordered list of teachers and their students? This strikes me as a remnant of storing data in linked lists in memory. Your trigger program is also failing to select any row, there is no row matching teacher_id = 3 and student_prev = 3. Perhaps you meant teacher_id=NEW.teacher_id and student_id = NEW.student_prev? This will add new students to the head of the list. Note that your design also limits students to having exactly one teacher.