Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new docs of alter sequence and replace view #768

Merged
merged 3 commits into from
Sep 21, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# **ALTER SEQUENCE**

## **语法说明**

`ALTER SEQUENCE` 用于修改现有序列。

## **语法结构**

```
> ALTER SEQUENCE [ IF EXISTS ] SEQUENCE_NAME
[ AS data_type ]
[ INCREMENT [ BY ] increment ]
[ MINVALUE minvalue] [ MAXVALUE maxvalue]
[ START [ WITH ] start ] [ [ NO ] CYCLE ]
```

### 语法释义

- `[ IF EXISTS ]`:可选的子句,表示如果指定的序列不存在,也不会引发错误。如果使用了此子句,系统将检查序列是否存在,如果不存在,将忽略修改请求。

- `SEQUENCE_NAME`:要修改的序列的名称。

- `[ AS data_type ]`:可选子句,它允许您为序列指定数据类型。通常,序列的数据类型是整数。

- `[ INCREMENT [ BY ] increment ]`:这是指定序列的增量值。序列的增量值是在每次递增或递减时要添加到当前值的数量。如果未指定增量值,通常默认为 1。

- `[ MINVALUE minvalue ]`:这是序列的最小值,它指定了序列允许的最小值。如果指定了最小值,序列的当前值不能低于此值。

- `[ MAXVALUE maxvalue ]`:这是序列的最大值,它指定了序列允许的最大值。如果指定了最大值,序列的当前值不能超过此值。

- `[ START [ WITH ] start ]`:这是序列的起始值,它指定序列的初始值。如果未指定起始值,通常默认为 1。

- `[ [ NO ] CYCLE ]`:可选子句,用于指定是否循环使用序列值。如果指定了 `NO CYCLE`,则在达到最大值或最小值后,序列将停止递增或递减。如果未指定此子句,通常默认为不循环。

## **示例**

```sql
-- 创建一个名为 alter_seq_01 的序列,将序列的增量设置为 2,设置序列的最小值为 30,最大值为 100,并启用循环
create sequence alter_seq_01 as smallint increment by 2 minvalue 30 maxvalue 100 cycle;

mysql> select * from alter_seq_01;
+--------------+-----------+-----------+-------------+-----------------+-------+-----------+
| last_seq_num | min_value | max_value | start_value | increment_value | cycle | is_called |
+--------------+-----------+-----------+-------------+-----------------+-------+-----------+
| 30 | 30 | 100 | 30 | 2 | true | false |
+--------------+-----------+-----------+-------------+-----------------+-------+-----------+
1 row in set (0.00 sec)

-- 取消序列 alter_seq_01 的循环
mysql> alter sequence alter_seq_01 no cycle;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from alter_seq_01;
+--------------+-----------+-----------+-------------+-----------------+-------+-----------+
| last_seq_num | min_value | max_value | start_value | increment_value | cycle | is_called |
+--------------+-----------+-----------+-------------+-----------------+-------+-----------+
| 30 | 30 | 100 | 30 | 2 | false | false |
+--------------+-----------+-----------+-------------+-----------------+-------+-----------+
1 row in set (0.00 sec)

-- 将序列 alter_seq_01 的起始值设置为 40
mysql> alter sequence alter_seq_01 start with 40;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from alter_seq_01;
+--------------+-----------+-----------+-------------+-----------------+-------+-----------+
| last_seq_num | min_value | max_value | start_value | increment_value | cycle | is_called |
+--------------+-----------+-----------+-------------+-----------------+-------+-----------+
| 40 | 30 | 100 | 40 | 3 | false | false |
+--------------+-----------+-----------+-------------+-----------------+-------+-----------+
1 row in set (0.01 sec)

-- 将序列 alter_seq_01 的增量值设置为 3
mysql> alter sequence alter_seq_01 increment by 3;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from alter_seq_01;
+--------------+-----------+-----------+-------------+-----------------+-------+-----------+
| last_seq_num | min_value | max_value | start_value | increment_value | cycle | is_called |
+--------------+-----------+-----------+-------------+-----------------+-------+-----------+
| 40 | 30 | 100 | 40 | 3 | false | false |
+--------------+-----------+-----------+-------------+-----------------+-------+-----------+
1 row in set (0.00 sec)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# **CREATE OR REPLACE VIEW**

## **语法说明**

`CREATE OR REPLACE VIEW` 用于创建一个新的视图,也可以用作当视图已经存在时,则替换已有的视图。这表示在视图已经存在时更新视图的定义,而不需要删除已有的视图。

## **语法结构**

```
> CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
```

### 语法释义

- `view_name`:要创建或替换的视图的名称,需要为视图指定一个唯一的名称。

- `AS`:用于指示以下的查询语句是视图的查询定义。

- `SELECT column1, column2, ...`:在 AS 关键字之后,你需要指定视图的查询定义。这是一个 SELECT 语句,可以选择表中的特定列,也可以使用计算字段、表达式等等。视图会将这个查询的结果作为其自身的数据。

- `FROM table_name`:`FROM` 子句,用于指定要查询的表名。你可以选择一个或多个表,并在视图中进行相关操作。

- `WHERE condition`:可选的 `WHERE` 子句,用于筛选查询结果。

## **示例**

```sql
-- 创建表 t1 包括两列 a 和 b
create table t1 (a int, b int);

-- 向表 t1 插入三行数据
insert into t1 values (1, 11), (2, 22), (3, 33);

-- 创建视图 v1,该视图包含表 t1 中的所有数据
create view v1 as select * from t1;

-- 查询视图 v1 的所有数据
mysql> select * from v1;
+------+------+
| a | b |
+------+------+
| 1 | 11 |
| 2 | 22 |
| 3 | 33 |
+------+------+
3 rows in set (0.01 sec)

-- 查询视图 v1 中列 a 大于 1 的数据
mysql> select * from v1 where a > 1;
+------+------+
| a | b |
+------+------+
| 2 | 22 |
| 3 | 33 |
+------+------+
2 rows in set (0.00 sec)

-- 替换视图 v1,新的视图仅包含表 t1 中列 a 大于 1 的数据
create or replace view v1 as select * from t1 where a > 1;

-- 再次查询视图 v1,现在只包含满足新条件的数据
mysql> select * from v1;
+------+------+
| a | b |
+------+------+
| 2 | 22 |
| 3 | 33 |
+------+------+
2 rows in set (0.00 sec)
```