generated.md 890 字节
Newer Older
M
Mars Liu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 生成列

Joe 需要为 Points 表

```mysql
create table points(
    id int primary key auto_increment,
    x float,
    y float
)
```

增加一个生成列,保存每个点的模(modulus),即 `√(x^2 + y^2)` 。下面哪个操作是正确的?

M
Mars Liu 已提交
15 16
<hr/>

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

F
feilong 已提交
19 20
* `show databases;` 列出所有数据库
* `show tables;` 列出所有表
M
Mars Liu 已提交
21

M
Mars Liu 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
## 答案

```mysql
alter table points add modulus double generated always as (sqrt(x*x + y*y));
```

## 选项

### A

```mysql
create generated modulus on table points as (sqrt(x*x + y*y));
```

### B

```mysql
alter table points add modulus generated always as (sqrt(x*x + y*y));
```

### C

```mysql
alter table points add modulus float generated sqrt(x*x + y*y);
```