Summary of Oracle Character Set Problems

Source: Internet
Author: User
Tags ultraedit

Some colleagues often ask questions about character sets in Oracle databases, such as data migration in different databases and data exchange with other systems, migration failure or garbled data in the database is often caused by different character sets. Now, I will summarize some knowledge about Oracle character sets and hope to help you in your future work.

1. What is the Oracle character set?

The Oracle character set is a collection of symbols for the interpretation of byte data. It can be divided into different sizes and have an inclusive relationship. Oracle supports the national language architecture, allowing you to store, process, and retrieve data in a localized language. It makes database tools, error messages, sorting order, date, time, currency, numbers, and calendar automatically adapt to localization languages and platforms.

The most important parameter that affects the character set of Oracle databases is the nls_lang parameter. The format is as follows:

Nls_lang = language_territory.charset

It has three components (language, region, and Character Set), each of which controls the NLS subset features. Where:

Language specifies the language of the server message, territory specifies the date and number format of the server, and charset specifies the character set. For example: American _ America. zhs16gbk

From the composition of nls_lang, we can see that the real impact on the database character set is actually the third part. Therefore, if the character set between the two databases is the same as that in the third part, data can be imported and exported to each other. The preceding information is only prompted in Chinese or English.

Ii. How to query Oracle character sets

Many people have encountered data import failures due to different character sets. This involves three character sets: one is the character set on the El server side, the other is the character set on the Oracle client side, and the other is the DMP file character set. During data import, the three character sets must be consistent before the data can be correctly imported.

1. query character sets of ORACLE Server

There are many ways to find out the character set of the Oracle server. The intuitive query method is as follows: SQL> select userenv ('language') from dual;

The results are as follows: American _ America. zhs16gbk

2. How to query the DMP file Character Set

The DMP file exported using Oracle's exp tool also contains character set information. The 2nd and 3rd bytes of the DMP file record the character set of the DMP file. If the DMP file is not large, for example, only a few MB or dozens of MB, you can use ultraedit to open it (in hexadecimal mode) and view the content of 2nd 3rd bytes, such as 0354, then, use the following SQL statement to find the corresponding character set:

SQL> select nls_charset_name (to_number ('20140901', 'xxxxx') from dual;

Zhs16gbk

If the DMP file is large, for example, 2 GB or above (this is also the most common case), you can use the following command (on a unix host) to open it slowly or completely ):

Cat exp. dmp | OD-x | head-1 | awk '{print $2 $3}' | cut-C 3-6

Then, you can use the preceding SQL statement to obtain its character set.

3. query the character set of the Oracle client

This is relatively simple. On Windows, it is the nls_lang of oraclehome in the registry. You can also set it in the DOS window, for example:

Set nls_lang = american_america.zhs16gbk

In this way, only the environment variables in this window are affected.

On UNIX platforms, the environment variable nls_lang is used.

$ Echo $ nls_lang

American_america.zhs16gbk

If the check result shows that the character sets on the server and client are inconsistent, change them to the same character set on the server.

3. Modify the character set of Oracle

As mentioned above, Oracle character sets have an inclusive relationship. For example, us7ascii is a subset of zhs16gbk. From us7ascii to zhs16gbk, there will be no data interpretation problems or data loss. Utf8 should be the largest among all character sets because it is based on Unicode and stores double-byte characters (so it occupies more space ).

Once a database is created, the character set of the database cannot be changed theoretically. Therefore, it is important to consider which character set to use at the beginning of design and installation. According to the official instructions of Oracle, Character Set conversion is from a subset to a superset, but not vice versa. If there is no relationship between Subsets and supersets between the two character sets, Character Set conversion is not supported by Oracle. For database servers, incorrect Character Set modification may lead to many unpredictable consequences, which may seriously affect the normal operation of the database, therefore, before modification, check whether the two character sets have the relationship between Subsets and supersets. Generally, we do not recommend that you modify the character set of the Oracle database server unless you have. In particular, the two most commonly used character sets zhs16gbk and zhs16cgb231280 do not have a subset or superset relationship. Therefore, in theory, mutual conversion between these two character sets is not supported.

1. Modify the server character set (not recommended)

Before Oracle 8, you can directly modify the data dictionary table props $ to change the character set of the database. However, after oracle8, at least three system tables record the information of the database character set. modifying only the props $ table is incomplete and may cause serious consequences. The correct modification method is as follows:

$ Sqlplus/nolog

SQL> Conn/As sysdba;

If the database server has been started, run the shutdown immediate command to shut down the database server, and then run the following command:

SQL> startup Mount;

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;

   SQL> alter database character set zhs16gbk;

   SQL> alter database National Character Set zhs16gbk;

SQL> shutdown immediate;

SQL> startup

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;
SQL> select value from nls_database_parameters where parameter = 'nls _ characterset ';
# Use internal_use to skip the superset check. Alter database character set internal_use

2. Modify the DMP file Character Set

As mentioned above, the 2nd 3rd bytes of the DMP file records the character set information. Therefore, you can directly modify the 2nd 3rd bytes of the DMP file to 'Cheat 'the Oracle check. In theory, this can be modified only from the subset to the superset, but in many cases it can be modified without the subset and superset relationships. Some of our commonly used character sets, such as us7ascii, we8iso8859p1, zhs16cgb231280, and zhs16gbk can be modified. Because only the DMP file is changed, it has little impact.

There are many specific modification methods. The simplest is to directly use ultraedit to modify the 2nd and 3rd bytes of the DMP file. For example, if you want to change the DMP file's character set to zhs16gbk, you can use the following SQL statement to find the hexadecimal code corresponding to this character set:

SQL> select to_char (nls_charset_id ('zhs16gbk'), 'xxx') from dual;

0354

Modify the 2 and 3 bytes of the DMP file to 0354.

If the DMP file is large and cannot be opened with ue, you need to use the program method. Some people on the internet use Java stored procedures to write the Conversion Program (the advantage of using Java stored procedures is that the versatility is good, but the disadvantage is relatively troublesome ). I passed the test in windows. However, the JVM option must be installed for Oracle databases.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.