更新时间:2024-06-29 GMT+08:00

AUTO_INCREMENT

在数据库应用中,我们经常需要用到自动递增的唯一编号来标识记录。在MySQL中,可通过数据列的auto_increment属性来自动生成。可在建表时可用“auto_increment=n”选项来指定一个自增的初始值。可用“alter table table_name auto_increment=n”命令来重设自增的起始值。GaussDB(DWS)不支持该参数,DSC迁移时会将设置该属性的字段迁移为SERIAL类型,并删除该关键字,转换如下

表1 数据类型转换

MySQL数字类型

MySQL INPUT

GaussDB(DWS) OUTPUT

TINYINT

TINYINT

SMALLSERIAL

SMALLINT

SMALLINT UNSIGNED

SMALLINT

SERIAL

SMALLSERIAL

DOUBLE/FLOAT

DOUBLE/FLOAT

BIGSERIAL

INT/INTEGER

INT/INTEGER UNSIGNED

INT/INTEGER

BIGSERIAL

SERIAL

BIGINT/SERIAL

BIGINT/SERIAL

BIGSERIAL

输入示例

1
2
3
4
5
CREATE TABLE `public`.`job_instance` (
  `job_sche_id` int(11) NOT NULL AUTO_INCREMENT,
  `task_name` varchar(100) NOT NULL DEFAULT '',
  PRIMARY KEY (`job_sche_id`)
) ENGINE=InnoDB AUTO_INCREMENT=219 DEFAULT CHARSET=utf8;

输出示例

1
2
3
4
5
6
7
8
9
CREATE TABLE "public"."job_instance"
(
  "job_sche_id" SERIAL NOT NULL,
  "task_name" VARCHAR(400) NOT NULL DEFAULT '',
  PRIMARY KEY ("job_sche_id")
)
  WITH ( ORIENTATION = ROW, COMPRESSION = NO )
  NOCOMPRESS
  DISTRIBUTE BY HASH ("job_sche_id");

此外,GaussDB(DWS)也不支持基于AUTO_INCREMENT属性修改表定义信息。DSC迁移时会将其移除。

输入示例

1
2
3
4
5
6
7
8
CREATE TABLE IF NOT EXISTS `public`.`runoob_alter_test`(
    `dataType1` int NOT NULL AUTO_INCREMENT,
    `dataType2` FLOAT(10,2),
    PRIMARY KEY(`dataType1`)
);

ALTER TABLE runoob_alter_test AUTO_INCREMENT 100;
ALTER TABLE runoob_alter_test AUTO_INCREMENT=100;

输出示例

1
2
3
4
5
6
7
8
9
CREATE TABLE IF NOT EXISTS "public"."runoob_alter_test"
(
  "datatype1" SERIAL NOT NULL,
  "datatype2" REAL,
  PRIMARY KEY ("datatype1")
)
  WITH ( ORIENTATION = ROW, COMPRESSION = NO )
  NOCOMPRESS
  DISTRIBUTE BY HASH ("datatype1");