当前位置:网站首页>PG基础篇--逻辑结构管理(触发器)
PG基础篇--逻辑结构管理(触发器)
2022-07-01 12:42:00 【51CTO】
创建触发器
1)先为触发器建一个执行函数,此函数的返回类型为触发器类型
2)建一个触发器
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
,
'张三'
,
14
)
;
insert
into student
values
(
2
,
'李四'
,
14
)
;
INSERT
0
1
postgres
=#
insert
into student
values
(
2
,
'李四'
,
14
)
;
INSERT
0
1
postgres
=#
insert
into student
values
(
3
,
'王二'
,
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.
语句级触发器
语句级触发器指执行每个sql语句时只执行一次。
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是触发器函数中的特殊变量,代表DML操作类型
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.
虽然删了2条记录,但是执行的是一条语句,所以在log_student中只记录了一次操作。
行级触发器
行级触发器执行每行SQL语句都会执行一次
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
,
'张三'
,
14
)
,
(
2
,
'李四'
,
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.
可以看到一行sql执行插入2行数据,日志表中记录了2条记录
BEFORE触发器
语句级别的BEFOR触发器是在语句开始做任何事情之前就被触发的
行级别的BEFORE触发器是在对特定行进行操作之前触发的。
AFTER触发器
语句级的AFTER触发器是在语句结束时才触发的
行级别的AFTER触发器是在语句结束时才触发的,它会在任何语句级别的AFTER触发器之前触发。
删除触发器
if exists 如果触发器不存在,发出一个notice而不是一个错误
cascade级联删除依赖此触发器的对象
restrict有依赖的对象就拒绝删除
注意:删除触发器时,触发器的函数不会被删除,不过,删除表时,表上的触发器会被删除。
触发器函数有返回值,语句级触发器应该总是返回NULL,即必选显示的在触发器函数写上return null,否则报错。
边栏推荐
猜你喜欢

"Analysis of 43 cases of MATLAB neural network": Chapter 40 research on prediction of dynamic neural network time series -- implementation of NARX based on MATLAB

Manage nodejs with NVM (downgrade the high version to the low version)
![79. Word search [DFS + backtracking visit + traversal starting point]](/img/d6/a7693b2af435b7cf4562161ca4bd3f.png)
79. Word search [DFS + backtracking visit + traversal starting point]

Fundamentals of number theory and its code implementation

Operations related to sequence table

redis探索之缓存一致性

基于开源流批一体数据同步引擎 ChunJun 数据还原 —DDL 解析模块的实战分享

Operator-1初识Operator

CS5268优势替代AG9321MCQ Typec多合一扩展坞方案

数论基础及其代码实现
随机推荐
List of QT players [easy to understand]
Tencent always takes epoll, which is annoying
Exploration and practice of inress in kubernetes
使用BurpSuite对app抓包教程
[encounter Django] - (II) database configuration
79. 单词搜索【dfs + 回溯visit + 遍历起点】
leetcode:329. The longest incremental path in the matrix [DFS + cache + no backtracking + elegance]
Zero copy technology of MySQL
运行Powershell脚本提示“因为在此系统上禁止运行脚本”解决办法
logstash报错:Cannot reload pipeline, because the existing pipeline is not reloadable
腾讯总考epoll, 很烦
Fundamentals of number theory and its code implementation
Chain storage of binary tree
【MAUI】为 Label、Image 等控件添加点击事件
网络socket的状态要怎么统计?
使用nvm管理nodejs(把高版本降级为低版本)
晓看天色暮看云,美图欣赏
Tencent Li Wei: deeply cultivate "regulatory technology" to escort the steady and long-term development of the digital economy
CPI教程-异步接口创建及使用
79. Word search [DFS + backtracking visit + traversal starting point]