audit.md 4.3 KB
Newer Older
M
trigger  
Mars Liu 已提交
1 2 3 4
# 交易审计

Orders 表

fix bug  
张志晨 已提交
5
```sql
M
trigger  
Mars Liu 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19
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 表

fix bug  
张志晨 已提交
20
```sql
M
trigger  
Mars Liu 已提交
21 22
create table orders_log
(
M
Mars Liu 已提交
23 24
    log_id          int primary key auto_increment,
    id     int,
M
trigger  
Mars Liu 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37
    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()
);
```

那么应该如何做?

M
Mars Liu 已提交
38 39
<hr/>

F
feilong 已提交
40
点击进入[MySQL实战练习环境](https://mydev.csdn.net/product/pod/new?image=cimg-centos7-skilltreemysql&connect=auto&create=auto&utm_source=skill){target="_blank"}。
F
feilong 已提交
41

F
feilong 已提交
42 43
* `show databases;` 列出所有数据库
* `show tables;` 列出所有表
M
Mars Liu 已提交
44

M
trigger  
Mars Liu 已提交
45 46
## 答案

fix bug  
张志晨 已提交
47
```sql
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
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');
M
trigger  
Mars Liu 已提交
86 87 88 89 90 91
```

## 选项

### A

fix bug  
张志晨 已提交
92
```sql
M
trigger  
Mars Liu 已提交
93
create trigger in_orders after insert on orders
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    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');
M
trigger  
Mars Liu 已提交
111 112 113 114 115

```

### B

fix bug  
张志晨 已提交
116
```sql
M
trigger  
Mars Liu 已提交
117 118

create trigger out_orders after delete on orders
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    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');
M
trigger  
Mars Liu 已提交
136 137 138 139 140
```


### C

fix bug  
张志晨 已提交
141
```sql
M
trigger  
Mars Liu 已提交
142
create trigger in_orders after insert on orders
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
        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');

M
trigger  
Mars Liu 已提交
161
    after delete on orders
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
        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');
M
trigger  
Mars Liu 已提交
179 180 181 182
```

### D

fix bug  
张志晨 已提交
183
```sql
M
trigger  
Mars Liu 已提交
184
create trigger in_orders after insert, after delete on orders
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    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');
M
trigger  
Mars Liu 已提交
202
```