Error creating sql syntax

I want to create a trigger, and I wrote this request, but it is not executed. Please check my request

CREATE TRIGGER 'blog_after_insert' AFTER INSERT ON 'blog' FOR EACH ROW BEGIN IF NEW.deleted THEN SET @changetype = 'DELETE'; ELSE SET @changetype = 'NEW'; END IF; INSERT INTO audit (blog_id, changetype) VALUES (NEW.id, @changetype); 

I get this error

 #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 ''blog_after_insert' AFTER INSERT ON 'blog' FOR EACH ROW BEGIN IF NEW.del' at line 2 
+6
source share
2 answers

Run this query

 DELIMITER $$ CREATE TRIGGER blog_after_insert AFTER INSERT ON blog FOR EACH ROW BEGIN IF NEW.deleted THEN SET @changetype = "DELETE"; ELSE SET @changetype = "NEW"; END IF; INSERT INTO audit (blog_id, changetype) VALUES (NEW.id, @changetype); END$$ DELIMITER ; 
+12
source

Single quotes ( ' ) denote string literals - object names, such as triggers and tables, must use forward quotes or not quotes at all:

 CREATE TRIGGER blog_after_insert AFTER INSERT -- here ON blog -- and here FOR EACH ROW BEGIN -- rest of code... 
0
source

Source: https://habr.com/ru/post/981833/


All Articles