基本数据类型
概述
MySQL支持多种基本数据类型,大致可以分为以下几类:数值、日期/时间、字符串(字符)、大对象、集合、二进制和布尔类型。GaussDB(DWS)或不支持部分MySQL基本数据类型,或不支持部分MySQL数据类型精度设定,或不支持"UNSIGNED"、"ZEROFILL"等关键字。DSC工具会根据GaussDB的支持情况做相应迁移。
数据类型是数据的一个基本属性,用于区分不同类型的数据。不同的数据类型所占的存储空间不同,能够进行的操作也不相同。数据库中的数据存储在数据表中。数据表中的每一列都定义了数据类型,用户存储数据时,须遵从这些数据类型的属性,否则可能会出错。各数据类型的基本概述详见表1。
数据类型 |
基本描述 |
---|---|
数字类型 |
内容详见数字类型对照。 |
日期和时间类型 |
文档中介绍如下日期和时间类型:DATETIME、TIME、TIMESTAMP、YEAR。GaussDB(DWS)不支持以上类型,DSC工具将会对其转换。内容详见时间和日期类型对照。 |
字符串类型 |
MySQL以字符单位解释字符列定义中的长度规范。这适用于 CHAR、VARCHAR和TEXT类型。内容详见字符串类型对照。 |
空间数据类型 |
MySQL具有对应于OpenGIS类的空间数据类型。内容详见空间数据类型对照。 |
大对象类型 |
BLOB是一个二进制大对象,可以容纳可变数量的数据。这四个BLOB 类型是TINYBLOB,BLOB, MEDIUMBLOB,和LONGBLOB。这些不同之处仅在于它们可以容纳的值的最大长度。内容详见大对象类型对照。 |
集合类型 |
|
布尔类型 |
MySQL 支持两种布尔写法:BOOL、BOOLEAN。内容详见布尔类型对照。 |
二进制类型 |
|
数字类型对照
MySQL数字类型 |
MySQL INPUT |
GaussDB(DWS) OUTPUT |
---|---|---|
DEC |
DEC DEC[(M[,D])] [UNSIGNED] [ZEROFILL] |
DECIMAL DECIMAL[(M[,D])] |
DECIMAL |
DECIMAL[(M[,D])] [UNSIGNED] [ZEROFILL] |
DECIMAL[(M[,D])] |
DOUBLE PRECISION |
DOUBLE PRECISION DOUBLE PRECISION [(M[,D])] [UNSIGNED] [ZEROFILL] |
FLOAT FLOAT[(M)] |
DOUBLE |
DOUBLE[(M[,D])] [UNSIGNED] [ZEROFILL] |
FLOAT[(M)] |
FIXED |
FIXED FIXED[(M[,D])] [UNSIGNED] [ZEROFILL] |
DECIMAL DECIMAL[(M[,D])] |
FLOAT |
FLOAT FLOAT [(M[,D])] [UNSIGNED] [ZEROFILL] FLOAT(p) [UNSIGNED] [ZEROFILL] |
FLOAT FLOAT[(M)] FLOAT(p) |
INT |
INT INT(p) [UNSIGNED] [ZEROFILL] |
INTEGER INTEGER(p) |
INTEGER |
INTEGER INTEGER(p) [UNSIGNED] [ZEROFILL] |
INTEGER INTEGER(p) |
MEDIUMINT |
MEDIUMINT MEDIUMINT(p) [UNSIGNED] [ZEROFILL] |
INTEGER INTEGER(p) |
NUMERIC |
NUMERIC NUMERIC [(M[,D])] [UNSIGNED] [ZEROFILL] |
DECIMAL DECIMAL[(M[,D])] |
REAL |
REAL[(M[,D])] |
FLOAT[(M)] |
SMALLINT |
SMALLINT SMALLINT(p) [UNSIGNED] [ZEROFILL] |
SMALLINT |
TINYINT |
TINYINT TINYINT(n) TINYINT(n) ZEROFILL TINYINT(n) UNSIGNED ZEROFILL |
TINYINT TINYINT TINYINT SMALLINT |
输入示例:TINYINT
CREATE TABLE IF NOT EXISTS `runoob_dataType_test`( `dataType_1` TINYINT, `dataType_2` TINYINT(0), `dataType_3` TINYINT(255), `dataType_4` TINYINT(255) UNSIGNED ZEROFILL, `dataType_5` TINYINT(255) ZEROFILL );
输出示例
CREATE TABLE "public"."runoob_datatype_test" ( "datatype_1" TINYINT, "datatype_2" TINYINT, "datatype_3" TINYINT, "datatype_4" SMALLINT, "datatype_5" TINYINT ) WITH ( ORIENTATION = ROW, COMPRESSION = NO ) NOCOMPRESS DISTRIBUTE BY HASH ("datatype_1");
时间和日期类型对照
MySQL日期时间类型 |
MySQL INPUT |
GaussDB(DWS) OUTPUT |
---|---|---|
DATETIME |
DATETIME[(fsp)] |
TIMESTAMP[(fsp)] WITHOUT TIME ZONE |
TIME |
TIME[(fsp)] |
TIME[(fsp)] WITHOUT TIME ZONE |
TIMESTAMP |
TIMESTAMP[(fsp)] |
TIMESTAMP[(fsp)] WITH TIME ZONE |
YEAR |
YEAR[(4)] |
VARCHAR(4) |
该fsp值如果给出,则必须在0到6的范围内。值为0表示没有小数部分。如果省略,则默认精度为0。
输入示例:DATETIME
CREATE TABLE IF NOT EXISTS `runoob_dataType_test`( `dataType_1` DATETIME, `dataType_2` DATETIME(0), `dataType_3` DATETIME(6), `dataType_4` DATETIME DEFAULT NULL, `dataType_5` DATETIME DEFAULT '2018-10-12 15:27:33.999999' );
输出示例
CREATE TABLE "public"."runoob_datatype_test" ( "datatype_1" TIMESTAMP WITHOUT TIME ZONE, "datatype_2" TIMESTAMP(0) WITHOUT TIME ZONE, "datatype_3" TIMESTAMP(6) WITHOUT TIME ZONE, "datatype_4" TIMESTAMP WITHOUT TIME ZONE DEFAULT NULL, "datatype_5" TIMESTAMP WITHOUT TIME ZONE DEFAULT '2018-10-12 15:27:33.999999' ) WITH ( ORIENTATION = ROW, COMPRESSION = NO ) NOCOMPRESS DISTRIBUTE BY HASH ("datatype_1");
输入示例:TIME
CREATE TABLE IF NOT EXISTS `runoob_dataType_test`( `dataType_1` TIME DEFAULT '20:58:10', `dataType_2` TIME(3) DEFAULT '20:58:10', `dataType_3` TIME(6) DEFAULT '20:58:10', `dataType_4` TIME(6) DEFAULT '2018-10-11 20:00:00', `dataType_5` TIME(6) DEFAULT '20:58:10.01234', `dataType_6` TIME(6) DEFAULT '2018-10-11 20:00:00.01234', `dataType_7` TIME DEFAULT NULL, `dataType_8` TIME(6) DEFAULT NULL, PRIMARY KEY (dataType_1) );
输出示例
CREATE TABLE "public"."runoob_datatype_test" ( "datatype_1" TIME WITHOUT TIME ZONE DEFAULT '20:58:10', "datatype_2" TIME(3) WITHOUT TIME ZONE DEFAULT '20:58:10', "datatype_3" TIME(6) WITHOUT TIME ZONE DEFAULT '20:58:10', "datatype_4" TIME(6) WITHOUT TIME ZONE DEFAULT '2018-10-11 20:00:00', "datatype_5" TIME(6) WITHOUT TIME ZONE DEFAULT '20:58:10.01234', "datatype_6" TIME(6) WITHOUT TIME ZONE DEFAULT '2018-10-11 20:00:00.01234', "datatype_7" TIME WITHOUT TIME ZONE DEFAULT NULL, "datatype_8" TIME(6) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY ("datatype_1") ) WITH ( ORIENTATION = ROW, COMPRESSION = NO ) NOCOMPRESS DISTRIBUTE BY HASH ("datatype_1");
输入示例:TIMESTAMP
CREATE TABLE IF NOT EXISTS `runoob_dataType_test`( `dataType_1` TIMESTAMP, `dateType_4` TIMESTAMP DEFAULT '2018-10-12 15:27:33', `dateType_5` TIMESTAMP DEFAULT '2018-10-12 15:27:33.999999', `dateType_6` TIMESTAMP DEFAULT '2018-10-12 15:27:33', `dateType_7` TIMESTAMP DEFAULT '2018-10-12 15:27:33', `dataType_8` TIMESTAMP(0) DEFAULT '2018-10-12 15:27:33', `dateType_9` TIMESTAMP(6) DEFAULT '2018-10-12 15:27:33' );
输出示例
CREATE TABLE "public"."runoob_datatype_test" ( "datatype_1" TIMESTAMP WITH TIME ZONE, "datetype_4" TIMESTAMP WITH TIME ZONE DEFAULT '2018-10-12 15:27:33', "datetype_5" TIMESTAMP WITH TIME ZONE DEFAULT '2018-10-12 15:27:33.999999', "datetype_6" TIMESTAMP WITH TIME ZONE DEFAULT '2018-10-12 15:27:33', "datetype_7" TIMESTAMP WITH TIME ZONE DEFAULT '2018-10-12 15:27:33', "datatype_8" TIMESTAMP(0) WITH TIME ZONE DEFAULT '2018-10-12 15:27:33', "datetype_9" TIMESTAMP(6) WITH TIME ZONE DEFAULT '2018-10-12 15:27:33' ) WITH ( ORIENTATION = ROW, COMPRESSION = NO ) NOCOMPRESS DISTRIBUTE BY HASH ("datatype_1");
输入示例:YEAR
CREATE TABLE IF NOT EXISTS `runoob_dataType_test`( `dataType_1` YEAR, `dataType_2` YEAR(4), `dataType_3` YEAR DEFAULT '2018', `dataType_4` TIME DEFAULT NULL );
输出示例
CREATE TABLE "public"."runoob_datatype_test" ( "datatype_1" VARCHAR(4), "datatype_2" VARCHAR(4), "datatype_3" VARCHAR(4) DEFAULT '2018', "datatype_4" TIME WITHOUT TIME ZONE DEFAULT NULL ) WITH ( ORIENTATION = ROW, COMPRESSION = NO ) NOCOMPRESS DISTRIBUTE BY HASH ("datatype_1");
字符串类型对照
MySQL字符串类型 |
MySQL INPUT |
GaussDB(DWS) OUTPUT |
---|---|---|
CHAR |
CHAR[(0)] |
CHAR[(1)] |
LONGTEXT |
LONGTEXT |
TEXT |
MEDIUMTEXT |
MEDIUMTEXT |
TEXT |
TEXT |
TEXT |
TEXT |
TINYTEXT |
TINYTEXT |
TEXT |
VARCHAR |
VARCHAR[(0)] |
VARCHAR[(1)] |
输入示例:CHAR
MySQL 一个长度CHAR列被固定在创建表声明的长度。长度可以是从0到255之间的任何值。CHAR 存储值时,它们将空格填充到指定的长度。
CREATE TABLE IF NOT EXISTS `runoob_dataType_test`( `dataType_1` CHAR NOT NULL, `dataType_2` CHAR(0) NOT NULL, `dataType_3` CHAR(255) NOT NULL );
输出示例
CREATE TABLE "public"."runoob_datatype_test" ( "datatype_1" CHAR NOT NULL, "datatype_2" CHAR(1) NOT NULL, "datatype_3" CHAR(255) NOT NULL ) WITH ( ORIENTATION = ROW, COMPRESSION = NO ) NOCOMPRESS DISTRIBUTE BY HASH ("datatype_1");
输入示例:[LONG|MEDIUM|TINY]TEXT
CREATE TABLE IF NOT EXISTS `runoob_dataType_test`( `dataType_1` LONGTEXT, `dataType_2` MEDIUMTEXT, `dataType_3` TEXT, `dataType_4` TINYTEXT );
输出示例
CREATE TABLE "public"."runoob_datatype_test" ( "datatype_1" TEXT, "datatype_2" TEXT, "datatype_3" TEXT, "datatype_4" TEXT ) WITH ( ORIENTATION = ROW, COMPRESSION = NO ) NOCOMPRESS DISTRIBUTE BY HASH ("datatype_1");
输入示例:VARCHAR
MySQL VARCHAR列中的 值是可变长度的字符串。长度可以指定为0到65,535之间的值。
CREATE TABLE IF NOT EXISTS `runoob_dataType_test`( `dataType_1` VARCHAR(0), `dataType_2` VARCHAR(1845) );
输出示例
CREATE TABLE "public"."runoob_datatype_test" ( "datatype_1" VARCHAR(1), "datatype_2" VARCHAR(1845) ) WITH ( ORIENTATION = ROW, COMPRESSION = NO ) NOCOMPRESS DISTRIBUTE BY HASH ("datatype_1");
空间数据类型对照
MySQL空间数据类型 |
MySQL INPUT |
GaussDB(DWS) OUTPUT |
---|---|---|
GEOMETRY |
GEOMETRY |
CIRCLE |
POINT |
POINT |
POINT |
LINESTRING |
LINESTRING |
POLYGON |
POLYGON |
POLYGON |
POLYGON |
MULTIPOINT |
MULTIPOINT |
BOX |
MULTILINESTRING |
MULTILINESTRING |
BOX |
MULTIPOLYGON |
MULTIPOLYGON |
POLYGON |
GEOMETRYCOLLECTION |
GEOMETRYCOLLECTION |
CIRCLE |
- GEOMETRY可以存储任何类型的几何值。其他单值类型(POINT, LINESTRING和POLYGON)将其值限制为特定的几何类型。
- GEOMETRYCOLLECTION可以存储任何类型的对象的集合。其他集合类型(MULTIPOINT, MULTILINESTRING, MULTIPOLYGON,和 GEOMETRYCOLLECTION)限制集合成员向那些具有特定的几何形状的类型。
CREATE TABLE `t_geo_test2` ( `id` int(11) NOT NULL, `name` varchar(255), `geometry_1` geometry NOT NULL, `point_1` point NOT NULL, `linestring_1` linestring NOT NULL, `polygon_1` polygon NOT NULL, `multipoint_1` multipoint NOT NULL, `multilinestring_1` multilinestring NOT NULL, `multipolygon_1` multipolygon NOT NULL, `geometrycollection_1` geometrycollection NOT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB;
输出示例
CREATE TABLE "public"."t_geo_test2" ( "id" INTEGER(11) NOT NULL, "name" VARCHAR(255), "geometry_1" CIRCLE NOT NULL, "point_1" POINT NOT NULL, "linestring_1" POLYGON NOT NULL, "polygon_1" POLYGON NOT NULL, "multipoint_1" BOX NOT NULL, "multilinestring_1" BOX NOT NULL, "multipolygon_1" POLYGON NOT NULL, "geometrycollection_1" CIRCLE NOT NULL, PRIMARY KEY ("id") ) WITH ( ORIENTATION = ROW, COMPRESSION = NO ) NOCOMPRESS DISTRIBUTE BY HASH ("id");
大对象类型对照
MySQL大对象类型 |
MySQL INPUT |
GaussDB(DWS) OUTPUT |
---|---|---|
TINYBLOB |
TINYBLOB |
BLOB |
BLOB |
BLOB |
BLOB |
MEDIUMBLOB |
MEDIUMBLOB |
BLOB |
LONGBLOB |
LONGBLOB |
BLOB |
输入示例:[TINY|MEDIUM|LONG]BLOB
CREATE TABLE IF NOT EXISTS `runoob_dataType_test`( `dataType_1` BIGINT, `dataType_2` TINYBLOB, `dataType_3` BLOB, `dataType_4` MEDIUMBLOB, `dataType_5` LONGBLOB );
输出示例
CREATE TABLE "public"."runoob_datatype_test" ( "datatype_1" BIGINT, "datatype_2" BLOB, "datatype_3" BLOB, "datatype_4" BLOB, "datatype_5" BLOB ) WITH ( ORIENTATION = ROW, COMPRESSION = NO ) NOCOMPRESS DISTRIBUTE BY HASH ("datatype_1");
集合类型对照
MySQL集合类型 |
MySQL INPUT |
GaussDB(DWS) OUTPUT |
---|---|---|
ENUM |
ENUM |
VARCHAR(14) |
SET |
SET |
VARCHAR(14) |
输入示例:ENUM
CREATE TABLE IF NOT EXISTS `runoob_dataType_test`( id int(2) PRIMARY KEY, `dataType_17` ENUM('dws-1', 'dws-2', 'dws-3') );
输出示例
CREATE TABLE "public"."runoob_datatype_test" ( "id" INTEGER(2) PRIMARY KEY, "datatype_17" VARCHAR(14) ) WITH ( ORIENTATION = ROW, COMPRESSION = NO ) NOCOMPRESS DISTRIBUTE BY HASH ("id");
输入示例:SET
CREATE TABLE IF NOT EXISTS `runoob_tbl_test`( `dataType_18` SET('dws-1', 'dws-2', 'dws-3') );
输出示例
CREATE TABLE "public"."runoob_tbl_test" ( "datatype_18" VARCHAR(14) ) WITH ( ORIENTATION = ROW, COMPRESSION = NO ) NOCOMPRESS DISTRIBUTE BY HASH ("datatype_18");
布尔类型对照
输入示例:BOOL/BOOLEAN
CREATE TABLE IF NOT EXISTS `runoob_dataType_test`( `dataType_1` INT, `dataType_2` BOOL, `dataType_3` BOOLEAN );
输出示例
CREATE TABLE "public"."runoob_datatype_test" ( "datatype_1" INTEGER, "datatype_2" BOOLEAN, "datatype_3" BOOLEAN ) WITH ( ORIENTATION = ROW, COMPRESSION = NO ) NOCOMPRESS DISTRIBUTE BY HASH ("datatype_1");
二进制类型对照
MySQL二进制类型 |
MySQL INPUT |
GaussDB(DWS) OUTPUT |
---|---|---|
BIT[(M)] |
BIT[(M)] |
BIT[(M)] |
BINARY[(M)] |
BINARY[(M)] |
BYTEA |
VARBINARY[(M)] |
VARBINARY[(M)] |
BYTEA |
输入示例:BIT
CREATE TABLE IF NOT EXISTS `runoob_dataType_test`( `dataType_1` INT, `dataType_2` BIT(1), `dataType_3` BIT(64) );
输出示例
CREATE TABLE "public"."runoob_datatype_test" ( "datatype_1" INTEGER, "datatype_2" BIT(1), "datatype_3" BIT(64) ) WITH ( ORIENTATION = ROW, COMPRESSION = NO ) NOCOMPRESS DISTRIBUTE BY HASH ("datatype_1");
输入示例:[VAR]BINARY
CREATE TABLE IF NOT EXISTS `runoob_dataType_test`( `dataType_1` INT, `dataType_2` BINARY, `dataType_3` BINARY(0), `dataType_4` BINARY(255), `dataType_5` VARBINARY(0), `dataType_6` VARBINARY(6553) );
输出示例
CREATE TABLE "public"."runoob_datatype_test" ( "datatype_1" INTEGER, "datatype_2" BYTEA, "datatype_3" BYTEA, "datatype_4" BYTEA, "datatype_5" BYTEA, "datatype_6" BYTEA ) WITH ( ORIENTATION = ROW, COMPRESSION = NO ) NOCOMPRESS DISTRIBUTE BY HASH ("datatype_1");