I wrote my own trigger today but when I enter it on mysql it give me the syntax error.
Here is the my trigger definition in the following code;
CREATE DEFINER = CURRENT_USER TRIGGER `Customer_Car_Trigger` AFTER INSERT ON `CUSTOMERS`
FOR EACH ROW
BEGIN
SET @CountOfCar = (SELECT COUNT(ID) FROM `CARS`,`CUSTOMERS` WHERE `CARS`.`CustomerID` = `CUSTOMERS`.`ID`)
IF @CountOfCar>0 THEN
UPDATE `CUSTOMERS` SET `NumberOfCars`=+1 WHERE `CUSTOMERS`.`ID`=NEW.`ID`
END IF
END;
When I put this code into phpmyadmin it gives me:
#1064 – You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘IF @CountOfCar>0 THEN UPDATE
CUSTOMERSSETNumberOfCars=+1
WHERE `CUSTOMER’ at line 5
I tried many different things to IF statement.Am I doing wrong? I didnt show any syntax error. Why phpmyadmin gives me this error to me? Is it different to veriable in IF-ELSE statements.
You forgot a few
;and to iterate by 1 in SQL you can’t use+=1