Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_mysql
提交
608cc553
S
skill_tree_mysql
项目概览
CSDN 技术社区
/
skill_tree_mysql
通知
21
Star
0
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
skill_tree_mysql
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
608cc553
编写于
5月 27, 2022
作者:
M
Mars Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
trigger
上级
d2fc511a
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
117 addition
and
0 deletion
+117
-0
data/2.MySQL中阶/1.数据库组成/6. 触发器/audit.json
data/2.MySQL中阶/1.数据库组成/6. 触发器/audit.json
+8
-0
data/2.MySQL中阶/1.数据库组成/6. 触发器/audit.md
data/2.MySQL中阶/1.数据库组成/6. 触发器/audit.md
+88
-0
gears/sql/trigger.sql
gears/sql/trigger.sql
+21
-0
未找到文件。
data/2.MySQL中阶/1.数据库组成/6. 触发器/audit.json
0 → 100644
浏览文件 @
608cc553
{
"type"
:
"code_options"
,
"author"
:
"ccat"
,
"source"
:
"audit.md"
,
"notebook_enable"
:
false
,
"exercise_id"
:
"457f6b7e9b2b4eb58d15f12cb21a1463"
}
\ No newline at end of file
data/2.MySQL中阶/1.数据库组成/6. 触发器/audit.md
0 → 100644
浏览文件 @
608cc553
# 交易审计
Orders 表
```
mysql
create table orders
(
id int primary key auto_increment,
item_id int,
amount int,
unit_price decimal(12, 4),
total decimal(12, 4),
description varchar(2000),
ts timestamp default now()
);
```
记录了未完成的订单。审计部门现在需要记录其变更——新增或删除,该表不会发生update——即将修改都记录到 orders_log 表
```
mysql
create table orders_log
(
id int primary key auto_increment,
item_id int,
amount int,
unit_price decimal(12, 4),
total decimal(12, 4),
description varchar(2000),
ts timestamp,
direction varchar(16),
log_at timestamp default now()
);
```
那么应该如何做?
## 答案
```
mysql
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');
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');
```
## 选项
### A
```
mysql
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
```
mysql
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
```
mysql
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
```
mysql
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
gears/sql/trigger.sql
0 → 100644
浏览文件 @
608cc553
create
table
orders_log
(
event_id
int
primary
key
auto_increment
,
id
int
,
item_id
int
,
amount
int
,
unit_price
decimal
(
12
,
4
),
total
decimal
(
12
,
4
),
description
varchar
(
2000
),
ts
timestamp
,
direction
varchar
(
16
),
log_at
timestamp
default
now
()
);
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'
);
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'
);
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录