Online Learning Platform

DBMS > Structural Query Language (SQL) > Trigger example using MySQL database
1. Create a table temp: s_id, marks
 create table temp (
         s_id int primary key,
         marks decimal(5,2)
      );
 
2. Create course_marks: s_id, marks, gp, gl
 
    create table course_marks( 
      s_id int,
      marks int null,
      gp decimal(5,2) null,
      gl varchar(2) null
   );
 
3. Objective: We will insert data into 'Temp' table but trigger will automatically insert same data into course_marks table after calculating GPA and GL;
 
In tyhe boxx paste the following bold text and 'GO'

CREATE TRIGGER gpgl_calc
    AFTER INSERT ON temp
    FOR EACH ROW
    begin
      Declare t_gp decimal(5,2) default 0;
      Declare t_gl varchar(5);
      Declare t_marks int;

      set t_marks = new.marks;
      IF t_marks >= 80 THEN
         SET t_gp = 4.0;
         SET t_gl = 'A+';
      ELSEIF t_marks >=70 AND t_marks < 80 THEN
         SET t_gp = 3.5;
         SET t_gl = 'A';
      ELSEIF t_marks >=60 AND t_marks < 70 THEN
         SET t_gp = 3.0;
         SET t_gl = 'B';
      ELSEIF t_marks >=50 AND t_marks < 60 THEN
         SET t_gp = 2.5;
         SET t_gl = 'C';
      ELSE
         SET t_gp = 0;
         SET t_gl = 'F';
     END IF;

     insert into course_marks (s_id,marks, gp, gl)
                         values(new.s_id,new.marks,t_gp,t_gl);
end;

 
 
5. Now enter data into 'Temp table . Same will automaticall inserted into course Marks Table will GPA and Grade letter calculation.
 
     insert into temp values(14,58);
 
This above command changes course_marks table as follws:
 
 

 

Prev
Calculation of CGPA using Procedure
Next
How to create user in MySQL?
Feedback
ABOUT

Statlearner


Statlearner STUDY

Statlearner