Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_pg
提交
92a0b9a1
S
skill_tree_pg
项目概览
CSDN 技术社区
/
skill_tree_pg
通知
9
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
2
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
skill_tree_pg
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
2
Issue
2
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
92a0b9a1
编写于
5月 27, 2022
作者:
M
Mars Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add audit
上级
bfa27941
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
210 addition
and
49 deletion
+210
-49
data/2.PostgreSQL中阶/1.基本结构/5. 触发器/audit.json
data/2.PostgreSQL中阶/1.基本结构/5. 触发器/audit.json
+8
-0
data/2.PostgreSQL中阶/1.基本结构/5. 触发器/audit.md
data/2.PostgreSQL中阶/1.基本结构/5. 触发器/audit.md
+104
-0
data/2.PostgreSQL中阶/1.基本结构/5. 触发器/config.json
data/2.PostgreSQL中阶/1.基本结构/5. 触发器/config.json
+2
-1
data/tree.json
data/tree.json
+96
-48
未找到文件。
data/2.PostgreSQL中阶/1.基本结构/5. 触发器/audit.json
0 → 100644
浏览文件 @
92a0b9a1
{
"type"
:
"code_options"
,
"author"
:
"ccat"
,
"source"
:
"audit.md"
,
"notebook_enable"
:
false
,
"exercise_id"
:
"47ab9b38b16d48ca9a4abd637dfe39b3"
}
\ No newline at end of file
data/2.PostgreSQL中阶/1.基本结构/5. 触发器/audit.md
0 → 100644
浏览文件 @
92a0b9a1
# 交易审计
Orders 表
```
postgresql
create table orders
(
id serial primary key,
item_id int,
amount int,
unit_price money,
total money,
description text,
ts timestamp default now()
);
```
记录了未完成的订单。审计部门现在需要记录其变更——新增或删除,该表不会发生update——即将修改都记录到 orders_log 表
```
postgresql
create table orders_log
(
log_id serial primary key,
id int,
item_id int,
amount int,
unit_price money,
total money,
description text,
ts timestamp,
direction varchar(16),
log_at timestamp default now()
);
```
那么应该如何做?
## 答案
```
postgresql
create or replace function log_order_in() returns trigger as $$
begin
insert into orders_log(id, item_id, amount, unit_price, total, description, ts, direction)
values(NEW.id, NEW.item_id, NEW.amount, NEW.unit_price, NEW.total, NEW.description, NEW.ts, 'in');
return NEW;
end
$$ language plpgsql;
create or replace function log_order_out() returns trigger as $$
begin
insert into orders_log(id, item_id, amount, unit_price, total, description, ts, direction)
values(OLD.id, OLD.item_id, OLD.amount, OLD.unit_price, OLD.total, OLD.description, OLD.ts, 'out');
return OLD;
end
$$ language plpgsql;
create trigger in_orders after insert on orders
for each row execute function log_order_in();
create trigger out_orders after delete on orders
for each row execute function log_order_out();
```
## 选项
### A
```
postgresql
create trigger in_orders after insert on orders
for each row insert into orders_log(id, item_id, amount, unit_price, total, description, ts, direction)
values(NEW.id, NEW.item_id, NEW.amount, NEW.unit_price, NEW.total, NEW.description, NEW.ts, 'in');
```
### B
```
postgresql
create trigger out_orders after delete on orders
for each row insert into orders_log(id, item_id, amount, unit_price, total, description, ts, direction)
values(OLD.id, OLD.item_id, OLD.amount, OLD.unit_price, OLD.total, OLD.description, OLD.ts, 'out');
```
### C
```
postgresql
create trigger in_orders after insert on orders
for each row insert into orders_log(id, item_id, amount, unit_price, total, description, ts, direction)
values(NEW.id, NEW.item_id, NEW.amount, NEW.unit_price, NEW.total, NEW.description, NEW.ts, 'in');
after delete on orders
for each row insert into orders_log(id, item_id, amount, unit_price, total, description, ts, direction)
values(OLD.id, OLD.item_id, OLD.amount, OLD.unit_price, OLD.total, OLD.description, OLD.ts, 'out');
```
### D
```
postgresql
create trigger in_orders after insert, after delete on orders
for each row insert into orders_log(id, item_id, amount, unit_price, total, description, ts, direction)
values(NEW.id, NEW.item_id, NEW.amount, NEW.unit_price, NEW.total, NEW.description, NEW.ts, 'in');
```
\ No newline at end of file
data/2.PostgreSQL中阶/1.基本结构/5. 触发器/config.json
浏览文件 @
92a0b9a1
...
@@ -6,7 +6,8 @@
...
@@ -6,7 +6,8 @@
],
],
"children"
:
[],
"children"
:
[],
"export"
:
[
"export"
:
[
"trigger.json"
"trigger.json"
,
"audit.json"
],
],
"keywords_must"
:
[
"keywords_must"
:
[
"trigger"
,
"trigger"
,
...
...
data/tree.json
浏览文件 @
92a0b9a1
...
@@ -29,7 +29,8 @@
...
@@ -29,7 +29,8 @@
"relational"
,
"relational"
,
"rdbms"
"rdbms"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -46,12 +47,14 @@
...
@@ -46,12 +47,14 @@
"服务器"
,
"服务器"
,
"客户端"
"客户端"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
}
}
],
],
"keywords_must"
:
[],
"keywords_must"
:
[],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -70,7 +73,8 @@
...
@@ -70,7 +73,8 @@
"keywords_must"
:
[
"keywords_must"
:
[
"安装"
"安装"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -85,7 +89,8 @@
...
@@ -85,7 +89,8 @@
"keywords_must"
:
[
"keywords_must"
:
[
"登录"
"登录"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -101,12 +106,14 @@
...
@@ -101,12 +106,14 @@
"基本用法"
,
"基本用法"
,
"入门"
"入门"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
}
}
],
],
"keywords_must"
:
[],
"keywords_must"
:
[],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -126,7 +133,8 @@
...
@@ -126,7 +133,8 @@
"keywords_must"
:
[
"keywords_must"
:
[
"psql"
"psql"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -141,7 +149,8 @@
...
@@ -141,7 +149,8 @@
"语法"
,
"语法"
,
"SQL"
"SQL"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -161,17 +170,20 @@
...
@@ -161,17 +170,20 @@
"update"
,
"update"
,
"delete"
"delete"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
}
}
],
],
"keywords_must"
:
[],
"keywords_must"
:
[],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
}
}
],
],
"keywords_must"
:
[],
"keywords_must"
:
[],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -196,7 +208,8 @@
...
@@ -196,7 +208,8 @@
"表"
,
"表"
,
"table"
"table"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -211,7 +224,8 @@
...
@@ -211,7 +224,8 @@
"函数"
,
"函数"
,
"function"
"function"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -236,7 +250,8 @@
...
@@ -236,7 +250,8 @@
"权限"
,
"权限"
,
"privilege"
"privilege"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -255,7 +270,8 @@
...
@@ -255,7 +270,8 @@
"constraints"
,
"constraints"
,
"index"
"index"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -270,7 +286,8 @@
...
@@ -270,7 +286,8 @@
"trigger"
,
"trigger"
,
"触发器"
"触发器"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -286,12 +303,14 @@
...
@@ -286,12 +303,14 @@
"数据库扩展"
,
"数据库扩展"
,
"extension"
"extension"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
}
}
],
],
"keywords_must"
:
[],
"keywords_must"
:
[],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -312,7 +331,8 @@
...
@@ -312,7 +331,8 @@
"分组"
,
"分组"
,
"聚合"
"聚合"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -327,7 +347,8 @@
...
@@ -327,7 +347,8 @@
"join"
,
"join"
,
"连接查询"
"连接查询"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -342,7 +363,8 @@
...
@@ -342,7 +363,8 @@
"子查询"
,
"子查询"
,
"subquery"
"subquery"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -359,7 +381,8 @@
...
@@ -359,7 +381,8 @@
"limit"
,
"limit"
,
"offset"
"offset"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -374,7 +397,8 @@
...
@@ -374,7 +397,8 @@
"keywords_must"
:
[
"keywords_must"
:
[
"cte"
"cte"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -388,7 +412,8 @@
...
@@ -388,7 +412,8 @@
"keywords_must"
:
[
"keywords_must"
:
[
"过程化"
"过程化"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -405,17 +430,20 @@
...
@@ -405,17 +430,20 @@
"授权"
,
"授权"
,
"ddl"
"ddl"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
}
}
],
],
"keywords_must"
:
[],
"keywords_must"
:
[],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
}
}
],
],
"keywords_must"
:
[],
"keywords_must"
:
[],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -441,7 +469,8 @@
...
@@ -441,7 +469,8 @@
"keywords_must"
:
[
"keywords_must"
:
[
"standby"
"standby"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -458,7 +487,8 @@
...
@@ -458,7 +487,8 @@
"流式复制"
,
"流式复制"
,
"流复制"
"流复制"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -473,12 +503,14 @@
...
@@ -473,12 +503,14 @@
"外部数据连接"
,
"外部数据连接"
,
"fdw"
"fdw"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
}
}
],
],
"keywords_must"
:
[],
"keywords_must"
:
[],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -498,7 +530,8 @@
...
@@ -498,7 +530,8 @@
"gis"
,
"gis"
,
"几何"
"几何"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -513,7 +546,8 @@
...
@@ -513,7 +546,8 @@
"json"
,
"json"
,
"jsonb"
"jsonb"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -535,7 +569,8 @@
...
@@ -535,7 +569,8 @@
],
],
""
""
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -550,12 +585,14 @@
...
@@ -550,12 +585,14 @@
"视图"
,
"视图"
,
"view"
"view"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
}
}
],
],
"keywords_must"
:
[],
"keywords_must"
:
[],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -577,7 +614,8 @@
...
@@ -577,7 +614,8 @@
"递归查询"
,
"递归查询"
,
"recursive"
"recursive"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -593,7 +631,8 @@
...
@@ -593,7 +631,8 @@
"function"
,
"function"
,
"window"
"window"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -610,7 +649,8 @@
...
@@ -610,7 +649,8 @@
"交叉透视表"
,
"交叉透视表"
,
"pivot"
"pivot"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -626,7 +666,8 @@
...
@@ -626,7 +666,8 @@
"conflict"
,
"conflict"
,
"冲突"
"冲突"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -641,7 +682,8 @@
...
@@ -641,7 +682,8 @@
"事务"
,
"事务"
,
"transaction"
"transaction"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -660,12 +702,14 @@
...
@@ -660,12 +702,14 @@
"performance"
,
"performance"
,
"优化"
"优化"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
}
}
],
],
"keywords_must"
:
[],
"keywords_must"
:
[],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
},
},
{
{
...
@@ -683,21 +727,25 @@
...
@@ -683,21 +727,25 @@
"keywords_must"
:
[
"keywords_must"
:
[
"数据库问答"
"数据库问答"
],
],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
}
}
],
],
"keywords_must"
:
[],
"keywords_must"
:
[],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
}
}
],
],
"keywords_must"
:
[],
"keywords_must"
:
[],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
}
}
],
],
"keywords_must"
:
[],
"keywords_must"
:
[],
"keywords_forbid"
:
[]
"keywords_forbid"
:
[],
"group"
:
0
}
}
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录