I am considering using triggers within my database to keep track of changes of bookings. Each time a booking is added I add a point to in a table that keeps a score in ‘tenure’.
When I run this command I am able to change the id before adding the trigger.
UPDATE bookit SET prov_id = 32 where book_id=2;
after adding the following trigger I get cannot update prov_id and get this error
1054 – Unknown column ‘bookit.prov_id’ in ‘where clause’
Trigger:
delimiter //
CREATE TRIGGER update_tenure_it BEFORE
UPDATE ON bookit FOR EACH ROW
BEGIN
if(new.prov_id<>old.prov_id) THEN
UPDATE provider_score set tenure=(tenure+1) where bookit.prov_id=provider_id;
END IF;
END;
//
Do you know what I am doing wrong and where the issue comes from? Should I even consider using triggers for such operation or just code it in PHP? Thanks!
In triggers, you don’t have access directly to the table via
TABLENAME.FIELD; instead, update triggers give youold.* andnew.* as aliases of the row before and after the update fires, respectively. In this case, the following should work: