当前位置:网站首页>MySQL advanced trigger -- trigger introduction, trigger syntax and trigger case
MySQL advanced trigger -- trigger introduction, trigger syntax and trigger case
2022-04-22 20:46:00 【CodeJiao】
List of articles
1. Trigger introduction
Triggers are database objects related to tables , Referring to insert / update / delete Before (BEFORE) Or after (AFTER), Trigger and execute the... Defined in the trigger SQL Statement set . This feature of trigger can help to ensure the integrity of data in database , logging , Data verification and other operations .
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 .
| Trigger Type | NEW and OLD |
|---|---|
| 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 |
2. Trigger Syntax
2.1 Create trigger
CREATE TRIGGER trigger_name
BEFORE/AFTER INSERT/UPDATE/DELETE
ON tbl_name FOR EACH ROW -- Line level triggers
BEGIN
trigger_stmt;
END;
2.2 Check triggers
SHOW TRIGGERS;
2.3 Delete trigger
-- If not specified schema_name, The default is the current database
DROP TRIGGER [schema_name.]trigger_name ;
3. Trigger case
Record by trigger tb_user Data change log for table , Insert the change log into the log table user_logs in , Including the addition of 、 modify 、 Delete ;
Table structure preparation :
-- preparation : Log table user_logs
create table user_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 of the ID',
operate_params varchar(500) comment ' Operating parameters ',
primary key (`id`)
) engine = innodb
default charset = utf8;
Sample data preparation :
create table tb_user
(
id int primary key auto_increment comment ' Primary key ',
name varchar(50) not null comment ' user name ',
phone varchar(11) not null comment ' cell-phone number ',
email varchar(100) comment ' mailbox ',
profession varchar(11) comment ' major ',
age tinyint unsigned comment ' Age ',
gender char(1) comment ' Gender , 1: male , 2: Woman ',
status char(1) comment ' state ',
createtime datetime comment ' Creation time '
) comment ' System user table ';
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' Lyu3 bu4 ', '17799990000', '[email protected]', ' Software Engineering ', 23, '1', '6', '2001-02-02 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' Cao Cao ', '17799990001', '[email protected]', ' Communication Engineering ', 33, '1', '0', '2001-03-05 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' zhaoyun ', '17799990002', '[email protected]', ' English ', 34, '1', '2', '2002-03-02 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' The Monkey King ', '17799990003', '[email protected]', ' Project cost ', 54, '1', '0', '2001-07-02 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' Hua mu LAN ', '17799990004', '[email protected]', ' Software Engineering ', 23, '2', '1', '2001-04-22 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' Big Joe ', '17799990005', '[email protected]', ' dance ', 22, '2', '0', '2001-02-07 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' Luna ', '17799990006', '[email protected]', ' Applied Mathematics ', 24, '2', '0', '2001-02-08 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' Cheng Yaojin ', '17799990007', '[email protected]', ' chemical ', 38, '1', '5', '2001-05-23 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' Xiang yu ', '17799990008', '[email protected]', ' Metal material ', 43, '1', '0', '2001-09-18 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' White ', '17799990009', '[email protected]', ' Mechanical engineering and its automation turn ', 27, '1', '2', '2001-08-16 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' Han xin ', '17799990010', '[email protected]', ' Inorganic nonmetallic material worker cheng ', 27, '1', '0', '2001-06-12 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' Jingke ', '17799990011', '[email protected]', ' accounting ', 29, '1', '0', '2001-05-11 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' The king of Lanling ', '17799990012', '[email protected]', ' Project cost ', 44, '1', '1', '2001-04-09 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' Berserker iron ', '17799990013', '[email protected]', ' Applied Mathematics ', 43, '1', '2', '2001-04-10 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' The sable cicada ', '17799990014', '[email protected]', ' Software Engineering ', 40, '2', '3', '2001-02-12 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' Daji ', '17799990015', '[email protected]', ' Software Engineering ', 31, '2', '0', '2001-01-30 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' Mi month ', '17799990016', '[email protected]', ' industrial economy ', 35, '2', '0', '2000-05-03 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' Ying Zheng ', '17799990017', '[email protected]', ' chemical ', 38, '1', '1', '2001-08-08 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' Judge dee ', '17799990018', '[email protected]', ' International Trade ', 30, '1', '0', '2007-03-12 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' Angela ', '17799990019', '[email protected]', ' city planning ', 51, '2', '0', '2001-08-15 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' Dianwei ', '17799990020', '[email protected]', ' city planning ', 52, '1', '2', '2000-04-12 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' Lian po ', '17799990021', '[email protected]', ' Civil Engineering ', 19, '1', '3', '2002-07-18 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' Hou Yi ', '17799990022', '[email protected]', ' Urban landscape ', 20, '1', '0', '2002-03-10 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime)
VALUES (' Jiang Ziya ', '17799990023', '[email protected]', ' Project cost ', 29, '1', '4', '2003-05-26 00:00:00');
3.1 Insert data trigger
create trigger tb_user_insert_trigger
after insert
on tb_user
for each row
begin
insert into user_logs(id, operation, operate_time, operate_id, operate_params)
VALUES (null, 'insert', now(), new.id,
concat(' The inserted data content is : id=', new.id, ',name=', new.name, ', phone=', NEW.phone, ', email=', NEW.email,
', profession=', NEW.profession));
end;
test :
-- see
show triggers;

