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 #552

Merged
merged 3 commits into from
Oct 8, 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,119 @@
# **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> 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 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 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 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)
```
Original file line number Diff line number Diff line change
@@ -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)
```