create_index.md 706 字节
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 42 43 44
# 创建索引

Joe 想给 Goods 表

```mysql
create table goods(
    id int primary key auto_increment,
    category_id int,
    name varchar(256),
    price decimal(12, 4),
    stock int,
    upper_time timestamp
)
```

的 category_id (类别)字段创建索引,便于根据类别查询商品。应该执行下面哪个语句?

*注意* 一个类别会包含多种商品。

## 答案

```mysql
create index idx_goods_category on goods(category_id);
```

## 选项

### A

```mysql
create unique index idx_goods_category on goods(category_id);
```

### B

```mysql
create index idx_goods_category on goods(category_id);
```

### C

```mysql
alter table goods add unique index (category_id);
```