当前位置:网站首页>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 .
边栏推荐
- Three stages of aho
- 下半年还有很多事要做
- Report on the "14th five year plan" and investment strategy recommendations for China's industrial robot industry 2022 ~ 2028
- 科学创业三问:关于时机、痛点与重要决策
- First intention is the most important
- Queue operation---
- 阿霍的三个阶段
- 不同的测试技术区分
- 运行Powershell脚本提示“因为在此系统上禁止运行脚本”解决办法
- How can genetic testing help patients fight disease?
猜你喜欢

晓看天色暮看云,美图欣赏

项目部署,一点也不难!
![leetcode:226. Flip binary tree [DFS flip]](/img/b8/6c5596ac30de59f0f347bb0bddf574.png)
leetcode:226. Flip binary tree [DFS flip]

ROS2 Foxy depthai_ ROS tutorial

Zabbix 6.0 源码安装以及 HA 配置

codeforces -- 4B. Before an Exam

Look at the sky at dawn and the clouds at dusk, and enjoy the beautiful pictures

Vs code setting Click to open a new file window without overwriting the previous window

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

硬件开发笔记(九): 硬件开发基本流程,制作一个USB转RS232的模块(八):创建asm1117-3.3V封装库并关联原理图元器件
随机推荐
高薪程序员&面试题精讲系列118之Session共享有哪些方案?
Idea of [developing killer]
VM虚拟机配置动态ip和静态ip访问
科学创业三问:关于时机、痛点与重要决策
華為面試題: 招聘
Huawei interview question: Recruitment
软件测试中功能测试流程
Update a piece of data from the database. Will CDC get two pieces of data with OP fields D and C at the same time? I remember before, only OP was U
Operator-1 first acquaintance with operator
The difference between memcpy and strcpy
工具箱之 IKVM.NET 项目新进展
从数据库中更新一条数据,用cdc会同时获得op字段分别为d和c的两条数据吗?我记得之前是只有op为u
商汤科技崩盘 :IPO时已写好的剧本
How to play with the reading and writing operations of blocking sockets?
游戏公会在去中心化游戏中的未来
leetcode:226. 翻转二叉树【dfs翻转】
简单斐波那契(递推)
Flinkcdc should extract Oracle in real time. What should be configured for oracle?
leetcode:329. 矩阵中的最长递增路径【dfs + cache + 无需回溯 + 优雅】
Different test techniques