From e2ee674ec686400a618ec8c08c32344ddc7641f0 Mon Sep 17 00:00:00 2001 From: prinz <281208364@qq.com> Date: Wed, 20 Sep 2023 19:30:31 +0800 Subject: [PATCH 1/2] new docs of alter sequence and replace view --- .../1.1-alter-sequence.md | 84 +++++++++++++++++++ .../1.1-create-replace-view.md | 73 ++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/1.1-alter-sequence.md create mode 100644 docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/1.1-create-replace-view.md diff --git a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/1.1-alter-sequence.md b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/1.1-alter-sequence.md new file mode 100644 index 000000000..8fdbf0cb5 --- /dev/null +++ b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/1.1-alter-sequence.md @@ -0,0 +1,84 @@ +# **ALTER SEQUENCE** + +## **Description** + +`ALTER SEQUENCE` is used to modify an existing sequence. + +## **Syntax** + +``` +> ALTER SEQUENCE [ IF EXISTS ] SEQUENCE_NAME +[ AS data_type ] +[ INCREMENT [ BY ] increment ] +[ MINVALUE minvalue] [ MAXVALUE maxvalue] +[ START [ WITH ] start ] [ [ NO ] CYCLE ] +``` + +### Explanations + +- `[ IF EXISTS ]`: An optional clause that indicates that if the specified sequence does not exist, it will not raise an error. If this clause is used, the system checks if the sequence exists; if it does not, it will ignore the modification request. + +- `SEQUENCE_NAME`: The name of the sequence to be modified. + +- `[ AS data_type ]`: An optional clause that allows you to specify the data type for the sequence. Typically, the data type of a sequence is an integer. + +- `[ INCREMENT [ BY ] increment ]`: This specifies the increment value for the sequence. The increment value of the sequence is the amount to be added to the current value each time it is incremented or decremented. If the increment value is not specified, it is typically set to 1. + +- `[ MINVALUE minvalue ]`: This is the minimum value of the sequence, specifying the minimum value allowed for the sequence. If a minimum value is set, the sequence's current value cannot go below this value. + +- `[ MAXVALUE maxvalue ]`: This is the maximum value of the sequence, specifying the maximum value allowed for the sequence. If a maximum value is specified, the sequence's current value cannot exceed this value. + +- `[ START [ WITH ] start ]`: This is the sequence's starting value, specifying the sequence's initial value. If the starting value is not specified, it is typically set to 1. + +- `[ [ NO ] CYCLE ]`: An optional clause used to specify whether the sequence values should cycle. If `NO CYCLE` is specified, the sequence will stop incrementing or decrementing after reaching the maximum or minimum value. If this clause is not specified, it typically defaults to not cycling. + +## **Examples** + +```sql +-- Create a sequence named alter_seq_01 with an increment of 2, a minimum value of 30, a maximum value of 100, and enable cycling +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) + +-- Remove cycling for sequence 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) + +-- Set the starting value of sequence alter_seq_01 to 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) + +-- Set the increment value of sequence alter_seq_01 to 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) +``` diff --git a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/1.1-create-replace-view.md b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/1.1-create-replace-view.md new file mode 100644 index 000000000..dafde32c0 --- /dev/null +++ b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/1.1-create-replace-view.md @@ -0,0 +1,73 @@ +# **CREATE OR REPLACE VIEW** + +## **Description** + +`CREATE OR REPLACE VIEW` is used to create a new view or, when the view already exists, replace the existing view. This means updating the definition of the view when it already exists without the need to delete the existing view. + +## **Syntax** + +``` +> CREATE OR REPLACE VIEW view_name AS +SELECT column1, column2, ... +FROM table_name +WHERE condition; +``` + +### Explanations + +- `view_name`: The name of the view to be created or replaced. You need to specify a unique name for the view. + +- `AS`: Indicates that the following query statement is the query definition of the view. + +- `SELECT column1, column2, ...`: After the AS keyword, you need to specify the query definition of the view. This SELECT statement can select specific columns from a table using computed fields, expressions, and more. The view will use the result of this query as its data. + +- `FROM table_name`: The `FROM` clause is used to specify the table's name to be queried. Select one or more tables and perform related operations in the view. + +- `WHERE condition`: An optional `WHERE` clause used to filter the query results. + +## **Examples** + +```sql +-- Create a table t1 with two columns, a and b +create table t1 (a int, b int); + +-- Insert three rows of data into table t1 +insert into t1 values (1, 11), (2, 22), (3, 33); + +-- Create a view v1 that includes all data from table t1 +create view v1 as select * from t1; + +-- Query all data from view v1 +mysql> select * from v1; ++------+------+ +| a | b | ++------+------+ +| 1 | 11 | +| 2 | 22 | +| 3 | 33 | ++------+------+ +3 rows in set (0.01 sec) + +-- Query data from view v1 where column a is greater than 1 +mysql> select * from v1 where a > 1; ++------+------+ +| a | b | ++------+------+ +| 2 | 22 | +| 3 | 33 | ++------+------+ +2 rows in set (0.00 sec) + +-- Replace view v1 with a new view that only includes data from table t1 where column a is greater than 1 +create or replace view v1 as select * from t1 where a > 1; + +-- Query view v1 again, now containing data that meets the new condition +mysql> select * from v1; ++------+------+ +| a | b | ++------+------+ +| 2 | 22 | +| 3 | 33 | ++------+------+ +2 rows in set (0.00 sec) +``` From bae829a706fc1dc528b0e5a1f6d56c0e3843e86c Mon Sep 17 00:00:00 2001 From: prinz <43231571+lacrimosaprinz@users.noreply.github.com> Date: Thu, 21 Sep 2023 13:33:23 +0800 Subject: [PATCH 2/2] Update 1.1-alter-sequence.md --- .../1.1-alter-sequence.md | 83 +++++++++++++------ 1 file changed, 59 insertions(+), 24 deletions(-) diff --git a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/1.1-alter-sequence.md b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/1.1-alter-sequence.md index 8fdbf0cb5..a57a44472 100644 --- a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/1.1-alter-sequence.md +++ b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/1.1-alter-sequence.md @@ -38,47 +38,82 @@ -- Create a sequence named alter_seq_01 with an increment of 2, a minimum value of 30, a maximum value of 100, and enable cycling 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 | -+--------------+-----------+-----------+-------------+-----------------+-------+-----------+ +mysql> show sequences; ++--------------+-----------+ +| Names | Data Type | ++--------------+-----------+ +| alter_seq_01 | SMALLINT | ++--------------+-----------+ +1 row in set (0.00 sec) + +mysql> alter sequence alter_seq_01 as bigint; +Query OK, 0 rows affected (0.01 sec) + +mysql> show sequences; ++--------------+-----------+ +| Names | Data Type | ++--------------+-----------+ +| alter_seq_01 | BIGINT | ++--------------+-----------+ 1 row in set (0.00 sec) -- Remove cycling for sequence 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 | -+--------------+-----------+-----------+-------------+-----------------+-------+-----------+ +mysql> select nextval('alter_seq_01'),currval('alter_seq_01'); ++-----------------------+-----------------------+ +| nextval(alter_seq_01) | currval(alter_seq_01) | ++-----------------------+-----------------------+ +| 30 | 30 | ++-----------------------+-----------------------+ +1 row in set (0.01 sec) + +mysql> select nextval('alter_seq_01'),currval('alter_seq_01'); ++-----------------------+-----------------------+ +| nextval(alter_seq_01) | currval(alter_seq_01) | ++-----------------------+-----------------------+ +| 32 | 32 | ++-----------------------+-----------------------+ 1 row in set (0.00 sec) -- Set the starting value of sequence alter_seq_01 to 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 | -+--------------+-----------+-----------+-------------+-----------------+-------+-----------+ +mysql> select nextval('alter_seq_01'),currval('alter_seq_01'); ++-----------------------+-----------------------+ +| nextval(alter_seq_01) | currval(alter_seq_01) | ++-----------------------+-----------------------+ +| 40 | 40 | ++-----------------------+-----------------------+ 1 row in set (0.01 sec) +mysql> select nextval('alter_seq_01'),currval('alter_seq_01'); ++-----------------------+-----------------------+ +| nextval(alter_seq_01) | currval(alter_seq_01) | ++-----------------------+-----------------------+ +| 42 | 42 | ++-----------------------+-----------------------+ +1 row in set (0.00 sec) + -- Set the increment value of sequence alter_seq_01 to 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 | -+--------------+-----------+-----------+-------------+-----------------+-------+-----------+ +mysql> select nextval('alter_seq_01'),currval('alter_seq_01'); ++-----------------------+-----------------------+ +| nextval(alter_seq_01) | currval(alter_seq_01) | ++-----------------------+-----------------------+ +| 40 | 40 | ++-----------------------+-----------------------+ +1 row in set (0.00 sec) + +mysql> select nextval('alter_seq_01'),currval('alter_seq_01'); ++-----------------------+-----------------------+ +| nextval(alter_seq_01) | currval(alter_seq_01) | ++-----------------------+-----------------------+ +| 43 | 43 | ++-----------------------+-----------------------+ 1 row in set (0.00 sec) ```