0
votes

hi i have created a trigger which i want to update a table field when insertion in another field occurs.but i don,t know how should it know that its updating the same user row who is inserting here is some code and database senerio

--> admin_reg_abr

'id    course_limit   username
10          0             ahmad '

note: id is primry key

--> abroad_course

'id    uni_id   username
1        10        ahmad'

note: uni_id is foriegn key or same as id in admin_reg_abr.

i write the following code for trigger:

DROP TRIGGER IF EXISTS ahmad.AI_course_each//

CREATE TRIGGER ahmad.AI_course_each AFTER INSERT ON ahmad.abroad_course FOR EACH ROW BEGIN

UPDATE admin_reg_abr SET admin_reg_abr.course_limit = admin_reg_abr.course_limit + 1 WHERE admin_reg_abr.id = new.id; END //

i want to update the same id how insert in the table but its not working.

1

1 Answers

1
votes

Didn't You mean:

DROP TRIGGER IF EXISTS ahmad.AI_course_each//

CREATE TRIGGER ahmad.AI_course_each 
AFTER INSERT ON ahmad.abroad_course FOR EACH ROW
BEGIN
    UPDATE admin_reg_abr SET 
       admin_reg_abr.course_limit = admin_reg_abr.course_limit + 1 
    WHERE admin_reg_abr.id = new.uni_id; 
END //

Note that in Your code NEW.id points nowhere in admin_reg_abr table

One more thing. You are talking about firing trigger on UPDATE, so, You would probably need another trigger:

DROP TRIGGER IF EXISTS ahmad.AI_course_each//

CREATE TRIGGER ahmad.AI_course_each 
AFTER UPDATE ON ahmad.abroad_course FOR EACH ROW
BEGIN
    UPDATE admin_reg_abr SET 
       admin_reg_abr.course_limit = admin_reg_abr.course_limit + 1 
    WHERE admin_reg_abr.id = new.uni_id; 
END //