当前位置:网站首页>MySQL storage functions and triggers
MySQL storage functions and triggers
2022-07-19 14:41:00 【After all, Yin Jianwei】
Storage function :
Introduce :
A stored function is a stored procedure with a return value , The parameters of the storage function can only be in type .
create function Store function name ([ parameter list ])
returns type [characteristic]
begin
--SQL
return...;
end;
characateristic explain :
deterministic: The same input parameters always produce the same results
NO SQL: It doesn't contain SQL sentence
READs SQL DATA: Contains statements that read data , But it doesn't include statements that write data
trigger :
Introduce :
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 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 |
grammar :
# establish
create trigger trigger_name
before/after/ insert/update/delete
on tbl_name for each row -- Line level triggers
begin
trigger_stmt;
end;
# see
show triggers;
# Delete
drop trigger [schema_name.]trigger_name; -- If not specified schema_name, Default to current data
practice : Defining triggers , Complete the following requirements
Record by trigger student Data change log for table , Insert the change log into the log table student_logs in , Including the addition of , modify
Delete ;
Trigger when inserting data :
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(' The inserted data content is :sno=',new.sno,',name=',new.sname));
-> end //
Query OK, 0 rows affected (0.01 sec)
mysql> insert into student values('20050601',' Zhao Liu ',' male ',now(),' Guiyang, Guizhou ',' Department of economics and management ',' Financial management ');
-> //
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 | The inserted data content is :sno=20050601,name= Zhao Liu |
+----+-----------+---------------------+------------+------------------------------------------+
1 row in set (0.00 sec)
Trigger for modifying data :
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(' The updated data content is :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 = ' Zhao Liu ';
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 | The inserted data content is :sno=20050601,name= Zhao Liu |
| 2 | update | 2022-05-25 14:13:42 | 20050601 | The updated data content is :sno=20050601,name= Zhao Liu |
+----+-----------+---------------------+------------+------------------------------------------+
2 rows in set (0.00 sec)
Trigger for deleting data :
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(' The deleted data content is :sno=',old.sno,',name=',old.sname));
-> end //
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ;
mysql> delete from student where sname = ' Zhao Liu ';
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 | The inserted data content is :sno=20050601,name= Zhao Liu |
| 2 | update | 2022-05-25 14:13:42 | 20050601 | The updated data content is :sno=20050601,name= Zhao Liu |
| 3 | delete | 2022-05-25 14:16:51 | 20050601 | The deleted data content is :sno=20050601,name= Zhao Liu |
+----+-----------+---------------------+------------+------------------------------------------+
3 rows in set (0.00 sec)
边栏推荐
- C - usage of this
- Alibaba microservice component Nacos registry
- 07--- Brewster point
- JSON path syntax introduction and usage scenarios
- Optimizer of pytoch framework optimizer
- Prefix Equality 【DP | 哈希】
- 华为无线设备配置动态负载均衡
- Keil环境下STM32定位hardfault位置方法和遇到的情况
- 慎用TongWeb的热部署功能
- Huawei wireless device configuration user CAC
猜你喜欢
随机推荐
通达信开户是真的吗?通达信开户安全吗?
Database SQL Server
【MQTT从入门到提高系列 | 07】MQTT3.1.1之链路保活及断开
Redis源码与设计剖析 -- 2.链表
华为无线设备配置用户CAC
Win10 Microsoft Store cannot be opened (enable TLS 1.2)
函數初認識-下
ospf 附录 防环 重发布
2021 national vocational college skills competition network construction and application events
OSPF appendix anti ring Reissue
O'Neill's RPS curve compilation method (original by Dr. Tao)
ping 命令还能这么玩?
AcWing 274. Mobile services [DP]
Opencv template
Huawei technologies:jonathan Krolikowski | from design to deployment, zero contact deep reinforcement learning WLANs
Ranking and evaluation of the second "green tree Cup" Mathematics Competition
树与二分图【思维】
洛谷:P4516 [JSOI2018] 潜入行动(树形dp、树上分组背包统计方案数)
07--- Brewster point
matplotlib绘制多折线图(解决matplotlib中文无法显示问题)









