当前位置:网站首页>MySQL advanced part 1: triggers
MySQL advanced part 1: triggers
2022-07-05 06:13:00 【Dawnlighttt】
List of articles
Trigger introduction
Triggers are database objects related to tables , Referring to insert/update/delete Before or after , Trigger and execute the... Defined in the trigger SQL Statement set . This feature of triggers can assist in application on the database side Ensure data integrity , logging , data verification Wait for the operation .
Use the alias OLD and NEW To refer to the changed record content in the trigger , This is similar to other databases . Now triggers only support row level triggering , Statement level triggering is not supported .(Oracle Existing row level triggers , There are also statement level triggers )
| Trigger Type | NEW and OLD Use |
|---|---|
| INSERT Type trigger | NEW Indicates the data to be added or added |
| UPDATE Type trigger | OLD Represents the data before modification , NEW Represents the data that will be or has been modified |
| DELETE Type trigger | OLD Data that will be or has been deleted |
Create trigger
Grammatical structure :
create trigger trigger_name
before/after insert/update/delete
on tbl_name
[ for each row ] -- Line level triggers
begin
trigger_stmt ;
end;
Example
demand
Record by trigger emp Data change log for table , Including the addition of , modify , Delete ;
First, create a log table :
create table emp_logs(
id int(11) not null auto_increment,
operation varchar(20) not null comment ' Operation type , insert/update/delete',
operate_time datetime not null comment ' Operating time ',
operate_id int(11) not null comment ' Operation table ID',
operate_params varchar(500) comment ' Operating parameters ',
primary key(`id`)
)engine=innodb default charset=utf8;
establish insert Type trigger , Complete logging when inserting data :
DELIMITER $
create trigger emp_logs_insert_trigger
after insert
on emp
for each row
begin
insert into emp_logs (id,operation,operate_time,operate_id,operate_params) values(null,'insert',now(),new.id,concat(' After inserting (id:',new.id,', name:',new.name,', age:',new.age,', salary:',new.salary,')'));
end $
DELIMITER ;
establish update Type trigger , Complete logging when updating data :
DELIMITER $
create trigger emp_logs_update_trigger
after update
on emp
for each row
begin
insert into emp_logs (id,operation,operate_time,operate_id,operate_params) values(null,'update',now(),new.id,concat(' Before the change (id:',old.id,', name:',old.name,', age:',old.age,', salary:',old.salary,') , After modification (id',new.id, 'name:',new.name,', age:',new.age,', salary:',new.salary,')'));
end $
DELIMITER ;
establish delete Triggers for rows , Complete logging when deleting data :
DELIMITER $
create trigger emp_logs_delete_trigger
after delete
on emp
for each row
begin
insert into emp_logs (id,operation,operate_time,operate_id,operate_params) values(null,'delete',now(),old.id,concat(' Before deleting (id:',old.id,', name:',old.name,', age:',old.age,', salary:',old.salary,')'));
end $
DELIMITER ;
test :
insert into emp(id,name,age,salary) values(null, ' The left emissary of light ',30,3500);
insert into emp(id,name,age,salary) values(null, ' Bright right emissary ',33,3200);
update emp set age = 39 where id = 3;
delete from emp where id = 5;
Delete trigger
Grammatical structure :
drop trigger [schema_name.]trigger_name
If not specified schema_name, The default is the current database .
Check triggers
Can be executed by SHOW TRIGGERS Command to view the status of the trigger 、 Grammar and other information .
Grammatical structure :
show triggers ;
边栏推荐
- Introduction et expérience de wazuh open source host Security Solution
- New title of module a of "PanYun Cup" secondary vocational network security skills competition
- Scope of inline symbol
- 中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
- [rust notes] 14 set (Part 2)
- [practical skills] technical management of managers with non-technical background
- 快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
- RGB LED infinite mirror controlled by Arduino
- 【Rust 笔记】15-字符串与文本(下)
- The difference between CPU core and logical processor
猜你喜欢

Appium foundation - use the first demo of appium

Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135

Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135

Quickly use Amazon memorydb and build your own redis memory database

Traditional databases are gradually "difficult to adapt", and cloud native databases stand out

Overview of variable resistors - structure, operation and different applications

Smart construction site "hydropower energy consumption online monitoring system"

Leetcode-6110: number of incremental paths in the grid graph
![[cloud native] record of feign custom configuration of microservices](/img/39/05cf7673155954c90e75a8a2eecd96.jpg)
[cloud native] record of feign custom configuration of microservices

7. Processing the input of multidimensional features
随机推荐
Leetcode-22: bracket generation
Simply sort out the types of sockets
Individual game 12
TypeScript 基础讲解
2022年貴州省職業院校技能大賽中職組網絡安全賽項規程
实时时钟 (RTC)
Time of process
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
leetcode-6110:网格图中递增路径的数目
Leetcode-6110: number of incremental paths in the grid graph
[rust notes] 14 set (Part 1)
LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively
2022 pole technology communication arm virtual hardware accelerates the development of Internet of things software
[practical skills] technical management of managers with non-technical background
Leetcode-31: next spread
【Rust 笔记】14-集合(上)
数据可视化图表总结(二)
【Rust 笔记】17-并发(下)
1039 Course List for Student