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
For an integer column with a
CHECKconstraint, you can use an online DDL operation to change the data type to another integer data type that supports a wider value range, such as from INT to BIGINT. However, you cannot change the data type of other columns with aCHECKconstraint.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
To change the length of a column without changing its data type, if the column is just an index column, you can perform an online DDL operation. For example, you can change a
CHARcolumn to aCHAR(11)column online. Since OceanBase Database can storeCHARdata online, data rewriting is not required in this example. If the column is a primary key column or a partitioning key column, you need to perform an offline DDL operation. In other words, to change the length of a primary key column or a partitioning key column without changing its data type, perform an offline DDL operation.To change the length of a
VARCHARcolumn, you can perform an online DDL operation. However, you must also change the schema of dependent objects such as the index table. Note that an online DDL operation can be performed in this case to change the column length even though the column is used as a primary key or partitioning key. In other words, even if the data does not change, the schema of the dependent object must still be changed.To change the length of an integer column, you can perform an online DDL operation. The column can be a common column, a primary key column, a partitioning key column, an index column, a column that a generated column depends on, or a column with a
CHECKconstraint.
In OceanBase Database, to change the data type of a numeric column, character column, or date and time column, you can perform an online DDL operation if this column is not referenced by an index table, foreign key, primary key, or partitioning key. If the column is referenced by a dependent object, you need to perform an offline DDL operation. In other words, offline DDL operations are required for changing the data type of columns on which any objects depend.
In OceanBase Database, if an integer column is a foreign key column, a column on which a foreign key depends, or a generated column, you cannot change the column type.
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 |