I’m having a problem creating a trigger on a database for a project
I’ve never used triggers before in MySQL and when i try to execute the below code it says there is an error at ‘ ‘ on line 4…
Here is the code
CREATE TRIGGER archiveCar AFTER UPDATE ON mms_cars
FOR EACH ROW BEGIN
IF NEW.active=2 THEN
INSERT INTO mms_cars_sold SELECT * FROM mms_cars WHERE ad_id = OLD.ad_id;
DELETE FROM mms_cars SELECT * FROM mms_cars WHERE ad_id = OLD.ad_id;
END IF;
END;
I’m not sure if i’m creating this correctly so please excuse any amateur errors. My goal is to copy a the info for a row from an active table to an archive table when the status for the car has been set to 2 (i.e. Sold) in order to maximise search efficiency on the active table
Hopefully somebody can be kind enough to help me out
Kind regards
Barry
EDIT
I have now got the following
DELIMITER //
CREATE TRIGGER archiveCar AFTER UPDATE ON mms_cars
FOR EACH ROW
BEGIN
IF NEW.active=2 THEN
INSERT INTO mms_cars_sold SELECT * FROM mms_cars WHERE ad_id=OLD.ad_id;
DELETE FROM mms_cars WHERE ad_id=OLD.ad_id;
END IF;
END //
DELIMITER;
It produces this error:
You should try to drop the trigger before recreating new one.