This article brings you relevant knowledge about mysql, which mainly introduces related issues about triggers. Triggers are a special stored procedure, triggers and stored procedures. The same is a function that can complete specific functions and storage. I hope it will be helpful to everyone.
Recommended learning: mysql tutorial
Trigger is a special stored procedure. A trigger, like a stored procedure, is a SQL segment that can complete a specific function and is stored on the database server. However, the trigger does not need to be called when performing DML operations on the data in the data table. The execution of this SQL segment is automatically triggered when
, no need to call it manually.
In MySQL, only the insert\delete\update operation can trigger the execution of the trigger. 2. Trigger usagecreate trigger tri_name -- tri_name:触发器别名 <before|after> -- 定义触发时机 <insert|delete|update> -- 定义DML类型 ON <table_name> -- 要触发的表 for each <row|statement> -- for each row 声明为⾏级触发器(操作的每⼀条记录就触发触发器执⾏⼀次) -- for each statement 声明语句级触发器(触发动作体执行一次) begin -- 当只有一句sql语句时,begin...end 可以省略 sql_statement -- 进行触发器对应的DML类型的操作 end; #示例 -- 创建触发器:当学⽣信息表发⽣添加操作时,则向⽇志信息表中记录⼀条⽇志 create trigger tri_test1 after insert on students for each row insert into stulogs(time,log_text) values(now(), concat('添 加',NEW.stu_num,'学⽣信息'));
#查看全部触发器 show triggers; #查看触发器的创建语句 show create trigger 触发器名字;
drop trigger 触发器名字;
NEW: used in the trigger to get the data added by the insert operation and modified by the update operation Recordsome DML related operations; we can use the NEW and OLD keywords to trigger Get the data of the DML operation that triggers this trigger
#insert操作中:NEW表示添加的新记录 create trigger tri_test1 after insert on students for each row insert into stulogs(time,log_text) values(now(), concat('添 加',NEW.stu_num,'学⽣信息')); #update操作中:NEW 表示修改后的数据 -- 创建触发器 : 在监听update操作的触发器中,可以使⽤NEW获取修改后的数据 create trigger tri_test2 after update on students for each row insert into stulogs(time,log_text) values(now(), concat('修改学⽣信息 为:',NEW.stu_num,NEW.stu_name));
#delete操作中:OLD表示删除的记录 create trigger tri_test3 after delete on students for each row insert into stulogs(time,log_text) values(now(), concat('删 除',OLD.stu_num,'学⽣信息')); #update操作中:OLD表示修改前的记录 create trigger tri_test2 after update on students for each row insert into stulogs(time,log_text) values(now(), concat('将学⽣姓名从 【',OLD.stu_name,'】修改为【',NEW.stu_name,'】'));
For projects with small concurrency, you can choose to use Use stored procedures, but the use of stored procedures is not recommended in Internet references
(Reason: when storing procedures, the logic of business implementation is handed over to the database for processing, which increases or decreases the load on the database; Not conducive to database migration)
Recommended learning:
mysql learning tutorialThe above is the detailed content of Detailed explanation of triggers in MySQL learning summary. For more information, please follow other related articles on the PHP Chinese website!