When you change the data type of a column in the MySQL mode of OceanBase Database, you must first consider the table-level column type change rules, especially in scenarios where some data types are forbidden, and then consider the change rules concerning the source data type and target data type.
Table-level column type change rules
Prohibitory rules
FOREIGN KEY constraints
The data type of a foreign key column cannot be changed by using online or offline DDL operations except in the following special cases. Note that only the display precision is changed and the storage structure is not changed.
If the data type of the column is
FLOAT(m,n), you can change the precision. However, conversion betweenSIGNEDandUNSIGNEDis not supported.If the data type of the column is
DOUBLE(m,n), you can change the precision. However, conversion betweenSIGNEDandUNSIGNEDis not supported.If the data type of the column is
VARCHAR(m), you can only increase the precision. You cannot decrease the precision or change the data type toCHAR.
CHECK constraints
In the MySQL mode of OceanBase Database, you cannot change the data types of columns with CHECK constraints by using online or offline DDL operations.
Trigger constraints
You cannot change the data type of a column with a non-
DISABLEtrigger constraint by using offline DDL operations. For a table with only aDISABLEtrigger, DDL operations are not affected, and the new table must contain thisDISABLEtrigger.
Change rules
For a variable-length data type, if you change the data type of a column from
CHARtoCHAR(11), data does not need to be rewritten becauseCHARdata stored in OceanBase Database supports online DDL operations. If an index is created on a column, you can change the length of this index column by using an online DDL operation. To specify the column as a primary key or partitioning key, perform an offline operation. In other words, to change the length of a column without changing the data type and specify the column as a primary key or partitioning key, perform offline DDL operations.For the variable-length
VARCHARdata type, you can change the data length by using an online DDL operation. However, you must also change the schemas of dependent objects such as the index table. In this case, the primary key or partitioning key can be changed by using an online DDL operation. In other words, even if the data does not change, the schema of the dependent object must still be changed.In OceanBase Database, the type change of a column may occur within a category of data types (such as numeric, character, and date and time data types). If this column is not referenced by an index table, foreign key, primary key, or partitioning key, you can perform online DDL operations to change the column type. Otherwise, you need to perform offline DDL operations to change the column type. In other words, offline DDL operations are required for column type change if any objects depend on the column.
When column type change is allowed, to convert between
SIGNEDandUNSIGNEDvalues of a numeric data type, perform an offline DDL operation. When column type change is allowed, to change the character set and collation, perform an offline DDL operation.If a column is referenced by a generated column, you can still change its data type.
Column-level conversion rules
In OceanBase Database, all column data types are classified into three categories: numeric data types, character data types, and date and time data types. The conversion rules are similar for the three categories. The change rules are also similar within each category. However, some special cases exist.
The following tables describe the conversion between categories and within each category.
Conversion between numeric data types
Numeric data types include the following ones:
Integer types:
TINYINT,SMALLINT,MEDIUMINT,INT, andBIGINTFloating-point types:
FLOATandDOUBLEFixed-point type:
NUMBERBinary type:
BIT
The following table describes the conversion between numeric data types.
| Data type | To TINYINT, SMALLINT, MEDIUMINT, INT, and BIGINT | To FLOAT and DOUBLE | To NUMBER | To BIT |
|---|---|---|---|---|
| Integer types | Supported | Supported | Supported | Supported |
| Floating-point types | Supported | Supported | Supported | Supported |
| Fixed-point type | Supported | Supported | Supported | Supported |
| Binary type | Supported | Supported | Supported | Supported |
Conversion between character data types
Character data types include the following ones:
CHARandVARCHARBINARYandVARBINARYTEXTtypes:TINYTEXT,MEDIUMTEXT,TEXT, andLONGTEXTBLOBtypes:TINYBLOB,MEDIUMBLOB,BLOB, andLONGBLOBENUMandSET
The following table describes the conversion between character data types.
| Data type | To CHAR | To BINARY | To VARCHAR and VARBINARY | To TEXT and BLOB | To ENUM and SET |
|---|---|---|---|---|---|
CHAR |
Supported | Supported | Supported | Supported | Supported |
BINARY |
Supported | Supported | Supported | Supported | Supported |
VARCHAR and VARBINARY |
Supported | Supported | Supported | Supported | Supported |
TEXT and BLOB types |
Supported | Supported | Supported | Supported | Supported |
ENUM and SET |
Supported | Supported | Supported | Supported | Supported |
Conversion between date and time data types
Date and time data types include the following ones:
DATETIMESTAMPDATETIMETIMEYEAR
The following table describes the conversion between date and time data types.
| Data type | To DATE | To TIMESTAMP | DATETIME | TIME | YEAR |
|---|---|---|---|---|---|
DATE |
Supported | Supported | Supported | Supported | Supported |
TIMESTAMP |
Supported | Supported | Supported | Supported | Supported |
DATETIME |
Supported | Supported | Supported | Supported | Supported |
TIME |
Supported | Supported | Supported | Supported | Supported |
YEAR |
Supported | Supported | Supported | Supported | Supported |
Conversion between numeric data types and character data types
The following table describes the conversion between numeric data types and character data types.
| Data type | To integer, floating-point, and fixed-point types | To BIT | To CHAR, BINARY, VARCHAR, VARBINARY, TEXT types, and BLOB types | To ENUM and SET |
|---|---|---|---|---|
| Integer, floating-point, and fixed-point types | See rules for conversion within the category | See rules for conversion within the category | Supported | Supported |
BIT |
See rules for conversion within the category | See rules for conversion within the category | Supported | Supported |
CHAR, BINARY, VARCHAR, VARBINARY, TEXT types, and BLOB types |
Supported | Supported | See rules for conversion within the category | Supported |
ENUM and SET |
Supported | Supported | See rules for conversion within the category | See rules for conversion within the category |
Conversion between numeric data types and date and time data types
The following table describes the conversion between numeric data types and date and time data types.
| Data type | To integer, floating-point, fixed-point, and BIT types | To DATE | To DATETIME | To TIMESTAMP | To TIME | To YEAR |
|---|---|---|---|---|---|---|
| Integer, floating-point, fixed-point, and BIT types | See rules for conversion within the category | Supported | Supported | Supported | Supported | Supported |
DATE |
Supported | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category |
DATETIME |
Supported | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category |
TIMESTAMP |
Supported | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category |
TIME |
Supported | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category |
YEAR |
Supported | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category |
Conversion between character data types and date and time data types
The following table describes the conversion between character data types and date and time data types.
| Data type | To CHAR, BINARY, VARCHAR, VARBINARY, TEXT types, and BLOB types | To ENUM and SET | To DATE | To DATETIME | To TIMESTAMP | To TIME | To YEAR |
|---|---|---|---|---|---|---|---|
CHAR, BINARY, VARCHAR, VARBINARY, TEXT types, and BLOB types |
See rules for conversion within the category | See rules for conversion within the category | Supported | Supported | Supported | Supported | Supported |
ENUM and SET |
See rules for conversion within the category | See rules for conversion within the category | Supported | Supported | Supported | Supported | Supported |
DATE |
Supported | Supported | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category |
DATETIME |
Supported | Supported | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category |
TIMESTAMP |
Supported | Supported | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category |
TIME |
Supported | Supported | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category |
YEAR |
Supported | Supported | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category | See rules for conversion within the category |