提交 92a0b9a1 编写于 作者: M Mars Liu

add audit

上级 bfa27941
{
"type": "code_options",
"author": "ccat",
"source": "audit.md",
"notebook_enable": false,
"exercise_id": "47ab9b38b16d48ca9a4abd637dfe39b3"
}
\ No newline at end of file
# 交易审计
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
......@@ -6,7 +6,8 @@
],
"children": [],
"export": [
"trigger.json"
"trigger.json",
"audit.json"
],
"keywords_must": [
"trigger",
......
......@@ -29,7 +29,8 @@
"relational",
"rdbms"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -46,12 +47,14 @@
"服务器",
"客户端"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
}
],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -70,7 +73,8 @@
"keywords_must": [
"安装"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -85,7 +89,8 @@
"keywords_must": [
"登录"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -101,12 +106,14 @@
"基本用法",
"入门"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
}
],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -126,7 +133,8 @@
"keywords_must": [
"psql"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -141,7 +149,8 @@
"语法",
"SQL"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -161,17 +170,20 @@
"update",
"delete"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
}
],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
}
],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -196,7 +208,8 @@
"表",
"table"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -211,7 +224,8 @@
"函数",
"function"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -236,7 +250,8 @@
"权限",
"privilege"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -255,7 +270,8 @@
"constraints",
"index"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -270,7 +286,8 @@
"trigger",
"触发器"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -286,12 +303,14 @@
"数据库扩展",
"extension"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
}
],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -312,7 +331,8 @@
"分组",
"聚合"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -327,7 +347,8 @@
"join",
"连接查询"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -342,7 +363,8 @@
"子查询",
"subquery"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -359,7 +381,8 @@
"limit",
"offset"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -374,7 +397,8 @@
"keywords_must": [
"cte"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -388,7 +412,8 @@
"keywords_must": [
"过程化"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -405,17 +430,20 @@
"授权",
"ddl"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
}
],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
}
],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -441,7 +469,8 @@
"keywords_must": [
"standby"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -458,7 +487,8 @@
"流式复制",
"流复制"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -473,12 +503,14 @@
"外部数据连接",
"fdw"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
}
],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -498,7 +530,8 @@
"gis",
"几何"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -513,7 +546,8 @@
"json",
"jsonb"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -535,7 +569,8 @@
],
""
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -550,12 +585,14 @@
"视图",
"view"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
}
],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -577,7 +614,8 @@
"递归查询",
"recursive"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -593,7 +631,8 @@
"function",
"window"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -610,7 +649,8 @@
"交叉透视表",
"pivot"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -626,7 +666,8 @@
"conflict",
"冲突"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -641,7 +682,8 @@
"事务",
"transaction"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -660,12 +702,14 @@
"performance",
"优化"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
}
],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
},
{
......@@ -683,21 +727,25 @@
"keywords_must": [
"数据库问答"
],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
}
],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
}
],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0
}
}
],
"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.
先完成此消息的编辑!
想要评论请 注册