当前位置:网站首页>MySQL存储函数和触发器
MySQL存储函数和触发器
2022-07-17 08:31:00 【毕竟尹稳健】
存储函数:
介绍:
存储函数是有返回值的存储过程,存储函数的参数只能是in类型。
create function 存储函数名称([参数列表])
returns type [characteristic]
begin
--SQL
return...;
end;
characateristic说明:
deterministic:相同的输入参数总是产生相同的结果
NO SQL:不包含SQL语句
READs SQL DATA:包含读取数据的语句,但不包含写入数据的语句
触发器:
介绍:
触发器是与表有关的数据库对象,指在insert/update/delete之前或之后,触发并执行触发器中定义的SQL语句集合。触发器的这种特性可以协助应用在数据库端确保数据的完整性,日志记录,数据校验等操作。
使用别名OLD和NEW来引用触发器中发生变化的记录内容,这与其他的数据库是相似的。现在触发器还只支持行级触发,不支持语句级触发。
| 触发器类型 | NEW和OLD |
|---|---|
| INSERT型触发器 | NEW表示将要或者已经新增的数据 |
| UPDATE型触发器 | OLD表示修改之前的数据,NEW表示将要或已经修改后的数据 |
| DELETE型触发器 | OLD表示将要或者已经删除的数据 |
语法:
# 创建
create trigger trigger_name
before/after/ insert/update/delete
on tbl_name for each row -- 行级触发器
begin
trigger_stmt;
end;
# 查看
show triggers;
# 删除
drop trigger [schema_name.]trigger_name; -- 如果没有指定schema_name,默认为当前数据
练习:定义触发器,完成如下需求
通过触发器记录student表的数据变更日志,将变更日志插入到日志表student_logs中,包含增加,修改
删除;
插入数据时的触发器:
mysql> delimiter //
mysql> create trigger student_insert_trigger
-> after insert
-> on student for each row
-> begin
-> insert into student_logs values(null,'insert',now(),new.sno,concat('插入的数据内容为:sno=',new.sno,',name=',new.sname));
-> end //
Query OK, 0 rows affected (0.01 sec)
mysql> insert into student values('20050601','赵六','男',now(),'贵州贵阳','经济管理系','财政管理');
-> //
Query OK, 1 row affected (0.01 sec)
mysql> select * from student_logs;
+----+-----------+---------------------+------------+------------------------------------------+
| id | operation | operate_time | operate_id | operate_params |
+----+-----------+---------------------+------------+------------------------------------------+
| 1 | insert | 2022-05-25 14:08:09 | 20050601 | 插入的数据内容为:sno=20050601,name=赵六 |
+----+-----------+---------------------+------------+------------------------------------------+
1 row in set (0.00 sec)
修改数据的触发器:
mysql> delimiter //
mysql> create trigger student_update_trigger
-> after update
-> on student for each row
-> begin
-> insert into student_logs values(null,'update',now(),new.sno,concat('更新的数据内容为:sno=',new.sno,',name=',new.sname));
-> end //
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ;
mysql> update student set sbirthday = now() where sname = '赵六';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student_logs;
+----+-----------+---------------------+------------+------------------------------------------+
| id | operation | operate_time | operate_id | operate_params |
+----+-----------+---------------------+------------+------------------------------------------+
| 1 | insert | 2022-05-25 14:08:09 | 20050601 | 插入的数据内容为:sno=20050601,name=赵六 |
| 2 | update | 2022-05-25 14:13:42 | 20050601 | 更新的数据内容为:sno=20050601,name=赵六 |
+----+-----------+---------------------+------------+------------------------------------------+
2 rows in set (0.00 sec)
删除数据的触发器:
mysql> delimiter //
mysql> create trigger student_delete_trigger
-> after delete
-> on student for each row
-> begin
-> insert into student_logs values(null,'delete',now(),old.sno,concat('删除的数据内容为:sno=',old.sno,',name=',old.sname));
-> end //
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ;
mysql> delete from student where sname = '赵六';
Query OK, 1 row affected (0.01 sec)
mysql> select * from student_logs;
+----+-----------+---------------------+------------+------------------------------------------+
| id | operation | operate_time | operate_id | operate_params |
+----+-----------+---------------------+------------+------------------------------------------+
| 1 | insert | 2022-05-25 14:08:09 | 20050601 | 插入的数据内容为:sno=20050601,name=赵六 |
| 2 | update | 2022-05-25 14:13:42 | 20050601 | 更新的数据内容为:sno=20050601,name=赵六 |
| 3 | delete | 2022-05-25 14:16:51 | 20050601 | 删除的数据内容为:sno=20050601,name=赵六 |
+----+-----------+---------------------+------------+------------------------------------------+
3 rows in set (0.00 sec)
边栏推荐
- Ribbon load balancing service call
- VMware中扩展硬盘
- 力扣1669合并两个链表笔记
- Collation of exercises of shallow neural networks in the third week of in-depth study
- JS study note 04-05 - modification of constructor and creation using factory method
- Sorting out of neural network basics exercises in the second week of in-depth study
- Solutions to license invalidation caused by MATLAB update
- Hcip - Comprehensive Experiment of OSPF
- 46. IO model
- JSON under mysql8
猜你喜欢

2022广东省安全员A证第三批(主要负责人)练习题及模拟考试

Matlab imports floating-point numbers with more than 9 digits after the decimal point

Redis

Use torch NN builds the simplest neural network framework

BCG 使用之CBCGPEdit控件

1. Decision tree

Finishing of key concepts on deep neural networks exercises in the fourth week of in-depth learning

Ribbon load balancing service call

1. Flask Foundation

力扣1669合並兩個鏈錶筆記
随机推荐
id Tech5的MegaTexture技术
【LeetCode】通用操作总结
Li Kou 43 string multiplication note
【人脸识别】基于直方图Histogram实现人脸识别附matlab代码
力扣455分发饼干笔记
分布式事务-最大努力通知方案
零基础C语言
Solutions to license invalidation caused by MATLAB update
JS learning notes 01-03 - Reference of this, global scope, method
不创建临时变量交换数组元素
Consul service registration and discovery
使用toruch.nn搭建最简单的神经网络骨架
Redis
深度学习第一周Introduction to Deep Learning习题整理
Eureka Basics
JS learning notes 06-08: traversal of arrays and four methods of arrays
6-9漏洞利用-Telnet登陆提权
MySQL data type
Redis overview installation
JS learning notes 06-08: traversal of arrays and four methods of arrays