ITPub博客

首页 > 数据库 > 数据库开发技术 > CHAR vs GRAPHIC in DB2

CHAR vs GRAPHIC in DB2

原创 数据库开发技术 作者:zchbaby2000 时间:2015-12-30 22:01:40 0 删除 编辑
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类型,你很可能截取到的字符是个乱码

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/725820/viewspace-1968005/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: DB2 SQL复制知识点
请登录后发表评论 登录
全部评论
喜欢各种数据库相关的开发,架构设计,运维和性能优化
  • 博文量
    260
  • 访问量
    122446