transaction.md 736 字节
Newer Older
M
Mars Liu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
# Transaction

下列选项中,哪个可以正确的保证 channel 表和 posts 表的同步修改?

## 答案

```sql
begin;
    insert into posts(title, content) select ?, ?;
    update channel set posts = posts +1 where channel_id = ?;
commit;
```

## 选项

### 没有 commit

```sql
begin;
    insert into posts(title, content) select ?, ?;
    update channel set posts = posts +1 where channel_id = ?;
end;
```

### 没有创建事务

```sql
insert into posts(title, content) select ?, ?;
update channel set posts = posts +1 where channel_id = ?;
commit;
```

### 多次提交

```sql
begin;
    insert into posts(title, content) select ?, ?;
commit;
    update channel set posts = posts +1 where channel_id = ?;
commit;
```