a trigger
Use triggers to customize the behavior of the user in the "Add, delete, change" operation of the table, note: No query
Create a trigger
# Insert before create TRIGGER tri_before_insert_tb1 before insert on TB1 for each rowbegin ... end# after inserting create TRIGGER tri_after_insert_tb1 after insert on TB1 for each rowbegin ... end# Delete before create TRIGGER tri_before_delete_tb1 before delete on tb1 for each rowbegin ... end# Delete Create TRIGGER tri_after_delete_tb1 after delete on tb1 for each rowbegin ... end# Update before the Create TRIGGER tri_before_update_tb1 before update on TB1 for each rowbegin ... end# Update after the Create TRIGGER tri_after_update_tb1 after update on TB1 for each rowbegin ... END
#准备表CREATE TABLE cmd (id INT PRIMARY KEY auto_increment, USER char (+), Priv char (ten), cmd char (+), Su B_time datetime, #提交时间 success enum (' Yes ', ' no ') #0代表执行失败); CREATE TABLE errlog (id INT PRIMARY KEY auto_increment, Err_cmd CHAR (+), Err_time datetime); #创建触发器delimiter// CREATE TRIGGER tri_after_insert_cmd after insert in cmd for each rowbegin IF new.success = "No" then #等值判断只有一个等号 INSERT into ErrLog (Err_cmd, Err_time) VALUES (New.cmd, new.sub_time); #必须加分号 END IF; #必须加分号END//delimiter; #往表cmd中插入记录 trigger trigger to insert the error log into cmd (USER, Priv, cmd, sub_time, Succe, depending on the IF condition) SS) 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 '), #查询错误日志, found two mysql> select * from errlog;+----+--- --------------+---------------------+| ID | Err_cmd | Err_time |+----+-----------------+---------------------+| 1 | cat/etc/passwd | 2017-09-14 22:18:48 | | 2 | Useradd XXX | 2017-09-14 22:18:48 |+----+-----------------+---------------------+rows in Set (0.00 sec)
Special: New represents the data row that is about to be inserted, and the old represents the data row that is about to be deleted.
two use triggers
Triggers cannot be called directly by the user, but are known to be passively triggered by the "Add/delete/change" operation on the table.
three delete triggers
Drop trigger Tri_after_insert_cmd;
Database: MySQL built-in features-triggers