ORACLE字符集 总结

 

在参考了oracle10g的安装文档和网友的文章后,今天总算把oracle10g的字符集问题基本弄懂了,借这个平台做个总结。虽然网上这样讲解oracle字符集的文章到处都有,但我觉得还是有必要写一篇,毕竟是我自己的理解。特别是对刚刚接触oracle的人来说应该还是很有帮助的。当然这里面可能还存在问题,希望大家有什么意见可以留言,然后我们再一起探讨、修改,共同进步。

 

系统环境:   Solaris10 x86版本

Oracle版本: oracle 10.2.0.2

 

ORACLE字符集

首先需要了解一些字符集的基础知识,在我的一篇 初识字符集 的文章里有介绍。

然后再说说oracle的字符集表示方法。

Oracle中字符集的表示方法为language_territory.characterset

oracle10g的安装文档  Oracle® Database Installation Guide

里有如下解释:

Configuring Oracle Database Globalization Support(配置oracle数据库全球化支持)

Parameter

Description

language

Specifies the language used for displaying Oracle messages, sorting, day names, and month names.

这一部分描述服务器端的一些信息的语言,如终端SQL*Plus中的提示信息

territory

Specifies the conventions for default date, monetary and numeric formats.

这一部分主要是关于国家字符集中常用默认日期、数字格式

characterset

Specifies the encoding used by the client application (normally the Oracle character set that corresponds to the character set of the user terminal or the operating system)

这一部分才是重点,就是oracle中数据真正编码用到的字符集

Oracle的字符集关键就是搞懂一个环境变量:NLS_LANG

来看看官网上对这个环境变量的描述

About NLS_LANG Environment Variable

Oracle provides Globalization Support that enables users to interact with a database in their preferred locale and character set settings. Setting the NLS_LANG environment variable specifies locale behavior for Oracle software.

 It sets the language and territory used by the client application and the database server. It also sets the character set for entering and displaying data by a client program, such as SQL*Plus.

NLS_LANG is an environment variable that specifies the locale behavior for Oracle software. This variable sets the language and territory used by the client application and the database server. It also sets the character set for the client, which is the character set for data entered or displayed by an Oracle client program, such as SQL*Plus.

NLS_LANG是用来告知服务器端,我客户端使用的是什么语言和什么字符集,当客户端存储数据时,服务器就可以判断是否要对数据编码进行转换。当客户端请求数据时返回何种提示语言的编码给客户端。

 

The NLS_LANG environment variable uses the following format: 格式如下

              NLS_LANG=language_territory.characterset

 

The NLS_LANG environment variable is set as a local environment variable for the shell on all UNIX-based platforms. For example, if the operating system locale setting is en_US.UTF-8

then the corresponding NLS_LANG environment variable should be set to AMERICAN_AMERICA.AL32UTF8.

在基于UNIX的平台上(Unix/Linux),我们可以用命令查看Shell正在使用的字符集,LANG表示。

#locale  显示当前系统或说是Shell正在使用的字符集环境变量LANG=en_us.utf-8当然这是英文系统的结果。这时就需要设置NLS_LANG = American_america.al32utf8 这样就可以正常的存储和查询汉字。

注意:oracle里面的utf8编码就表示为AL32utf8,它与带短横线的utf-8是不同的,下面一段话说明了此问题。

AL32UTF8 is the Oracle Database character set that is appropriate for XMLType data. It is equivalent to the IANA registered standard UTF-8 encoding, which supports all valid XML characters.

AL32UTF8很完美的支持XML类型的数据

Do not confuse Oracle Database database character set UTF8 (no hyphen) with database character set AL32UTF8 or with character encoding UTF-8. 

Database character set UTF8 has been superseded by AL32UTF8. Do not use UTF8 for XML data. UTF8 supports only Unicode version 3.1 and earlier; it does not support all valid XML characters. AL32UTF8 has no such limitation.

AL32UTF8UTF8的超集,不要用UTF8字符集来存储XML数据,很可能发生严重异常

Using database character set UTF8 for XML data could potentially cause a fatal error or affect security negatively. If a character that is not supported by the database character set appears in an input-document element name, 

a replacement character (usually "?") is substituted for it. This will terminate parsing and raise an exception.

下面来说说NLS_LANG环境变量工作的原理。

NLS_LANG实际上定义了oracle中的这样几个环境变量,NLS_LANGUAGE NLS_TERRITORY、和NLS_CHARACTERSET.  oracle10g数据库的创建中,有字符集的选项,这个地方最好是选择AL32UTF8方便以后数据库的迁入迁出,虽然占用的字节数会稍微多一点。

 


默认语言用来设置数据库的显示语言,与数据存储无关,国家字符集邮UTF-16UTF-8两种,影响的是环境变量NLS_NCHAR_CHARACTERSET

 

有个问题选择还没有具体弄清楚,当不设置环境变量NLS_LANG时,查询NLS参数在v$nls_parameters nls_database_parameters中的值是不一样的。这样造成了SQL*PLUS的的提示语言和数据存储都出现问题。

 

当建立数据库时选择的是AL32UTF8字符集时,在配置NLS_LANG时最后一项必须是al32utf8

否则进入SQL*PLUS是会提示无法读取NLS参数文件。前面两项则只是修改显示界面的语言。比如。查询到系统的字符集为zh_CN.utf-8 时,若想将SQL*PLUS的提示语言也设置为中文,则配置

NLS_LANG=Simplified/ Chinese_China.Al32utf8

这样提示语言为中文。若想将提示语言改为英文,则配置

NLS_LANG=American_America.Al32utf8

数据的存储和查询两种方法都是可行的。

关于其他一些字符集的配置方案,我觉得只需要将oracle的字符集显示与存储的原理搞清楚,那么怎么变化都是可以的。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值