当前位置:网站首页>PG basics -- Logical Structure Management (trigger)
PG basics -- Logical Structure Management (trigger)
2022-07-01 12:53:00 【51CTO】
Create trigger
1) First, create an execution function for the trigger , The return type of this function is trigger type
2) Build a trigger
postgres
=#
create function student_delete_trigger
(
)
postgres
-# returns trigger
as $$
postgres$#
begin
postgres$#
delete
from score
where student_no
=OLD
.student_no
;
postgres$# return OLD
;
postgres$# end
;
postgres$# $$
postgres
-# language plpgsql
;
CREATE FUNCTION
postgres
=#
postgres
=#
create trigger delete_student_trigger
postgres
-# after
delete
on student
postgres
-# for each row execute procedure student_delete_trigger
(
)
;
CREATE TRIGGER
postgres
=#
insert
into student
values
(
1
,
' Zhang San '
,
14
)
;
insert
into student
values
(
2
,
' Li Si '
,
14
)
;
INSERT
0
1
postgres
=#
insert
into student
values
(
2
,
' Li Si '
,
14
)
;
INSERT
0
1
postgres
=#
insert
into student
values
(
3
,
' The king 2 '
,
14
)
;
INSERT
0
1
postgres
=#
postgres
=#
insert
into score
values
(
1
,
85
,
75
,
date '2022-06-29'
)
;
INSERT
0
1
postgres
=#
insert
into score
values
(
1
,
80
,
73
,
date '2022-01-29'
)
;
INSERT
0
1
postgres
=#
insert
into score
values
(
2
,
87
,
75
,
date '2022-03-29'
)
;
INSERT
0
1
postgres
=#
insert
into score
values
(
3
,
75
,
75
,
date '2022-06-29'
)
;
INSERT
0
1
postgres
=#
insert
into score
values
(
3
,
55
,
75
,
date '2022-04-29'
)
;
INSERT
0
1
postgres
=#
delete
from student
where student_no
=
3
;
DELETE
1
postgres
=#
select
*
from score
;
student_no
| chinese_score
| math_score
| test_date
------------+---------------+------------+------------
1
|
85
|
75
|
2022
-
06
-
29
1
|
80
|
73
|
2022
-
01
-
29
2
|
87
|
75
|
2022
-
03
-
29
(
3 rows
)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
Statement level triggers
Statement level triggers refer to the execution of each sql Statement is executed only once .
create
table log_student
(
update_time
timestamp
,
db_user
varchar
(
40
)
,
opr_type
varchar
(
6
)
)
create
or replace function log_student_trigger
(
)
returns trigger
as $$
begin
insert
into log_student
values
(now
(
)
,user
,TG_OP
)
;
return
null
;
end
;
$$
language
"plpgsql"
;
TG_OP Is a special variable in the trigger function , representative DML Operation type
create trigger log_student_trigger
after
insert
or
delete
or
update
on student
for statement execute procedure log_student_trigger
(
)
;
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
Although deleted 2 Bar record , But the execution is a statement , So in log_student Only one operation is recorded in .
Line level triggers
Row level triggers execute each row SQL Statements are executed once
create trigger log_student_trigger2
after
insert
or
delete
or
update
on student
for ROW EXECUTE PROCEDURE log_student_trigger
(
)
;
postgres
=#
insert
into student
values
(
1
,
' Zhang San '
,
14
)
,
(
2
,
' Li Si '
,
30
)
;
INSERT
0
2
postgres
=#
select
*
from log_student
;
update_time
| db_user
| opr_type
----------------------------+----------+----------
2022
-
06
-
30
10
:
42
:
58.834223
| postgres
|
INSERT
2022
-
06
-
30
10
:
42
:
58.834223
| postgres
|
INSERT
(
2 rows
)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
You can see a line sql Execution insert 2 Row data , The log table records 2 Bar record
BEFORE trigger
Statement level BEFOR Triggers are triggered before the statement starts doing anything
Line level BEFORE Triggers are triggered before the operation on a specific row .
AFTER trigger
Statement level AFTER Triggers are triggered at the end of the statement
Line level AFTER Triggers are triggered at the end of the statement , It will be at any statement level AFTER Trigger before trigger .
Delete trigger
if exists If the trigger does not exist , Send out a notice Not a mistake
cascade Cascade delete objects that depend on this trigger
restrict If there are dependent objects, they refuse to delete
Be careful : When you delete a trigger , The function of the trigger will not be deleted , however , When deleting a table , The trigger on the table will be deleted .
Trigger function has return value , Statement level triggers should always return NULL, That is, the required display is written in the trigger function return null, Otherwise, the report will be wrong .
边栏推荐
- 华为面试题: 招聘
- leetcode:226. 翻转二叉树【dfs翻转】
- [today in history] July 1: the father of time sharing system was born; Alipay launched barcode payment; The first TV advertisement in the world
- 阿霍的三个阶段
- Tencent always takes epoll, which is annoying
- 软件测试中功能测试流程
- 快速整明白Redis中的压缩列表到底是个啥
- Redis explores cache consistency
- There are risks in trading
- Interpretation of hard threshold function [easy to understand]
猜你喜欢

79. 单词搜索【dfs + 回溯visit + 遍历起点】

《MATLAB 神经网络43个案例分析》:第40章 动态神经网络时间序列预测研究——基于MATLAB的NARX实现

Zabbix 6.0 源码安装以及 HA 配置

Manage nodejs with NVM (downgrade the high version to the low version)

Fiori 应用通过 Adaptation Project 的增强方式分享
![Idea of [developing killer]](/img/74/f4a18afd2b86373996e4ca62b50f88.png)
Idea of [developing killer]

【邂逅Django】——(二)数据库配置

华为HMS Core携手超图为三维GIS注入新动能

VS Code 设置单击打开新文件窗口,不覆盖前一个窗口

redis探索之缓存击穿、缓存雪崩、缓存穿透
随机推荐
Mysql间隙锁
Function test process in software testing
[20220605] Literature Translation -- visualization in virtual reality: a systematic review
79. 单词搜索【dfs + 回溯visit + 遍历起点】
localtime居然不可重入,踩坑了
be based on. NETCORE development blog project starblog - (13) add friendship link function
手机便签应用
Topic 2612: the real topic of the 12th provincial competition of the Blue Bridge Cup in 2021 - the least weight (enumerating and finding rules + recursion)
Tencent Li Wei: deeply cultivate "regulatory technology" to escort the steady and long-term development of the digital economy
数字信号处理——线性相位型(Ⅱ、Ⅳ型)FIR滤波器设计(2)
List of QT players [easy to understand]
Tencent security released the white paper on BOT Management | interpreting BOT attacks and exploring ways to protect
Tencent security and KPMG released a regulatory technology white paper to analyze the "3+3" hot application scenarios
I spent tens of thousands of dollars to learn and bring goods: I earned 3 yuan in three days, and the transaction depends on the bill
VS Code 设置单击打开新文件窗口,不覆盖前一个窗口
Feign & Eureka & Zuul & Hystrix 流程
VM virtual machine configuration dynamic IP and static IP access
项目部署,一点也不难!
Detailed explanation of OSPF LSA of routing Foundation
Development trend and market demand analysis report of China's high purity copper industry Ⓕ 2022 ~ 2028