Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 45 additions & 9 deletions docs/reference/sql/alter.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,47 @@
# ALTER

`ALTER` can be used to modify any table settings or data within the table:
`ALTER` can be used to modify any database options, table options or metadata of the table, including:

* Modify database options
* Add/Drop/Modify a column
* Rename a table
* Modify table options

## Syntax
## ALTER DATABASE

Comment thread
nicecui marked this conversation as resolved.
`ALTER DATABASE` statements can be used to modify the options of databases.

### Syntax

```sql
ALTER DATABASE db
[SET <option_name>=<option_value> [, ...]
| UNSET <option_name> [, ...]
]
```

Comment thread
CookiePieWw marked this conversation as resolved.
Currently following options are supported:
- `ttl`: the default retention time of data in database.

### Examples

#### Modify default retention time of data in database

Change the default retention time of data in the database to 1 day:

```sql
ALTER DATABASE db SET 'ttl'='1d';
```

Remove the default retention time of data in the database:

```sql
ALTER DATABASE db UNSET 'ttl';
```

## ALTER TABLE

### Syntax

```sql
ALTER TABLE [db.]table
Expand All @@ -20,9 +56,9 @@ ALTER TABLE [db.]table
]
```

## Examples
### Examples

### Add column
#### Add column

Adds a new column to the table:

Expand All @@ -49,7 +85,7 @@ Adds a new column as a tag(primary key) with a default value:
ALTER TABLE monitor ADD COLUMN app STRING DEFAULT 'shop' PRIMARY KEY;
```

### Remove column
#### Remove column

Removes a column from the table:

Expand All @@ -59,7 +95,7 @@ ALTER TABLE monitor DROP COLUMN load_15;

The removed column can't be retrieved immediately by all subsequent queries.

### Modify column type
#### Modify column type

Modify the date type of a column

Expand All @@ -69,7 +105,7 @@ ALTER TABLE monitor MODIFY COLUMN load_15 STRING;

The modified column cannot be a tag (primary key) or time index, and it must be nullable to ensure that the data can be safely converted (returns `NULL` on cast failures).

### Alter table options
#### Alter table options

`ALTER TABLE` statements can also be used to change the options of tables.

Expand Down Expand Up @@ -104,7 +140,7 @@ ALTER TABLE monitor SET 'compaction.twcs.max_inactive_window_runs'='6';
ALTER TABLE monitor UNSET 'ttl';
```

### Modify column fulltext index options
#### Modify column fulltext index options

Enable fulltext index on a column:

Expand All @@ -129,7 +165,7 @@ The column must be a string type to alter the fulltext index.

If the fulltext index has never been enabled, you can enable it and specify the `analyzer` and `case_sensitive` options. When the fulltext index is already enabled on a column, you can disable it but **cannot modify the options**.

### Rename table
#### Rename table

Renames the table:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
# ALTER

`ALTER` 可以用来修改表的设置或者表中的数据
`ALTER` 可以用来修改数据库的设置,表的设置或表的元数据,包括

* 修改数据库选项
* 添加/删除/修改列
* 重命名表
* 修改表选项

## Syntax
## ALTER DATABASE


`ALTER DATABASE` 语句可以用来修改数据库的选项。

### 语法

```sql
ALTER TABLE [db.]table
Expand All @@ -19,9 +26,43 @@ ALTER TABLE [db.]table
]
```

## 示例
当前支持修改以下数据库选项:
- `ttl`: 数据库中数据的默认保留时间。

### 示例

#### 修改数据库中数据的默认保留时间

修改数据库中数据的默认保留时间为 1 天:

```sql
ALTER DATABASE db SET 'ttl'='1d';
```

取消数据库中数据的默认保留时间设置:

```sql
ALTER DATABASE db UNSET 'ttl';
```

## ALTER TABLE

## 语法

```sql
ALTER TABLE [db.]table
[ADD COLUMN name type [options]
| DROP COLUMN name
| MODIFY COLUMN name type
| MODIFY COLUMN name SET FULLTEXT [WITH <options>]
| RENAME name
| SET <option_name>=<option_value> [, ...]
]
```

### 示例

### 增加列
#### 增加列

在表中增加新列:

Expand Down Expand Up @@ -49,7 +90,7 @@ ALTER TABLE monitor ADD COLUMN app STRING DEFAULT 'shop' PRIMARY KEY;
```


### 移除列
#### 移除列

从表中移除列:

Expand All @@ -59,7 +100,7 @@ ALTER TABLE monitor DROP COLUMN load_15;

后续的所有查询立刻不能获取到被移除的列。

### 修改列类型
#### 修改列类型

修改列的数据类型

Expand All @@ -69,7 +110,7 @@ ALTER TABLE monitor MODIFY COLUMN load_15 STRING;

被修改的的列不能是 tag 列(primary key)或 time index 列,同时该列必须允许空值 `NULL` 存在来保证数据能够安全地进行转换(转换失败时返回 `NULL`)。

### 修改表的参数
#### 修改表的参数

`ALTER TABLE` 语句也可以用来更改表的选项。
当前支持修改以下表选项:
Expand Down Expand Up @@ -104,8 +145,7 @@ ALTER TABLE monitor SET 'compaction.twcs.max_inactive_window_runs'='6';
ALTER TABLE monitor UNSET 'ttl';
```


### 修改列全文索引选项
#### 修改列全文索引选项

启用列的全文索引:

Expand All @@ -130,7 +170,7 @@ ALTER TABLE monitor MODIFY COLUMN load_15 UNSET FULLTEXT;

当列的全文索引未开启过时,可以启用全文索引,并设置 `analyzer` 和 `case_sensitive` 选项;当列的全文索引选项已经启用时,可以关闭全文索引,**但不能修改选项**。

### 重命名表
#### 重命名表

```sql
ALTER TABLE monitor RENAME monitor_new;
Expand Down