本文介紹了Sequence的隱式用法。
創(chuàng)建Sequence
在為拆分表或廣播表的主鍵定義AUTO_INCREMENT后,Sequence可以用于自動填充主鍵,由PolarDB-X 1.0自動維護(hù)。
擴(kuò)展標(biāo)準(zhǔn)建表語法,增加了自增列的Sequence類型,如果未指定類型關(guān)鍵字,則默認(rèn)類型為GROUP。PolarDB-X 1.0自動創(chuàng)建的、跟表相關(guān)聯(lián)的Sequence名稱,都是以AUTO_SEQ_
為前綴,后面加上表名。
語法
創(chuàng)建單元化Group SequenceCREATE TABLE <name> (
<column> ... AUTO_INCREMENT [ BY GROUP | SIMPLE | TIME ],
<column definition>,
...
) ... AUTO_INCREMENT=<start value>
說明 如果指定了
BY TIME
,即Time-based Sequence,則該列類型必須為BIGINT。
語法
CREATE TABLE <name> (
<column> ... AUTO_INCREMENT [ BY GROUP ] [ UNIT COUNT <numeric value> INDEX <numeric value> ],
<column definition>,
...
) ... AUTO_INCREMENT=<start value>
示例- 示例一:默認(rèn)創(chuàng)建一張使用Group Sequence作為自增列的表。
mysql> CREATE TABLE tab1 ( col1 BIGINT NOT NULL AUTO_INCREMENT, col2 VARCHAR(16), PRIMARY KEY(col1) ) DBPARTITION BY HASH(col1);
- 示例二:創(chuàng)建3張同名的、使用相同單元數(shù)量和不同單元索引的單元化Group Sequence作為自增列的表,分別用于3個不同的實(shí)例或庫。
- 實(shí)例1/庫1
請?jiān)诿钚休斎肴缦麓a:
mysql> CREATE TABLE tab2 ( col1 BIGINT NOT NULL AUTO_INCREMENT UNIT COUNT 3 INDEX 0, col2 VARCHAR(16), PRIMARY KEY(col1) ) DBPARTITION BY HASH(col1);
- 實(shí)例2/庫2
請?jiān)诿钚休斎肴缦麓a:
mysql> CREATE TABLE tab2 ( col1 BIGINT NOT NULL AUTO_INCREMENT UNIT COUNT 3 INDEX 1, col2 VARCHAR(16), PRIMARY KEY(col1) ) DBPARTITION BY HASH(col1);
- 實(shí)例3/庫3
請?jiān)诿钚休斎肴缦麓a:
mysql> CREATE TABLE tab2 ( col1 BIGINT NOT NULL AUTO_INCREMENT UNIT COUNT 3 INDEX 2, col2 VARCHAR(16), PRIMARY KEY(col1) ) DBPARTITION BY HASH(col1);
- 實(shí)例1/庫1
- 示例三:創(chuàng)建一張使用Time-based Sequence作為自增列的表。
mysql> CREATE TABLE tab3 ( col1 BIGINT NOT NULL AUTO_INCREMENT BY TIME, col2 VARCHAR(16), PRIMARY KEY(col1) ) DBPARTITION BY HASH(col1);
- 示例四:創(chuàng)建一張使用Simple Sequence作為自增列的表。
mysql> CREATE TABLE tab4 ( col1 BIGINT NOT NULL AUTO_INCREMENT BY SIMPLE, col2 VARCHAR(16), PRIMARY KEY(col1) ) DBPARTITION BY HASH(col1);
修改Sequence
暫不支持通過ALTER TABLE
來修改對應(yīng)Sequence的類型,但您可以參見如下語法通過ALTER TABLE
修改起始值:
ALTER TABLE <name> ... AUTO_INCREMENT=<start value>
說明
- 如果想要修改表相關(guān)的Sequence類型,需要通過
SHOW SEQUENCES
指令查找出Sequence的具體名稱和類型,然后再用ALTER SEQUENCE
指令去修改。 - 使用Sequence后,請謹(jǐn)慎修改
AUTO_INCREMENT
的起始值(仔細(xì)評估已經(jīng)產(chǎn)生的Sequence值,以及生成新Sequence值的速度,防止產(chǎn)生沖突)。
查看表信息及相關(guān)Sequence類型
SHOW CREATE TABLE當(dāng)表為拆分表或者廣播表時,顯示自增列Sequence的類型。
查看已創(chuàng)建的表語法如下:
SHOW CREATE TABLE <name>
說明
SHOW CREATE TABLE
僅顯示相關(guān)Sequence的類型,并不顯示Sequence詳細(xì)信息,如需查看,請使用SHOW SEQUENCES
命令。- 關(guān)聯(lián)了單元化Group Sequence的表并不顯示單元數(shù)量和單元索引,因此不能將
SHOW CREATE TABLE
顯示的DDL直接用于創(chuàng)建具備同樣單元化Group Sequence能力的表。 - 如果需要創(chuàng)建具備同樣單元化能力的表,必須使用
SHOW SEQUENCES
查看單元數(shù)量和單元索引,然后參照CREATE TABLE
的語法修改通過SHOW CREATE TABLE
獲取的建表DDL。
- 示例一:建表時指定
AUTO_INCREMENT
,但沒有指定Sequence類型關(guān)鍵字,則默認(rèn)使用Group Sequence。mysql> SHOW CREATE TABLE tab1;
返回結(jié)果如下:
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | tab1 | CREATE TABLE `tab1` ( `col1` bigint(20) NOT NULL AUTO_INCREMENT BY GROUP, `col2` varchar(16) DEFAULT NULL, PRIMARY KEY (`col1`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 dbpartition by hash(`col1`) | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.02 sec)
- 示例二:建表時為
AUTO_INCREMENT
指定了單元數(shù)量和單元索引,使用單元化 Group Sequence,但SHOW CREATE TABLE
時并不顯示單元數(shù)量和單元索引,不能將此DDL用于創(chuàng)建具備同樣單元化 Group Sequence能力的表。mysql> SHOW CREATE TABLE tab2;
返回結(jié)果如下:
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | tab2 | CREATE TABLE `tab2` ( `col1` bigint(20) NOT NULL AUTO_INCREMENT BY GROUP, `col2` varchar(16) DEFAULT NULL, PRIMARY KEY (`col1`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 dbpartition by hash(`col1`) | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.01 sec)
- 示例三:建表時為
AUTO_INCREMENT
指定了BY TIME
,即Time-based Sequence類型。mysql> SHOW CREATE TABLE tab3;
返回結(jié)果如下:
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | tab3 | CREATE TABLE `tab3` ( `col1` bigint(20) NOT NULL AUTO_INCREMENT BY TIME, `col2` varchar(16) DEFAULT NULL, PRIMARY KEY (`col1`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 dbpartition by hash(`col1`) | +-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.01 sec)
- 示例四:建表時為
AUTO_INCREMENT
指定了BY SIMPLE
,即Simple Sequence類型。mysql> SHOW CREATE TABLE tab4;
返回結(jié)果如下:
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | tab3 | CREATE TABLE `tab4` ( `col1` bigint(20) NOT NULL AUTO_INCREMENT BY TIME, `col2` varchar(16) DEFAULT NULL, PRIMARY KEY (`col1`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 dbpartition by hash(`col1`) | +-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.01 sec)
建表后相關(guān)的Sequence名稱和詳細(xì)信息,可通過SHOW SEQUENCES
查看。
mysql> SHOW SEQUENCES;
返回結(jié)果如下:
+---------------+--------+------------+------------+------------+--------------+------------+---------------------+-------+--------+
| NAME | VALUE | UNIT_COUNT | UNIT_INDEX | INNER_STEP | INCREMENT_BY | START_WITH | MAX_VALUE | CYCLE | TYPE |
+---------------+--------+------------+------------+------------+--------------+------------+---------------------+-------+--------+
| seq1 | 100000 | 1 | 0 | 100000 | N/A | N/A | N/A | N/A | GROUP |
| seq2 | 400000 | 3 | 1 | 100000 | N/A | N/A | N/A | N/A | GROUP |
| seq3 | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | TIME |
| seq4 | 1006 | N/A | N/A | N/A | 2 | 1000 | 99999999999 | N | SIMPLE |
| AUTO_SEQ_tab1 | 100000 | 1 | 0 | 100000 | N/A | N/A | N/A | N/A | GROUP |
| AUTO_SEQ_tab2 | 400000 | 3 | 1 | 100000 | N/A | N/A | N/A | N/A | GROUP |
| AUTO_SEQ_tab3 | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | TIME |
| AUTO_SEQ_tab4 | 2 | N/A | N/A | N/A | 1 | 1 | 9223372036854775807 | N | SIMPLE |
+---------------+--------+------------+------------+------------+--------------+------------+---------------------+-------+--------+
8 rows in set (0.01 sec)