Search code examples
mysqlsqlinnodb

Mysql Trigger Error with IF THEN


I have a problem with trigger.. I would like to change a value to false if new returnBike is not null.

    CREATE TRIGGER returnBikeTrigger
AFTER UPDATE ON Rent
FOR EACH ROW
    IF (NEW.returnBike <> null) THEN
        UPDATE Bike SET isRented = false
        WHERE id = (select Bike from Rent); -- but there is Error (missing semicolon)
        END IF;

Any advice?


Solution

  • I would be inclined to just do:

    DELIMITER $$    
    CREATE TRIGGER returnBikeTrigger
    AFTER UPDATE ON Rent
    FOR EACH ROW
    BEGIN
        UPDATE Bike
            SET isRented = false
        WHERE id = new.Bike AND NEW.returnBike IS NOT NULL;
    END; $$
    DELIMITER ;