你好,游客 登录 注册 搜索
背景:
阅读新闻

Oracle数据库多语言支持

[日期:2011-08-05] 来源:Linux社区  作者:fw0124 [字体:大 中 小]
以下情况建议使用Unicode Data Type
Situation Explanation

You want to add multilingual support incrementally.
逐步增加多语言支持

If you want to add Unicode support to the existing database without migrating the character set, then consider using Unicode data types to store Unicode data. You can add columns of the SQLNCHAR data types to existing tables or new tables to support multiple languages incrementally.

You want to build a packaged application.
提供组件给用户

If you are building a packaged application to sell to customers, then you may want to build the application using SQLNCHAR data types. The SQLNCHAR data type is a reliable Unicode data type in which the data is always stored in Unicode, and the length of the data is always specified in UTF-16 code units. As a result, you need to test the application only once. The application will run on customer databases with any database character set.

You want better performance with single-byte database character sets.

If performance is your main concern, then consider using a single-byte database character set and storing Unicode data in the SQLNCHAR data types.如果效率很重要,考虑使用single-byte的数据库字符集,Unicode数据使用NCHAR的数据类型存储。

You require UTF-16 support in Windows clients.
Windows客户端使用UTF-16

If your applications are written in Visual C/C++ or Visual Basic running on Windows, then you may want to use the SQLNCHAR data types. You can store UTF-16 data in SQLNCHAR data types in the same way that you store it in thewchar_t buffer in Visual C/C++ andstring buffer in Visual Basic. You can avoid buffer overflow in client applications because the length of thewchar_t andstring data types match the length of the SQLNCHAR data types in the database.


======================================================================================

在需要支持多国语言,包括文档存储的应用中,最好的办法是Unicode Database+ Unicode Data Type的方式。

实验环境:
NLS_CHARACTERSET = AL32UTF8
NLS_NCHAR_CHARACTERSET = AL16UTF16

测试对中日韩三种语言进行支持,

字符串 UTF8编码 UTF16编码
中国 E4 B8 AD, E5 9B BD 4E2D, 56FD
ペン E3 83 9A, E3 83 B3 30DA, 30F3
?? ED 95 9C, EA B8 80 D55C, AE00


create table tbl_varchar(col varchar2(6));
insert into tbl_varchar values('中国');
insert into tbl_varchar values('ペン');
insert into tbl_varchar values('??');
commit;

查看数据库中存储的数据,
select dump(col, 16) from tbl_varchar;
DUMP(COL,16)
-------------------------------
Typ=1 Len=6: e4,b8,ad,e5,9b,bd
Typ=1 Len=6: e3,83,9a,e3,83,b3
Typ=1 Len=6: ed,95,9c,ea,b8,80
可以看到是以UTF8格式存储,每个字符都占用了3个字节。
下面验证varchar2的长度是按照字节指定的。
insert into tbl_varchar values('中国A');
执行后,得到错误结果:
SQL Error: ORA-12899: value too large for column "HR"."TBL_VARCHAR"."COL" (actual: 7, maximum: 6)
'中国A'虽然是3个字符,但是如果用UTF8编码,是7个字节。

create table tbl_nvarchar(col nvarchar2(2));
insert into tbl_nvarchar values('中国');
insert into tbl_nvarchar values('ペン');
insert into tbl_nvarchar values('??');
commit;

查看数据库中存储的数据,
select dump(col, 16) from tbl_nvarchar;
DUMP(COL,16)
------------------------
Typ=1 Len=4: 4e,2d,56,fd
Typ=1 Len=4: 30,da,30,f3
Typ=1 Len=4: d5,5c,ae,0

可以看到是以UTF16格式存储,每个字符都占用了2个字节。

接下来尝试一下中文排序功能。
select value from v$nls_valid_values where parameter='SORT' and value like '%SCHINESE%';
返回可用的中文排序方法
SCHINESE_PINYIN_M    -- 按照拼音排序
SCHINESE_STROKE_M -- 按照笔划(第一顺序)、部首(第二顺序)排序
SCHINESE_RADICAL_M -- 按照部首(第一顺序)、笔划(第二顺序)排序

先插入数据,
delete from tbl_nvarchar;
insert into tbl_nvarchar values('荀彧');
insert into tbl_nvarchar values('荀攸');
insert into tbl_nvarchar values('蒋干');
insert into tbl_nvarchar values('董卓');
insert into tbl_nvarchar values('刘备');
insert into tbl_nvarchar values('刘表');
insert into tbl_nvarchar values('郭嘉');

按照拼音排序结果:
alter session set nls_sort='SCHINESE_PINYIN_M';
select * from tbl_nvarchar order by col;
COL
---
董卓
郭嘉
蒋干
刘备
刘表
荀攸
荀彧

按照笔画排序结果:
alter session set nls_sort='SCHINESE_STROKE_M';
select * from tbl_nvarchar order by col;
COL
---
刘表
刘备
荀攸
荀彧
郭嘉
董卓
蒋干

按照部首排序结果:
alter session set nls_sort='SCHINESE_RADICAL_M';
select * from tbl_nvarchar order by col;
COL
---
刘表
刘备
荀攸
荀彧
董卓
蒋干
郭嘉 linux
相关资讯       Oracle教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款