CHAR vs GRAPHIC in DB2

db2 => describe table test

                                Data type                     Column
Column name                     schema    Data type name      Length     Scale Nulls
------------------------------- --------- ------------------- ---------- ----- ------
NAME                            SYSIBM    GRAPHIC                     10     0 Yes   

  1 record(s) selected.

db2 => describe table test1

                                Data type                     Column
Column name                     schema    Data type name      Length     Scale Nulls
------------------------------- --------- ------------------- ---------- ----- ------
NAME                            SYSIBM    CHARACTER                   10     0 Yes   

  1 record(s) selected.

如果插入英文字母,那么都最多可以插入10个
db2 => insert into test values('helloworld')
DB20000I  The SQL command completed successfully.
db2 => insert into test1 values('helloworld')
DB20000I  The SQL command completed successfully.

插入汉字,最多可以插入几个那?
db2 => insert into test values('插入十个汉字试试看吧')
DB20000I  The SQL command completed successfully.
db2 => insert into test1 values('插入十个汉字试试看吧')
DB21034E  The command was processed as an SQL statement because it was not a
valid Command Line Processor command.  During SQL processing it returned:
SQL0433N  Value "插入十个汉字试试看吧" is too long.  SQLSTATE=22001

GRAPHIC类型,那么那个10就表示个数,无论是英文,汉字,日文,都是指的个数
CHAR类型定义的那个10表示的是字节数,在UTF-8的编码下,一个汉字占用3个字节
db2 => insert into test1 values('插入十g')
DB20000I  The SQL command completed successfully.
db2 => insert into test1 values('插入十gh')
DB21034E  The command was processed as an SQL statement because it was not a
valid Command Line Processor command.  During SQL processing it returned:
SQL0433N  Value "插入十gh" is too long.  SQLSTATE=22001

GRAPHIC类型有个好处,比如你用substr,你可以截取到整个的字符,无论是什么字符,都是整个的
但是CHAR类型,你很可能截取到的字符是个乱码

请使用浏览器的分享功能分享到微信等