**触发器入门**
转自
https://www.cnblogs.com/phpper/p/7587031.html
用户users表
CREATE TABLE users
(
id
int(11) unsigned NOT NULL AUTO_INCREMENT,
name
varchar(255) CHARACTER SET utf8mb4 DEFAULT NULL,
add_time
int(11) DEFAULT NULL,
PRIMARY KEY (id
),
KEY name
(name
(250)) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=1000001 DEFAULT CHARSET=latin1;
日志logs表:
CREATE TABLE logs
(
Id
int(11) NOT NULL AUTO_INCREMENT,
log
varchar(255) DEFAULT NULL COMMENT ‘日志说明’,
PRIMARY KEY (Id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT=‘日志表’;
注意搜索引擎改为 innodb
DELIMITER $
CREATE TRIGGER user_log AFTER INSERT ON users FOR EACH ROW
BEGIN
DECLARE s1 VARCHAR(40)character set utf8;
DECLARE s2 VARCHAR(20) character set utf8;
SET s2 = " is created";
SET s1 = CONCAT(NEW.name,s2); #函数CONCAT可以将字符串连接
INSERT INTO logs(log) values(s1);
END #$
DELIMITER ;