-- Insert data into tb_user
insert into tb_user(id, name, phone, email, profession, age, gender, status, createtime)
VALUES (26, ' Second prince ', '18809091212', '[email protected]', ' Software Engineering ', 23, '1', '1', now());
see user_logs The data in it

After the test , Check whether the data in the log table can be inserted normally , And the correctness of the inserted data .
3.2 Modify data trigger
create trigger tb_user_update_trigger
after update
on tb_user
for each row
begin
insert into user_logs(id, operation, operate_time, operate_id, operate_params)
VALUES (null, 'update', now(), new.id,
concat(' Update previous data : id=', old.id, ',name=', old.name, ', phone=', old.phone, ', email=', old.email,
', profession=', old.profession, ' | The updated data : id=', new.id, ',name=', new.name, ', phone=',
NEW.phone, ', email=', NEW.email, ', profession=', NEW.profession));
end;
-- see
show triggers;

-- to update
update tb_user
set profession = ' accounting '
where id = 23;
update tb_user
set profession = ' accounting '
where id <= 5;
After the test , Check whether the data in the log table can be inserted normally , And the correctness of the inserted data .

3.3 Delete data trigger
create trigger tb_user_delete_trigger
after delete
on tb_user
for each row
begin
insert into user_logs(id, operation, operate_time, operate_id, operate_params)
VALUES (null, 'delete', now(), old.id,
concat(' Delete previous data : id=', old.id, ',name=', old.name, ', phone=', old.phone, ', email=', old.email,
', profession=', old.profession));
end;
View new delete data triggers :
show triggers;

-- Delete data
delete
from tb_user
where id = 26;
After the test , Check whether the data in the log table can be inserted normally , And the correctness of the inserted data .

4. Summary

版权声明
本文为[CodeJiao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222041496235.html
边栏推荐
- LeeCode 130. Surrounded area
- 做了5年Android,靠着这份190页的面试资料,成功入职腾讯
- 工控安全解决方案
- Postman tests the correct posture of array, list and map input APIs
- 显示实现接口和隐式实现接口的区别
- Use constant member functions for constant types (design a date class and time)
- 7-1 C language programming experiment 6-3 insertion of one-way linked list (30 points)
- 华为机考题汇总
- Ordinary functions as friends (using examples to solve friend functions)
- 华为机试题——HJ62 查找输入整数二进制中1的个数
猜你喜欢

Leetcode学习计划之动态规划入门day1,2(共4题)

Error running ‘JeecgSystemApplication‘: Command line is too long. Shorten command line for JeecgSyst

Asynchronous replication of MySQL master-slave replication

Gtid replication of MySQL master-slave replication

未授权访问漏洞总结

MySQL 进阶 触发器 -- 触发器介绍、触发器语法、触发器案例

Lenovo computer housekeeper graphic introduction: how to download Lenovo computer housekeeper?

Detailed explanation of sorting methods (8 kinds) - bucket sorting

@Requestmapping get request parameters

H. Maximal AND
随机推荐
Use of swift extension
String. Join() and stringutils Join () gracefully solves the splicing of arrays or collections
字符数组与字符串:删除字符串所有空格。 (10 分)编写一个函数,用来删除字符串中的所有空格。
Leetcode222. Number of nodes of complete binary tree
一文讲透,商业智能BI的未来形态,发展现状及前景分析|推荐收藏
做了5年Android,靠着这份190页的面试资料,成功入职腾讯
L1-046 divide singles
Thinking and summary of multi switch and multi service line design
October's Android interview failed miserably in byte three, and fortunately won Xiaomi offer
Evaluaion mark in natural language processing field//updating
Add / remove / filter / sort array elements
十月的Android面试之旅,惨败在字节三面,幸斩获小米Offer
leetcode222、完全二叉树的节点个数
显示实现接口和隐式实现接口的区别
未授权访问漏洞总结
华为机考题汇总
[office] ppt production process
Huawei machine test question -- hj72 100 money to buy 100 chickens
How to install monitoring without network in rural areas, and what kind of monitoring without WiFi at home
华为机试题——HJ62 查找输入整数二进制中1的个数