oracle11g更改数据库字符集

oracle更改字符集说明

方法1,通过csalter.sql、csscan命令

   - 演示环境说明

      假设我们安装已经安装好了一个数据库,字符集为ZHS16GBK,库已经运行了一段时间。由于开发需求,需要将数据库服务器的端的字符集转换为AL32UTF8,下面开始演示步骤:

      源数据库信息:
       server character: AMERICAN_AMERICA.ZHS16GBK
NLS_LANG: AMERICAN_AMERICA.UTF8

      目标数据库信息:
       server character:
NLS_LANG: AMERICAN_AMERICA.UTF8

   - 配置相应的环境变量和执行相应的准备脚本

      1) 查看和设置oracle客户端的环境变量NLS_LANG

[oracle@oracledb ~]$ export NLS_LANG=AMERICAN_AMERICA.UTF8

      2).设置LD_LIBRARY_PATH

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib:/usr/lib

      注:不设置会出现, csscan: error while loading shared libraries: libclntsh.so.11.1: cannot open shared object file: No such file or directory

      3).执行csminst.sql脚本,安装csscan,csalter

[oracle@oracledb ~]$ sqlplus /nolog
SQL> conn / as sysdba
SQL> @?/rdbms/admin/csminst.sql

      注:不执行,会出现CSS-00107: Character set migration utility schema not installed

   - 创建演示用户,并且插入演示数据

SQL> select userenv('language') from dual;
USERENV('LANGUAGE')
----------------------------------------------------
AMERICAN_AMERICA.ZHS16GBK
SQL> create user cnmef identified by cnmef ;
SQL> grant dba to cnmef;
SQL> conn cnmef/cnmef;
SQL> create table test(id number,name varchar2(64));
SQL> insert into test values (1,'柯凡');
SQL> commit;
SQL> select * from test;
    ID              NAME
---------- ------------------------------
 1              柯凡

   - 执行csscan命令,设置相应的字符集转换规则

[oracle@oracledb ~]$ csscan system/oracle  

(1)Full database, (2)User, (3)Table, (4)Column: 1 > 1

Current database character set is ZHS16GBK.

Enter new database character set name: > AL32UTF8

...

Creating Database Scan Summary Report...

Creating Individual Exception Report...

Scanner terminated successfully.

   - 检查scan.txt中的内容

      检查scan.txt中的内容,确保Convertible列中的值为0,如果不为0,导出数据库中的对应的表,然后drop这些表。
      1)导出Convertible不为空的表

SQL> create directory  UPLOAD_DIR as '/upload';
SQL> grant read,write on directory UPLOAD_DIR to cnmef;

[oracle@oracledb ~]$ expdp  cnmef/cnmef directory=UPLOAD_DIR dumpfile=cnmefdp0614.dmp tables='test'

      2)drop表

SQL> conn cnmef/cnmef
SQL> drop table  test;

      3)重新执行csscan命令

[oracle@oracledb ~]$ csscan system/oracle FULL=y   TOCHAR=AL32UTF8 TONCHAR=AL16UTF16 PROCESS=1 ARRAY=1024000

      *注:确保csscan.txt中不要有Convertible为非0或者非空的值,如果不住行这一步,执行csalter.plb时会出现
“Checking or Converting phase did not finish successfully
No database (national) character set will be altered
CSALTER finished unsuccessfully”错误.*

   - 以RESTRICT模式启动数据库

SQL> shutdown immediate;
SQL> startup restrict;

   - 执行csalter.sql完成数据库的转换

[oracle@oracledb ~]$ sqlplus / as sysdba
SQL>  @?/rdbms/admin/csalter.plb
Would you like to proceed (Y/N)?Y
old   6: if (UPPER('&conf') <> 'Y') then
new   6: if (UPPER('Y') <> 'Y') then
Checking data validity...
begin converting system objects
PL/SQL procedure successfully completed.
Alter the database character set...
Alter the national database character set...

   - 重新启动数据库,查看字符集是否设置成功

SQL> shutdown immediate;
SQL> startup
SQL> select userenv('language') from dual;
USERENV('LANGUAGE')
AMERICAN_AMERICA.AL32UTF8
SQL>

   - 导入相应的dmp文件

impdp cnmef/cnmef directory=UPLOAD_DIR dumpfile=cnmefdp0614.dmp

方法2:通过alter database修改

   - 获取当前数据库的charcter set

SQL> select userenv('language') from dual;
USERENV('LANGUAGE')
AMERICAN_AMERICA.AL32UTF8
SQL>

   - altersystem 修改数据库的参数

SQL>  shutdown  immediate;
SQL>  startup mount;
SQL>  alter session set sql_trace=true;
SQL>  alter system enable restricted session; 
SQL>  alter system set job_queue_processes=0; 
SQL>  alter system set aq_tm_processes=0; 
SQL>  alter database open; 
    /* ----->gbk    ZHS16GBK*/
    /*------>utf8   AL32UTF8*/
SQL>  ALTER DATABASE character set INTERNAL_USE ZHS16GBK

   - 重启数据库检查数据库的字符编码设置

SQL> shutdown immediate;
SQL> startup
SQL> select userenv('language') from dual;
USERENV('LANGUAGE')
--------------------------------------------------------------------
AMERICAN_AMERICA.ZHS16GBK

你可能感兴趣的:(数据库)