提交 608cc553 编写于 作者: M Mars Liu

trigger

上级 d2fc511a
{
"type": "code_options",
"author": "ccat",
"source": "audit.md",
"notebook_enable": false,
"exercise_id": "457f6b7e9b2b4eb58d15f12cb21a1463"
}
\ No newline at end of file
# 交易审计
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
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.
先完成此消息的编辑!
想要评论请 注册