Unable to create SQL trigger query

Good infinityfree team.
Please this trigger query which i am trying to use to auto calculate GPA for my school website is not working;

*DELIMITER //*

*CREATE TRIGGER update_gpa*

*AFTER UPDATE ON marks*

*FOR EACH ROW*

*BEGIN*

    *DECLARE total_weighted_points DECIMAL(10,2);*

    *DECLARE total_credits INT;*

    *DECLARE semester_gpa DECIMAL(3,2);*

    *-- Calculate semester GPA*

    *SELECT SUM(m.weighted_point), SUM(c.credit_value)*

    *INTO total_weighted_points, total_credits*

    *FROM marks m*

    *JOIN courses c ON m.course_id = c.course_id*

    *WHERE m.student_id = NEW.student_id*

      *AND m.school_year_id = NEW.school_year_id*

      *AND m.semester = NEW.semester;*

    *SET semester_gpa = IF(total_credits > 0, total_weighted_points / total_credits, 0);*

    *-- Update semester_gpa in student_enrollments*

    *UPDATE student_enrollments*

    *SET semester_gpa = semester_gpa*

    *WHERE student_id = NEW.student_id*

      *AND school_year_id = NEW.school_year_id*

      *AND semester = NEW.semester;*

    *-- Calculate cumulative GPA (across all semesters)*

    *SELECT SUM(m.weighted_point), SUM(c.credit_value)*

    *INTO total_weighted_points, total_credits*

    *FROM marks m*

    *JOIN courses c ON m.course_id = c.course_id*

    *WHERE m.student_id = NEW.student_id;*

    *SET @cumulative_gpa = IF(total_credits > 0, total_weighted_points / total_credits, 0);*

    *-- Update cumulative_gpa in student_enrollments*

    *UPDATE student_enrollments*

    *SET cumulative_gpa = @cumulative_gpa*

    *WHERE student_id = NEW.student_id*

      *AND school_year_id = NEW.school_year_id;*

*END //*

*DELIMITER ;
*

This is the error it is giving;

*#1142 - TRIGGER command denied to user β€˜if0_40269699’@β€˜192.168.0.6’ for table `if0_40269699_university_db`.`marks`
*
PLEASE SOMEONE SHOULD HELP ME GRANT PERMISSION :folded_hands:

1 Like

Trigger is not available on the free plan unfortunately

8 Likes

MySQL triggers are indeed not supported on free hosting, they are only available on premium hosting.

However, I would personally recommend to avoid using MySQL triggers in the first place, and handle all business logic through PHP code instead. So instead of having the database update the GPA when a mark is added, add PHP code to the place where marks are added to update the GPA tables along with it.

Using only PHP code is more portable (the importance of that you see here), PHP code is more versatile than SQL, and it’s easier to test and debug in my opinion.

8 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.