本文介绍了Sequence的隐式用法。
创建Sequence
在为拆分表或广播表的主键定义AUTO_INCREMENT后,Sequence可以用于自动填充主键,由PolarDB-X 1.0自动维护。
扩展标准建表语法,增加了自增列的Sequence类型,如果未指定类型关键字,则默认类型为GROUP。PolarDB-X 1.0自动创建的、跟表相关联的Sequence名称,都是以AUTO_SEQ_
为前缀,后面加上表名。
语法
创建单元化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>
示例- 示例一:默认创建一张使用Group Sequence作为自增列的表。
mysql> CREATE TABLE tab1 ( col1 BIGINT NOT NULL AUTO_INCREMENT, col2 VARCHAR(16), PRIMARY KEY(col1) ) DBPARTITION BY HASH(col1);
- 示例二:创建3张同名的、使用相同单元数量和不同单元索引的单元化Group Sequence作为自增列的表,分别用于3个不同的实例或库。
- 实例1/库1
请在命令行输入如下代码:
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);
- 实例2/库2
请在命令行输入如下代码:
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);
- 实例3/库3
请在命令行输入如下代码:
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);
- 实例1/库1
- 示例三:创建一张使用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);
- 示例四:创建一张使用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
来修改对应Sequence的类型,但您可以参见如下语法通过ALTER TABLE
修改起始值:
ALTER TABLE <name> ... AUTO_INCREMENT=<start value>
说明
- 如果想要修改表相关的Sequence类型,需要通过
SHOW SEQUENCES
指令查找出Sequence的具体名称和类型,然后再用ALTER SEQUENCE
指令去修改。 - 使用Sequence后,请谨慎修改
AUTO_INCREMENT
的起始值(仔细评估已经产生的Sequence值,以及生成新Sequence值的速度,防止产生冲突)。
查看表信息及相关Sequence类型
SHOW CREATE TABLE当表为拆分表或者广播表时,显示自增列Sequence的类型。
查看已创建的表语法如下:
SHOW CREATE TABLE <name>
说明
SHOW CREATE TABLE
仅显示相关Sequence的类型,并不显示Sequence详细信息,如需查看,请使用SHOW SEQUENCES
命令。- 关联了单元化Group Sequence的表并不显示单元数量和单元索引,因此不能将
SHOW CREATE TABLE
显示的DDL直接用于创建具备同样单元化Group Sequence能力的表。 - 如果需要创建具备同样单元化能力的表,必须使用
SHOW SEQUENCES
查看单元数量和单元索引,然后参照CREATE TABLE
的语法修改通过SHOW CREATE TABLE
获取的建表DDL。
- 示例一:建表时指定
AUTO_INCREMENT
,但没有指定Sequence类型关键字,则默认使用Group Sequence。mysql> SHOW CREATE TABLE tab1;
返回结果如下:
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | 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
指定了单元数量和单元索引,使用单元化 Group Sequence,但SHOW CREATE TABLE
时并不显示单元数量和单元索引,不能将此DDL用于创建具备同样单元化 Group Sequence能力的表。mysql> SHOW CREATE TABLE tab2;
返回结果如下:
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | 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;
返回结果如下:
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | 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;
返回结果如下:
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | 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)
建表后相关的Sequence名称和详细信息,可通过SHOW SEQUENCES
查看。
mysql> SHOW SEQUENCES;
返回结果如下:
+---------------+--------+------------+------------+------------+--------------+------------+---------------------+-------+--------+
| 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)