CHAR vs GRAPHIC in DB2

zchbaby2000發表於2015-12-30
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部落格 ” ,連結:http://blog.itpub.net/725820/viewspace-1968005/,如需轉載,請註明出處,否則將追究法律責任。

相關文章