首页 > 代码库 > 论Oracle字符集“转码”过程
论Oracle字符集“转码”过程
本文将通过实验来演示一下Oracle字符集“转码”的确认过程。
1.实验环境说明
客户端是Windows XP操作系统的SQL*Plus程序,客户端字符集是936(对应Oracle的ZHS16GBK字符集);
数据库版本是Oracle 10g,数据库字符集是AL32UTF8;
NLS_LANG参数将在实验中进行指定。
1)确认客户端字符集
C:\>chcp
活动代码页: 936
注释:936对应Oracle的ZHS16GBK字符集。
2)查看数据库版本信息:
sec@ora10g> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi
PL/SQL Release 10.2.0.3.0 - Production
CORE 10.2.0.3.0 Production
TNS for Linux: Version 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - Production
3)确认数据库的字符集:
sec@ora10g> col PARAMETER for a20
sec@ora10g>col valuefor a20
sec@ora10g>select *from v$nls_parameters where parameter = ‘NLS_CHARACTERSET‘;
PARAMETER VALUE
-------------------- --------------------
NLS_CHARACTERSET AL32UTF8
2.实验中将会涉及到的两种场景
“转码”场景:设置客户端的NLS_LANG与客户端字符集一致,这里是ZHS16GBK;
“非转码”场景:设置客户端的NLS_LANG与数据库服务器端字符集一致,此处是AL32UTF8.
3.创建实验表T
sec@ora10g> create table t (x number(1), client_characterset varchar2(10),nls_lang varchar2(10), database_characterset varchar2(10), y varchar2(10));
Tablecreated.
sec@ora10g> desc t;
Name Null? Type
----------------------------------- -------- ------------------------
X NUMBER(1)
CLIENT_CHARACTERSET VARCHAR2(10)
NLS_LANG VARCHAR2(10)
DATABASE_CHARACTERSET VARCHAR2(10)
Y VARCHAR2(10)
T表包含五个字段,分表表示序号、客户端字符集、客户端NLS_LANG设置情况以及数据库服务器字符集设置情况。
4.两种NLS_LANG设置方法下分别插入一条数据
1)当客户端的NLS_LANG设置为ZHS16GBK时,我们插入第一条记录(“转码”场景)。
C:\>setnls_lang=AMERICAN_AMERICA.ZHS16GBK
C:\>sqlplus zj/zj@orcl
SQL*Plus: Release 10.2.0.3.0 - Production on Fri Feb 5 19:21:312010
Copyright (c) 1982, 2006, Oracle. All Rights Reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning and Data Mining options
sec@ora10g> insert into t values (1,‘ZHS16GBK‘,‘ZHS16GBK‘,‘AL32UTF8‘,‘圣‘);
1 rowcreated.
sec@ora10g> commit;
Commit complete.
2)当客户端的NLS_LANG设置为AL32UTF8时,我们插入第二条记录(“非转码”场景)。
C:\>setnls_lang=AMERICAN_AMERICA.AL32UTF8
C:\>sqlpluszj/zj@orcl
SQL*Plus: Release 10.2.0.3.0 - Production on Fri Feb 5 20:41:152010
Copyright (c) 1982, 2006, Oracle. All Rights Reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning and Data Mining options
sec@ora10g> insert into t values (2,‘ZHS16GBK‘,‘AL32UTF8‘,‘AL32UTF8‘,‘圣‘);
1 row created.
sec@ora10g> commit;
Commit complete.
5.两种NLS_LANG设置方法下分别查看刚刚插入的两条数据
1)当客户端的NLS_LANG设置为ZHS16GBK时(“转码”场景)。
sec@ora10g> col x for 9
sec@ora10g> col CLIENT_CHARACTERSET for a8
sec@ora10g> col NLS_LANG for a8
sec@ora10g> col DATABASE_CHARACTERSET for a8
sec@ora10g> col y for a4
sec@ora10g> col dump for a50
sec@ora10g> select t.*,dump(y,1016) dump from t order by 1;
X CLIENT_C NLS_LANG DATABASEY DUMP
-- -------- -------- -------- ------------------------------------------------------
1 ZHS16GBK ZHS16GBK AL32UTF8 圣 Typ=1 Len=3 CharacterSet=AL32UTF8: e5,9c,a3
2 ZHS16GBK AL32UTF8 AL32UTF8 ? Typ=1 Len=2 CharacterSet=AL32UTF8: ca,a5
2)当客户端的NLS_LANG设置为AL32UTF8时(“非转码”场景)。
sec@ora10g> col x for 9
sec@ora10g> col CLIENT_CHARACTERSET for a8
sec@ora10g> col NLS_LANG for a8
sec@ora10g> col DATABASE_CHARACTERSET for a8
sec@ora10g> col y for a4
sec@ora10g> col dump for a50
sec@ora10g> select t.*,dump(y,1016) dump from t order by 1;
X CLIENT_C NLS_LANG DATABASEY DUMP
-- -------- -------- -------- ------------------------------------------------------
1 ZHS16GBK ZHS16GBK AL32UTF8 鍦? Typ=1 Len=3 CharacterSet=AL32UTF8: e5,9c,a3
2 ZHS16GBK AL32UTF8 AL32UTF8 圣 Typ=1 Len=2 CharacterSet=AL32UTF8: ca,a5
实验结论
1)如果有可能,尽量保证客户端编码(Windows XP的cmd工具可以使用chcp命令来确认)、NLS_LANG参数和数据库字符集这三个内容一致,这样设置,无论是从性能上,还是从防止编码转换上都是最佳的;
2)如果目的是支持中文,数据库Server端的字符集应该尽量选择ZHS16GBK或AL32UTF8字符集,这样可以减少因不当的“转码”导致的字符乱码故障;
3)(推荐)可已将NLS_LANG参数与操作终端字符编码一致,这样可以保证数据库能正确获得应用终端使用的编码,这时会发生“编码转换”,但是,这样就可以保证正确转码,可以防止错误的编码存入数据库;
论Oracle字符集“转码”过程