In MySQL mode of OceanBase Database, the common data types include: numeric, date and time, character, large object, text, enumeration, set, JSON, and spatial data types. This topic describes each data type.
Numeric data types
Numeric data types include:
Integer types: BOOL/BOOLEAN/TINYINT, SMALLINT, MEDIUMINT, INT/INTEGER, and BIGINT
Fixed-point types: DECIMAL and NUMERIC
Floating-point types: FLOAT/DOUBLE
Bit-value type: BIT
Category |
Type |
Range |
Description |
|---|---|---|---|
| Numeric data types-Integer | BIGINT | With sign: [-263, 263 - 1] Without sign: [0, 264 - 1] |
Used to represent a large integer. |
| INT INTEGER |
With sign: [-231, 231 - 1] Without sign: [0, 232 - 1] |
Used to represent a normal-sized integer. | |
| SMALLINT | With sign: [-215, 215 - 1] Without sign: [0, 216 - 1] |
Used to represent a small integer. | |
| BOOL BOOLEAN TINYINT |
With sign: [-27, 27 - 1] Without sign: [0, 28 - 1] |
|
|
| Fixed-point | DECIMAL | The value range and precision depend on the precision and scale, and whether it is a signed number. M is the total number of digits (precision) that can be stored, and D is the number of digits after the decimal point (scale). The decimal point and the negative sign “-” are not counted as digits. If D is 0, the value does not have a decimal point or fractional part. The maximum number of effective digits in the integer part is M - D (precision - scale). All basic calculations (+, -, *, /) on DECIMAL columns use 65-bit precision. | DECIMAL is equivalent to NUMERIC. |
| Floating-point | FLOAT | [-3.402823466E+38, -1.175494351E-38]、0 and [1.175494351E-38,3.402823466E+38], with 7 digits of precision. | Used to represent a single-precision floating-point number. |
| DOUBLE | [-1.7976931348623157E+308, -2.2250738585072014E-308]、0 and [2.2250738585072014E-308,1.7976931348623157E+308], with 15 digits of precision. | Used to represent a double-precision floating-point number. | |
| Numeric data types-Integer, fixed-point, and floating-point | NUMBER NUMBER(P) NUMBER(P, S) |
Precision (P) is the precision, and Scale (S) is the number of digits to the right of the decimal point. The value range of P is 1 to 38, and the value range of S is -84 to 127.
|
Includes integers, fixed-point numbers, and floating-point numbers. |
| Bit-value type | BIT | Contains two values: 0 and 1. The length is 1 bit. | Used to store bit values. |
Date and time types
The date and time types are: DATETIME, TIMESTAMP, DATE, TIME, and YEAR.
Category |
Type |
Range |
Description |
|---|---|---|---|
| Time types | DATE | YYYY-MM-DD, which contains only the date part. | Used to represent values that contain the date part but not the time part. |
| TIME | HH:MM:SS[.fraction], which contains only the time part. | Can represent a time of day, a duration, or the interval between two events (which can be much greater than 24 hours). | |
| DATETIME | YYYY-MM-DD HH:MM:SS[.fraction], which contains the date and time (ignoring the time zone). | Used to represent values that contain both the date and time parts. | |
| TIMESTAMP | YYYY-MM-DD HH:MM:SS[.fraction], which contains the date and time (considering the time zone). | Used to represent values that contain both the date and time parts. | |
| YEAR | YYYY,[1901, 2155] | Used to represent the year in the 4-digit format. |
Character types
The character types are: CHAR, VARCHAR, BINARY, and VARBINARY.
Category |
Type |
Range |
Description |
|---|---|---|---|
| Variable-length character types | VARCHAR(N) | Up to 262,144 bytes in length. UTF8MB4 character set. | The declared length is the maximum number of characters that can be stored. |
| VARBINARY | Up to 1,048,576 bytes in length. BINARY character set. | Similar to the VARCHAR type, but it stores binary byte strings. | |
| Fixed-length character types | CHAR(N) | Up to 256 bytes in length. UTF8MB4 character set. | The declared length is the maximum number of characters that can be stored. |
| BINARY | Up to 256 bytes in length. BINARY character set. | Similar to the CHAR type, but it stores binary byte strings. |
Large object types
The large object types are: TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB.
Category |
Type |
Range |
Description |
|---|---|---|---|
| Blob | TINYBLOB | 0 to 255 bytes | The TINYBLOB type stores binary data with a length of up to 255 bytes. A length prefix that occupies 1 byte is added before the value to indicate the number of bytes in the value. |
| BLOB | 0 to 64 KB | The BLOB type stores binary data of up to 65,536 bytes in length. It can store a variable amount of data. | |
| MEDIUMBLOB | 0 to 16 MB | The MEDIUMBLOB type stores binary data of up to 16,777,216 bytes in length. | |
| LONGBLOB | 0 to 48 MB | The LONGBLOB type stores binary data of up to 50,331,648 bytes in length. |
Text types
The text types are: TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT.
Category |
Type |
Range |
Description |
|---|---|---|---|
| Text | TINYTEXT | 0 to 255 bytes, [28 - 1]. | Similar to the TEXT type, but with a maximum length of 256 characters. If the value contains multi-byte characters, the maximum effective length is shorter. |
| TEXT | 0 to 64 KB, [216 - 1]. | The maximum length of a column of this type is 65,536 characters. If the value contains multi-byte characters, the maximum effective length is shorter. | |
| MEDIUMTEXT | 0 to 16 MB, [224 - 1]. | Similar to the TEXT type, but with a maximum length of 16,777,216 characters. If the value contains multi-byte characters, the maximum effective length is shorter. | |
| LONGTEXT | 0 to 48 MB. | Similar to the TEXT type, but with a maximum length of 50,331,648 or 48 MB of characters. If the value contains multi-byte characters, the maximum effective length is shorter. The maximum length of a column of this type depends on the maximum packet size and available memory in the client/server protocol. |
Enumeration type
An enumeration type is a data type that contains a predefined set of values. For example, the sex field is of the enumeration type, and its values are M for male and F for female.
Category |
Type |
Applicability |
Description |
|---|---|---|---|
| Type | ENUM | At most 65,535 elements, with each element being no more than 255 characters long. The character set is UTF8MB4. | A string object that can contain only one value. The value must be from the specified list of values, such as 'value1', 'value2', .... The enumerated values must be enclosed in quotation marks. |
Set type
A set type: allows you to select one or more values within a specified range.
Category |
Type |
Range |
Description |
|---|---|---|---|
| Type | SET | At most 65,535 elements, with each element up to 255 characters long, in UTF8MB4 character set. | A string object that can have zero or more values from the list 'Value1', 'Value2', .... |
JSON data types
Category |
Type |
Applicability |
Description |
|---|---|---|---|
| Type | JSON | The data can be in the form of an object or an array. | A lightweight text data format for storing and representing data. |
Spatial data types
A spatial data type is a data type that represents spatial data, such as geographical locations, coordinates, and rectangles.
For more information, see Overview of spatial data types.
Example
Example 1
Create a
full-type table.CREATE TABLE `full-type table` ( `col1` int(11) NOT NULL AUTO_INCREMENT COMMENT 'First column', `col2` integer DEFAULT NULL COMMENT 'Second column', `col3` decimal(10,2) DEFAULT NULL COMMENT 'Third column', col4` number DEFAULT NULL COMMENT 'Fourth column', `col5` tinyint(4) DEFAULT NULL COMMENT 'Fifth column', `col6` smallint(6) DEFAULT NULL COMMENT 'Sixth column', `col7` mediumint(9) DEFAULT NULL COMMENT 'Seventh column', `col8` bigint(20) DEFAULT NULL COMMENT 'Eighth column', `col9` float(10,2) DEFAULT NULL COMMENT 'Ninth column', `col10` double(10,2) DEFAULT NULL COMMENT 'Tenth column', `col11` varchar(10) DEFAULT NULL COMMENT 'Eleventh column', `col12` char(10) NOT NULL DEFAULT 'Chinese field' COMMENT 'Twelfth column', `col13` text DEFAULT NULL COMMENT 'Thirteenth column', `col14` tinytext DEFAULT NULL COMMENT 'Fourteenth column', `col15` mediumtext DEFAULT NULL COMMENT 'Fifteenth column', `col16` longtext DEFAULT NULL COMMENT 'Sixteenth column', `col17` blob DEFAULT NULL COMMENT 'Seventeenth column', `col18` tinyblob DEFAULT NULL COMMENT 'Eighteenth column', `col19` longblob DEFAULT NULL COMMENT 'Nineteenth column', `col20` mediumblob DEFAULT NULL COMMENT 'Twentieth column', `col21` binary(16) DEFAULT NULL COMMENT 'Twenty-first column', `col22` varbinary(16) DEFAULT NULL COMMENT 'Twenty-second column', `col23` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Twenty-third column', `col24` time DEFAULT NULL COMMENT 'Twenty-fourth column', `col25` date DEFAULT NULL COMMENT 'Twenty-fifth column', `col26` datetime DEFAULT NULL COMMENT 'Twenty-sixth column', `col27` year(4) DEFAULT NULL COMMENT 'Twenty-seventh column', PRIMARY KEY (`col1`) ) AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = 'zstd_1.0' REPLICA_NUM = 1 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 0 COMMENT = 'Full-type table';Insert data into the
full-type table.obclient> INSERT INTO `full-type table` VALUES(101,2,3,4,5,6,7,8,9.1,10.1,'Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen','Twenty','Twenty-one','Twenty-two','19700101','00:00:00','19700101','19700101','1970'); Query OK, 1 row affectedQuery the
full-type table.obclient> select * from `full-type table`; +------+------+------+-------+------+------+------+------+------+-------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+------------------+-----------+---------------------+----------+------------+---------------------+-------+ | col1 | col2 | col3 | col4 | col5 | col6 | col7 | col8 | col9 | col10 | col11 | col12 | col13 | col14 | col15 | col16 | col17 | col18 | col19 | col20 | col21 | col22 | col23 | col24 | col25 | col26 | col27 | +------+------+------+-------+------+------+------+------+------+-------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+------------------+-----------+---------------------+----------+------------+---------------------+-------+ | 101 | 2 | 3.00 | 4 | 5 | 6 | 7 | 8 | 9.10 | 10.10 | Eleven | Twelve | Thirteen | Fourteen | Fifteen | Sixteen | Seventeen | Eighteen | Nineteen | Twenty | Twenty-one | Twenty-two | 1970-01-01 00:00:00 | 00:00:00 | 1970-01-01 | 1970-01-01 00:00:00 | 1970 | +------+------+------+-------+------+------+------+------+------+-------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+------------------+-----------+---------------------+----------+------------+---------------------+-------+ 1 row in set
Example 2
Create a table named
t_bool.obclient> CREATE TABLE t_bool(c1 bool); Query OK, 0 rows affecteInsert data into the
t_booltable.obclient> INSERT INTO t_bool VALUES(1); Query OK, 1 row affectedQuery the
t_booltable.obclient> SELECT * FROM t_bool; +------+ | c1 | +------+ | 1 | +------+ 1 row in set
Example 3
Create a table named
t_boolean.obclient> CREATE TABLE t_boolean(c1 boolean); Query OK, 0 rows affectedInsert data into the
t_booleantable.obclient> INSERT INTO t_boolean VALUES(1); Query OK, 1 row affectedQuery the
t_booleantable.obclient> SELECT * FROM t_boolean; +------+ | c1 | +------+ | 1 | +------+ 1 row in set
Example 4
Create a table named
t_bit.obclient> CREATE TABLE t_bit(c1 bit(8)); Query OK, 0 rows affectedInsert data into the
t_bittable.obclient> INSERT INTO t_bit VALUES(101); Query OK, 1 row affectedQuery the
t_bittable.obclient> SELECT c1,bin(c1),hex(c1) FROM t_bit; +------+---------+---------+ | c1 | bin(c1) | hex(c1) | +------+---------+---------+ | e | 1100101 | 65 | +------+---------+---------+ 1 row in set
Example 5
Create a table named
t_enum.obclient> CREATE TABLE t_enum(c1 enum('RED','GREEN','BLUE')); Query OK, 0 rows affectedInsert data into the
t_enumtable.obclient> INSERT INTO t_enum VALUES('RED'); Query OK, 1 row affectedQuery the
t_enumtable.obclient> SELECT * FROM t_enum; +------+ | c1 | +------+ | RED | +------+ 1 row in set
Example 6
Create a table named
t_json.obclient> CREATE TABLE t_json(c1 json); Query OK, 0 rows affectedInsert data into the
t_settable.obclient> INSERT INTO t_json VALUES( '{"name":"ny", "gender":"Male","age":22}' ); Query OK, 1 row affectedQuery the
t_settable.obclient> SELECT * FROM t_json; +--------------------------------------------+ | c1 | +--------------------------------------------+ | {"age": 22, "name": "ny", "gender": "Male"} | +--------------------------------------------+ 1 row in set
Example 7
Create a table named
t_gis.obclient> CREATE TABLE t_gis(c1 geometry); Query OK, 0 rows affectedInsert data into the
t_gistable.obclient> INSERT INTO t_gis VALUES(ST_GeomFromText('POINT(100 100)')); Query OK, 1 row affectedQuery the
t_gistable.SELECT st_astext(c1) FROM t_gis; +----------------+ | st_astext(c1) | +----------------+ | POINT(100 100) | +----------------+ 1 row in set
