no se puede crear un disparador en mysql

i'm trying to create a trigger but there is an error somewhere and since i'm new at this i can't solve it...

So basically i have two tables:

students(stud_num:INT, grade_avg :real)

grading(stud_num:INT, classe:char(5), grade:int)

in students stud_num is the primary key, and in grading it references the table students..

What my professor wants is for me to create a trigger that every time we insert a grade in grading, the grade_avg is updated in students.


Esto es lo que tengo hasta ahora:

DELIMITER %%

CREATE TRIGGER something

AFTER INSERT ON grading

PARA CADA FILA COMIENZA

@stud_num=new.stud_num;

UPDATE students

SET grade_avg=(SELECT AVG(grade) FROM grading WHERE stud_num=@stud_num);

FIN;

%%

¿Puede alguien ayudarme?

preguntado el 27 de noviembre de 13 a las 00:11

And the error message is.... -

according to mysql its a syntax error in '@stud_num=new.stud_num;' -

1 Respuestas

The problem is the way you use the user-defined variable, and you don't actually need it, so you can just skip that by using new.stud_num en lugar de @stud_num.

You should also restrict your update statement to only update the row for the relevant student, rather than all rows.

DELIMITER %%

CREATE TRIGGER something

AFTER INSERT ON grading

FOR EACH ROW BEGIN

UPDATE students
SET grade_avg=(
  SELECT AVG(grade) 
  FROM grading 
  WHERE stud_num=new.stud_num
)
WHERE stud_num=new.stud_num;

END;

%%

DELIMITER ;

respondido 27 nov., 13:00

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.