建立一个表

CREATE TABLE cmd (
        id INT PRIMARY KEY auto_increment,
        USER CHAR (32),
        priv CHAR (10),
        cmd CHAR (64),
        sub_time datetime, #提交时间
        success enum ('yes', 'no') #0代表执行失败
);

建立一个要被触发的表

CREATE TABLE errlog (
        id INT PRIMARY KEY auto_increment,
        err_id int
);

建立触发器

delimiter $$  #把;变换成$$,让下面的代码完整运行
CREATE TRIGGER tri_after_insert_cmd AFTER INSERT ON cmd FOR EACH ROW
BEGIN
        if NEW.success = 'no' then   # NEW(新的信息,也就是插入cmd的信息)
                insert into errlog(err_id) values(NEW.id);  # 插入语法.当上面的条件成立
        end if;
END $$  # 制造触发器完毕
delimiter ; # 将符号还原成;

插入数据

INSERT INTO cmd (
        USER,
        priv,
        cmd,
        sub_time,
        success
)
VALUES
        ('egon','0755','ls -l /etc',NOW(),'yes'),
        ('egon','0755','cat /etc/passwd',NOW(),'no'),
        ('egon','0755','useradd xxx',NOW(),'no'),
        ('egon','0755','ps aux',NOW(),'yes');

删除触发器

drop trigger tri_after_insert_cmd;