更新时间:2024-07-19 GMT+08:00

DEFAULT

alter table增加列包含not null约束时,如果无default值,在MySQL中会插入默认值,而在GaussDB(DWS)中插入到非空表时会报错,因此针对常见数据类型,alter table add column包含not null约束补充默认default值(详情见表1)

输入示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
DROP TABLE IF EXISTS default_test_1;
CREATE TABLE default_test_1
(
    c1 tinyint,
    c2 smallint,
    c3 MEDIUMINT,
    c4 int,
    c5 bigint
);
INSERT INTO default_test_1
VALUES (1, 1, 1, 1, 1);
SELECT *
FROM default_test_1;
ALTER TABLE default_test_1 add COLUMN (c1_1 tinyint not null, c2_1 SMALLINT not null, c3_1 MEDIUMINT not null, c4_1 int not null, c5_1 BIGINT not null);
SELECT *
FROM default_test_1;

输出示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
DROP TABLE IF EXISTS "public"."default_test_1";
CREATE TABLE "public"."default_test_1" (
  "c1" SMALLINT,
  "c2" SMALLINT,
  "c3" INTEGER,
  "c4" INTEGER,
  "c5" BIGINT
) WITH (ORIENTATION = ROW, COMPRESSION = NO) NOCOMPRESS DISTRIBUTE BY HASH ("c1");
INSERT INTO
  "public"."default_test_1"
VALUES
  (1, 1, 1, 1, 1);
SELECT
  *
FROM
  default_test_1;
ALTER TABLE
  "public"."default_test_1"
ADD
  COLUMN "c1_1" SMALLINT NOT NULL DEFAULT 0,
ADD
  COLUMN "c2_1" SMALLINT NOT NULL DEFAULT 0,
ADD
  COLUMN "c3_1" INTEGER NOT NULL DEFAULT 0,
ADD
  COLUMN "c4_1" INTEGER NOT NULL DEFAULT 0,
ADD
  COLUMN "c5_1" BIGINT NOT NULL DEFAULT 0;
SELECT
  *
FROM
  default_test_1;
表1

数据类型

MYSQL

GaussDB(DWS)对应数据类型

转换结果

有符号整型

TINYINT

SMALLINT

0

SMALLINT

SMALLINT

0

MEDIUMINT

INT/INTEGER

0

INT/INTEGER

INT/INTEGER

0

BIGINT

BIGINT

0

无符号整型

TINYINT UNSIGNED

TINYINT

0

SMALLINT UNSIGNED

INT

0

MEDIUMINT UNSIGNED

INT

0

INT UNSIGNED

BIGINT

0

BIGINT UNSIGNED

NUMERIC

0

浮点型

FLOAT

REAL

0

FLOAT UNSIGNED

REAL

0

DOUBLE

DOUBLE PRECISION

0

DOUBLE UNSIGNED

DOUBLE PRECISION

0

DOUBLE PRECISION (DOUBLE)

DOUBLE PRECISION

0

DOUBLE PRECISION [UNSIGNED] (DOUBLE)

DOUBLE PRECISION

0

REAL(默认按double处理,

设置REAL_AS_FLOAT时,按float处理)

REAL/DOUBLE PRECISION

0

REAL [UNSIGNED](默认按double处理,

设置REAL_AS_FLOAT时,按float处理)

REAL/DOUBLE PRECISION

0

NUMERIC/DECIMAL

NUMERIC/DECIMAL

0

布尔类型

BOOLEAN/BOOL

BOOLEAN

0

字符类型

CHAR/CHARACTER

CHAR/CHARACTER * 4

空字符串

NCHAR

CHAR/CHARACTER * 4

空字符串

VARCHAR

VARCHAR * 4

空字符串

文本类型

TINYTEXT

TEXT

空字符串

TEXT

TEXT

空字符串

MEDIUMTEXT

TEXT

空字符串

LONGTEXT

TEXT

空字符串

二进制类型

BINARY

BYTEA

空字符串

VARBINARY

BYTEA

空字符串

TINYBLOB

BYTEA(列存)/BLOB(非列存)

空字符串

BLOB

BYTEA(列存)/BLOB(非列存)

空字符串

MEDIUMBLOB

BYTEA(列存)/BLOB(非列存)

空字符串

LONGBLOB

BYTEA(列存)/BLOB(非列存)

空字符串

CHAR BYTE(BINARY)

BYTEA

空字符串

日期类型

DATE

DATE

1970-01-01

TIME

TIME(P) WITHOUT TIME ZONE

00:00:00

DATETIME

TIMESTAMP WITHOUT TIME ZONE

1970-01-01 00:00:00

TIMESTAMP

TIMESTAMP(P) WITH TIME ZONE

1970-01-01 00:00:00

datetime/datetime(0)

datetime->timestamp with out time zone

datetime(0)->timestamp(0) with out time zone

1970-01-01 00:00:00.xx(x个数与精度位数有关)

YEAR

SMALLINT

0000

位串类型

BIT

BIT

DEFAULT 0:: BIT(x)

集合类型

ENUM

CHARACTER VARYING()

默认第一个元素

SET

CHARACTER VARYING()

空字符串

自增序列

SERIAL( BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE)

CREATE SEQUENCE serial_name INCREMENT 2 START 1

不作处理

JSON类型

JSON

jsonb

不作处理

空间类型

point

point

不做处理

polygon

polygon

不做处理

geometry

不支持,原状返回

不做处理

linestring

polygon

不做处理

geometrycollection

不支持,原状返回

不做处理

multipoint

box

不做处理

multilinestring

box

不做处理

multipolygon

polygon

不做处理