我需要帮助编写MySQL触发器。

假设您有一个包含以下表格的学生数据库:

ENROLLMENT(SSN, CLASS_NO, GRADE)
CLASS(CLASS_NO, CLASS_TITLE, NO_OF_STUDENTS).

我需要写一个触发器来增加NO_OF_STUDENTS一个,如果一个新的学生加入到ENROLLMENT表,以CLASS_NO

===============>>#1 票数:6 已采纳

你可以使用mysql触发器来做到这一点。 尝试类似的东西

CREATE TRIGGER 'database_name'.'after_insert_enrollment' AFTER INSERT ON 'ENROLLMENT' 
FOR EACH ROW
BEGIN
UPDATE class SET NO_OF_STUDENTS = NO_OF_STUDENTS +1 WHERE CLASS_NO = NEW.CLASS_NO;
END

===============>>#2 票数:2

如果删除注册,我也会减少no_of_studens

delimiter //
CREATE TRIGGER AFTER_INS_ENROLLMENT AFTER INSERT ON ENROLLMENT
FOR EACH ROW
BEGIN
    UPDATE CLASS SET NO_OF_STUDENTS = NO_OF_STUDENTS + 1 WHERE CLASS_NO = NEW.CLASS_NO;
END;//
delimiter;

delimiter //
CREATE TRIGGER AFTER_INS_ENROLLMENT AFTER DELETE ON ENROLLMENT
FOR EACH ROW
BEGIN
    UPDATE CLASS SET NO_OF_STUDENTS = NO_OF_STUDENTS - 1 WHERE CLASS_NO = NEW.CLASS_NO;
END;//
delimiter ;

===============>>#3 票数:1

你为什么要将它存储在唱片中? 也许您应该使用Student Class_Id = @Class_Id中的Select COUNT(Student_Id)

===============>>#4 票数:0

你尝试过类似的东西:

delimiter |

CREATE TRIGGER testref BEFORE INSERT ON ENROLLMENT
  FOR EACH ROW BEGIN
    UPDATE CLASS SET NO_OF_STUDENTS = NO_OF_STUDENTS + 1 WHERE CLASS_NO = NEW.CLASS_NO;
  END;
|

delimiter ;

  ask by wael translate from so

未解决问题?本站智能推荐: