delete.md 1.6 KB
Newer Older
M
Mars Liu 已提交
1 2 3 4
# 删除

现在 orders 表结构如下:

fix bug  
张志晨 已提交
5
```sql
M
Mars Liu 已提交
6
create table orders (
7 8 9 10 11 12 13 14
    id int primary key auto_increment,
    item_id int,
    amount int,
    unit_price decimal(12, 4),
    price decimal(12, 4),
    description varchar(2000),
    ts timestamp default now(),
    deal bool default false
M
Mars Liu 已提交
15 16 17
);
```

M
Mars Liu 已提交
18
有一个业务系统会实时的将已经成交(deal 字段为 true)的订单数据转储,现在我们仅需要一个清理 程序,将已经成 交的数据从 orders 表删除。下面哪个操作是对的?
M
Mars Liu 已提交
19

M
Mars Liu 已提交
20 21
<hr/>

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

F
feilong 已提交
24 25
* `show databases;` 列出所有数据库
* `show tables;` 列出所有表
M
Mars Liu 已提交
26

M
Mars Liu 已提交
27 28 29 30
## 答案

在一个独立的定时任务中执行

fix bug  
张志晨 已提交
31
```sql
M
Mars Liu 已提交
32 33 34 35 36 37 38 39 40 41 42
delete
from orders
where deal;
```

## 选项

### A

在一个独立的定时任务中执行

fix bug  
张志晨 已提交
43
```sql
M
Mars Liu 已提交
44 45 46 47 48 49 50
truncate orders;
```

### B

在一个独立的定时任务中执行

fix bug  
张志晨 已提交
51
```sql
M
Mars Liu 已提交
52 53 54 55 56 57 58 59
delete
from orders;
```

### C

在一个独立的定时任务中执行

fix bug  
张志晨 已提交
60
```sql
M
Mars Liu 已提交
61 62
drop table orders;
create table if not exists orders (
63 64 65 66 67 68 69 70
    id int primary key auto_increment,
    item_id int,
    amount int,
    unit_price decimal(12, 4),
    price decimal(12, 4),
    description varchar(2000),
    ts timestamp default now(),
    deal bool default false
M
Mars Liu 已提交
71 72 73 74 75 76 77
);
```

### D

建立视图

fix bug  
张志晨 已提交
78
```sql
M
Mars Liu 已提交
79 80 81 82 83 84 85 86 87 88 89 90
create view order_view as
select id, meta, content, created_at
from orders
where not deal;
```

并要求业务系统只能访问这个视图。

### E

在一个独立的定时任务中执行

fix bug  
张志晨 已提交
91
```sql
M
Mars Liu 已提交
92 93 94 95 96
delete
from orders
where deal;
```

M
Mars Liu 已提交
97
并记录操作前后表中的最大 id