ALTER TABLE MODIFY COLUMN 的操作不允许把一个非字符类型的字段修改为字符类型,会报如下错误。
ERROR 1235 (0A000): Alter non string type not supported
适用版本
OceanBase 数据库 V4.x 版本。
操作方法
一种变通的方法来把非字符类型的字段修改为字符类型。
该方法的思路如下:
在表中新增 varchar 字段(col_new)。
用需要转换的字段(col_old)去更新 col_new。
删除 col_old。
更改字段名 col_old 为 col_new。 MySQL 模式下的测试示例:
创建测试表。
obclient [gtx]> create table t1 (c1 int,c2 varchar(100),c3 int, key t1_ix_c3(c3)); Query OK, 0 rows affected (0.128 sec)尝试把非字符类型的字段 c3 修改为 varchar。
obclient [gtx]> alter table t1 modify column c3 varchar(100); ERROR 1235 (0A000): Not supported feature or function添加一个新的 varchar 字段 c3_new。
obclient [gtx]> alter table t1 add c3_new varchar(100); Query OK, 0 rows affected (0.091 sec)用旧字段 c3 的值去更新新字段 c3_new。
obclient [gtx]> update t1 set c3_new=c3; Query OK, 0 rows affected (0.028 sec) Rows matched: 0 Changed: 0 Warnings: 0在删除旧字段 c3 前先要删除其上的索引。
obclient [gtx]> alter table t1 drop column c3; ERROR 5071 (42000): Cannot alter index columnobclient [gtx]> drop index t1_ix_c3 on t1; Query OK, 0 rows affected (0.051 sec)删除旧字段。
obclient [gtx]> alter table t1 drop column c3; Query OK, 0 rows affected (0.055 sec)把新字段重命名为旧字段。
obclient [gtx]> alter table t1 change column c3_new c3 varchar(100); Query OK, 0 rows affected (0.047 sec)重建删除的索引。
obclient [gtx]> create index t1_ix_c3 on t1 (c3); Query OK, 0 rows affected (0.564 sec)obclient [gtx]> desc t1;输出结果如下:
+-------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | c1 | int(11) | YES | | NULL | | | c2 | varchar(100) | YES | | NULL | | | c3 | varchar(100) | YES | | NULL | | +-------+--------------+------+-----+---------+-------+