Arthur Khlghatyan
Arthur Khlghatyan

Reputation: 45

Mysql create trigger throws error

I've tried to create trigger for each new post here is my code

CREATE TRIGGER insertprod AFTER INSERT ON post
FOR EACH ROW
BEGIN
  INSERT INTO attributes SET attrtext = 'sometext', post_id = NEW.id;
END;

I'm getting this error

enter image description here

Upvotes: 0

Views: 63

Answers (3)

Gordon Linoff
Gordon Linoff

Reputation: 1269445

You probably just need a delimiter:

DELIMITER $$
CREATE TRIGGER insertprod AFTER INSERT ON post
FOR EACH ROW
BEGIN
  INSERT INTO attributes SET attrtext = 'sometext', post_id = NEW.id;
END;
$$
DELIMITER ;

However, you are also using a non-standard form of INSERT. I would suggest:

DELIMITER $$
CREATE TRIGGER insertprod AFTER INSERT ON post
FOR EACH ROW
BEGIN
  INSERT INTO attributes(attrtext, post_id)
     VALUES('sometext', NEW.id);
END;
$$
DELIMITER ;

Upvotes: 2

eribeiro
eribeiro

Reputation: 592

Your INSERT command is wrong. It should have been

 INSERT INTO attributes (attrtext, post_id) VALUES ('sometext', NEW.id);

Upvotes: 0

zerobandwidth
zerobandwidth

Reputation: 1303

The parser is confused by the ; that terminates the statement inside the trigger's BEGIN...END block. You need to use a DELIMITER statement to define a new delimiter, which you can then use to end the CREATE TRIGGER statement.

Upvotes: 0

Related Questions