MYSQL 触发器语法

语法:

create trigger triggerName  
after/before insert/update/delete on 表名  
for each row   #这句话在mysql是固定的  
begin  
    sql语句;  
end;  

例子:

DROP TRIGGER IF EXISTS `category_insert`;
DELIMITER $$
CREATE TRIGGER category_insert BEFORE INSERT ON category FOR EACH ROW
BEGIN
IF
new.parent_id = 0 THEN
SET new.path = new.category_name,
new.idpath = ( SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'category' );
ELSE
SET new.path = CONCAT( new.path, ",", new.category_name ),
new.idpath = CONCAT( new.idpath, ",", ( SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'category' ) );
END IF;
END $$
DELIMITER;

 

posted @ 2020-02-27 14:57  方大帝的博客  阅读(473)  评论(0编辑  收藏  举报