修改Oracle數據庫字符集
前兩天安裝Oracle數據庫時忘記設置字符集了。等安裝完進行下一步測試時才發現字符集不對。
參考內容:
如何修改Oracle數據庫字符集
http://www.linuxidc.com/Linux/2012-07/65501.htm通常情況下,字符集是在安裝的時候選定好的,需要修改數據庫的字符集Oracle建議的做法是重建數據庫(EXP/EXPDP導出後再導入),
雖然Oracle官方文檔也有說如何修改字符集,但這不是Orale推薦的方法.下面是Oracle官方文檔中提到的如何修改字符集的方法.
1.查看當前數據庫字符集
select * from nls_database_parameters
where parameter='NLS_CHARACTERSET';
-----------------------------------
PARAMETER VALUE
NLS_CHARACTERSET WE8ISO8859P1
這裏的字符集WE8ISO8859P1是不支持中文的
2.將字符集修改爲中文字符集ZHS16GBK
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> STARTUP MOUNT;
ORACLE instance started.
Total System Global Area 734003200 bytes
Fixed Size 1221564 bytes
Variable Size 264244292 bytes
Database Buffers 465567744 bytes
Redo Buffers 2969600 bytes
Database mounted.
SQL> ALTER SYSTEM ENABLE RESTRICTED SESSION;
System altered.
SQL> ALTER DATABASE OPEN;
Database altered.
SQL> ALTER DATABASE CHARACTER SET ZHS16GBK;
ALTER DATABASE CHARACTER SET ZHS16GBK
*
ERROR at line 1:
ORA-12712: new character set must be a superset of old character set
SQL> ALTER DATABASE character set INTERNAL_USE ZHS16GBK;
Database altered.
SQL> SHUTDOWN IMMEDIATE;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.
Total System Global Area 734003200 bytes
Fixed Size 1221564 bytes
Variable Size 264244292 bytes
Database Buffers 465567744 bytes
Redo Buffers 2969600 bytes
Database mounted.
Database opened.
這樣將數據庫的字符集由原來的WE8ISO8859P1修改成了ZHS16GBK,對原來的字符集有中文的顯示的還是亂碼,但對新入庫的中文字符集就能正常顯示.
-- The End --
本人測試結果如下:
#登錄數據庫
[oracle@localhost data]$ sqlplus /nolog
SQL*Plus: Release 10.2.0.1.0 - Production on Mon Sep 14 18:53:39 2015
Copyright (c) 1982, 2005, Oracle. All rights reserved.
ORACLE instance started.
Total System Global Area 608174080 bytes
Fixed Size 1220820 bytes
Variable Size 171970348 bytes
Database Buffers 427819008 bytes
Redo Buffers 7163904 bytes
Database mounted.
Database opened.
#當前字符集是WE8ISO8859P1
SQL> select * from nls_database_parameters;
SQL> select userenv('language') from dual;
Database closed.
Database dismounted.
ORACLE instance shut down.
#啓動mount狀態
SQL> startup mount;
ORACLE instance started.
Total System Global Area 608174080 bytes
Fixed Size 1220820 bytes
Variable Size 171970348 bytes
Database Buffers 427819008 bytes
Redo Buffers 7163904 bytes
Database mounted.
SQL> ALTER SYSTEM ENABLE RESTRICTED SESSION;
System altered.
SQL> ALTER DATABASE OPEN;
Database altered.
SQL> ALTER DATABASE CHARACTER SET ZHS16GBK;
ALTER DATABASE CHARACTER SET ZHS16GBK
*
ERROR at line 1:
ORA-12712: new character set must be a superset of old character set
#修改成功
SQL> ALTER DATABASE character set INTERNAL_USE ZHS16GBK;
Database altered.
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.
Total System Global Area 608174080 bytes
Fixed Size 1220820 bytes
Variable Size 171970348 bytes
Database Buffers 427819008 bytes
Redo Buffers 7163904 bytes
Database mounted.
Database opened.
SQL> select userenv('language') from dual;
USERENV('LANGUAGE')
----------------------------------------------------
AMERICAN_AMERICA.ZHS16GBK
SQL> QUIT
---END
小結:
以後安裝時最好先選好字符集,避免出現修改字符集的情況出現。
畢竟當導入數據後才發現字符集不對,後續操作很麻煩的。需要刪除已導入的數據庫,修改字符集後重新再導入。
在創建數據庫的過程中,可以將數據庫的字符集設置爲Unicode UTF-8,防止出現亂碼。