Question

USE `movieinfo`;
DELIMITER $$
CREATE TRIGGER `Movie_BINS` BEFORE INSERT ON `movies` FOR EACH ROW
BEGIN
IF (length <30 && length >480 && year <1929)
THEN 
INSERT into movies
END IF;
END

I am trying to create a trigger but I keep getting the Error Code: 1064 because my syntax is wrong somewhere.

Was it helpful?

Solution

I am guessing that you want to validate the data and report an error when the data doesn't look right. In the more recent versions of MySQL, you can use [signal][1]:

DELIMITER $$
CREATE TRIGGER `Movie_BINS` BEFORE INSERT ON `movies` FOR EACH ROW
BEGIN
    IF ((new.length < 30 OR new.length > 480) AND new.year < 1929) THEN 
         SIGNAL SQLSTATE '45000'
               SET MESSAGE_TEXT = 'Data error on insert';
    END IF;
END$$

DELIMITER ;

A before insert trigger doesn't need to do an explicit insert into the table (in fact, it can't). Instead, if it completes successfully, then the data in the new record will be inserted. If you want to stop the insert, you need to generate an error, and signal is a good way to do that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top