Skip to content

Commit

Permalink
Update 1.1-alter-sequence.md
Browse files Browse the repository at this point in the history
  • Loading branch information
lacrimosaprinz authored Sep 21, 2023
1 parent e2ee674 commit bae829a
Showing 1 changed file with 59 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

0 comments on commit bae829a

Please sign in to comment.