4

I'm trying to create a new trigger in MySQL but no matter what I try I'm getting syntax error. I want to insert a value into the html_id column which would be a concatenation of letter f and column id:

CREATE TRIGGER htmlid 
BEFORE INSERT ON makelist_food
FOR EACH ROW
BEGIN
  IF (NEW.html_id IS NULL) THEN
  NEW.html_id = CONCAT('f', NEW.id);
  END IF;
END

I also tried this:

CREATE TRIGGER htmlid 
BEFORE INSERT ON makelist_food
FOR EACH ROW
BEGIN
  IF (NEW.html_id IS NULL) THEN
  INSERT INTO makelist_food SET html_id = CONCAT('f', NEW.id);
  END IF;
END

And this (changing delimiter):

DELIMITER $$

CREATE TRIGGER htmlid 
BEFORE INSERT ON makelist_food
FOR EACH ROW
BEGIN
  IF (NEW.html_id IS NULL) THEN
  NEW.html_id = CONCAT('f', NEW.id);
  END IF;
END$$

DELIMITER ;

The error I'm getting: #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 '.html_id = CONCAT('f', NEW.id)' at line 6

I'm running MySQL 5.5.22.

2 Answers 2

3

Try with this query:

delimiter |

    CREATE TRIGGER htmlid 
    BEFORE INSERT ON makelist_food
    FOR EACH ROW
    BEGIN
      IF (NEW.html_id IS NULL) THEN
      INSERT INTO makelist_food SET html_id = CONCAT('f', NEW.id);
      END IF;
    END
    |
    delimiter ;
Sign up to request clarification or add additional context in comments.

2 Comments

That worked, thanks! The only things you changed were that you changed the delimiter from $$ to | and typed delimiter keyword in lowercase. Is there any known issue with using $$ as delimiter?
I think the difference is the SET keyword
2

I think the difference is the SET keyword. try this

DELIMITER $$

CREATE TRIGGER htmlid 
BEFORE INSERT ON makelist_food
FOR EACH ROW
BEGIN
  IF (NEW.html_id IS NULL) THEN
     SET NEW.html_id = CONCAT('f', NEW.id);
  END IF;
END$$

DELIMITER ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.