本文主要介绍如何查看与自增列关联的Sequence信息。

SHOW CREATE TABLE

在DRDS模式数据库中,当表为拆分表或者广播表时,显示自增列Sequence的类型。AUTO模式数据库中不显示自增列的类型,默认为New 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。
示例
  • 示例一

    DRDS模式数据库中,建表时指定AUTO_INCREMENT,但没有指定Sequence类型关键字,则默认关联Group Sequence:

    SHOW CREATE TABLE tab1\G

    返回结果如下:

    *************************** 1. row ***************************
           Table: tab1
    Create Table: CREATE TABLE `tab1` (
        `col1` bigint(20) NOT NULL AUTO_INCREMENT BY GROUP,
        `col2` varchar(16) DEFAULT NULL,
        PRIMARY KEY (`col1`)
    ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 DEFAULT COLLATE = utf8mb4_0900_ai_ci  dbpartition by hash(`col1`)
  • 示例二

    DRDS模式数据库中,建表时为自增列指定了BY TIME,即Time-based Sequence类型:

    SHOW CREATE TABLE tab2;

    返回结果如下:

    *************************** 1. row ***************************
           Table: tab2
    Create Table: CREATE TABLE `tab2` (
        `col1` bigint(20) NOT NULL AUTO_INCREMENT BY TIME,
        `col2` varchar(16) DEFAULT NULL,
        PRIMARY KEY (`col1`)
    ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 DEFAULT COLLATE = utf8mb4_0900_ai_ci  dbpartition by hash(`col1`)
  • 示例三

    AUTO模式数据库中,建表时为自增列默认关联了New Sequence:

    SHOW FULL CREATE TABLE tab3\G

    返回结果如下:

    *************************** 1. row ***************************
           TABLE: tab3
    CREATE TABLE: CREATE PARTITION TABLE `tab3` (
        `col1` int(11) NOT NULL AUTO_INCREMENT,
        `col2` varchar(16) DEFAULT NULL,
        PRIMARY KEY (`col1`)
    ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 DEFAULT COLLATE = utf8mb4_0900_ai_ci
    PARTITION BY KEY(`col1`)
    PARTITIONS 3
    /* tablegroup = `tg222` */

SHOW SEQUENCES

查询与自增列关联的Sequence名称和详细信息,详细语法说明请参见SHOW SEQUENCES

执行以下语句:

SHOW SEQUENCES;

返回结果如下:

+-------------+---------------+-------+------------+------------+------------+--------------+------------+-----------+-------+-------+--------------+
| SCHEMA_NAME | NAME          | VALUE | UNIT_COUNT | UNIT_INDEX | INNER_STEP | INCREMENT_BY | START_WITH | MAX_VALUE | CYCLE | TYPE  | PHY_SEQ_NAME |
+-------------+---------------+-------+------------+------------+------------+--------------+------------+-----------+-------+-------+--------------+
| ddltest     | AUTO_SEQ_tab2 | N/A   | N/A        | N/A        | N/A        | N/A          | N/A        | N/A       | N/A   | TIME  | N/A          |
| ddltest     | seq2          | N/A   | N/A        | N/A        | N/A        | N/A          | N/A        | N/A       | N/A   | TIME  | N/A          |
| ddltest     | AUTO_SEQ_tab1 | 0     | 1          | 0          | 100000     | N/A          | N/A        | N/A       | N/A   | GROUP | N/A          |
| ddltest     | seq1          | 0     | 1          | 0          | 100000     | N/A          | N/A        | N/A       | N/A   | GROUP | N/A          |
+-------------+---------------+-------+------------+------------+------------+--------------+------------+-----------+-------+-------+--------------+