在Trafodion数据库,当我们设定一个字段类型为字符类型的时候(如CHAR或VARCHAR),字符编码会以数据库默认的字符编码为准。
比如下面例子,创建有一个字符类型字段的表,通过SHOWDDL命令可以看到这个字段的编码方式为ISO88591。
SQL>drop table seabase.test_chinese;
create table seabase.test_chinese (rol_a varchar(10));
showddl seabase.test_chinese;
--- SQL operation complete.
SQL>
--- SQL operation complete.
SQL>
CREATE TABLE TRAFODION.SEABASE.TEST_CHINESE
(
ROL_A VARCHAR(10) CHARACTER SET ISO88591 COLLATE
DEFAULT DEFAULT NULL SERIALIZED
)
;
--- SQL operation complete.
此时如果想插入中文字符会报错,解决方法就是修改字符的编码类型为UTF8。
SQL>insert into seabase.test_chinese values('中国');
*** ERROR[8690] An invalid character value encountered in TRANSLATE function. [2016-10-25 06:22:47]
SQL>alter table seabase.test_chinese alter column rol_a varchar(10) character set utf8;
--- SQL operation complete.
现在我们再通过SHOWDDL查看字段属性已修改为UTF8类型,这时插入中文字符成功。
SQL>showddl seabase.test_chinese;
CREATE TABLE TRAFODION.SEABASE.TEST_CHINESE
(
ROL_A VARCHAR(10 CHARS) CHARACTER SET UTF8
COLLATE DEFAULT DEFAULT NULL SERIALIZED /*altered_col*/
)
;
--- SQL operation complete.
SQL>insert into seabase.test_chinese values('中国');
--- 1 row(s) inserted.
SQL>select * from seabase.test_chinese;
ROL_A
----------------------------------------
中国
--- 1 row(s) selected.
当然我们也可以在建表的时候直接定义字段类型为UTF8,方法如下:
SQL>create table test_chinese(rol_a varchar(10) character set utf8);
--- SQL operation complete.
SQL>showddl test_chinese;
CREATE TABLE TRAFODION.SEABASE.TEST_CHINESE
(
ROL_A VARCHAR(10 CHARS) CHARACTER SET UTF8
COLLATE DEFAULT DEFAULT NULL NOT SERIALIZED
)
ATTRIBUTES ALIGNED FORMAT
;
--- SQL operation complete.