SQL Server 2005 中的 Unicode 支持
Jun 07, 2016 pm 03:04 PMUnicode 支持 是 SQL Server 2005 中多語言 支持 的基
Unicode 支持是 SQL Server 2005 中多語言支持的基礎(chǔ)。Unicode 是由 Unicode Consortium(一個(gè)提倡為所有語言使用單一字符集的組織)創(chuàng)立的一項(xiàng)標(biāo)準(zhǔn)。SQL Server 2005 支持 Unicode 標(biāo)準(zhǔn) 3.2 版。Unicode 標(biāo)準(zhǔn)的 3.01 版與 ISO-10646(一項(xiàng)與 Unicode 中的所有碼位均相符的國際標(biāo)準(zhǔn))完全相同。
Unicode 的工作方式是,為每個(gè)字符提供一個(gè)唯一的碼位,該碼位與平臺、程序或語言無關(guān)。支持 Unicode 的程序可以處理任何語言的數(shù)據(jù)。因?yàn)槠湓O(shè)計(jì)宗旨是涵蓋世界上所有語言的所有字符,所以不需要讓不同的代碼頁來處理不同的字符集。
因?yàn)樗?Unicode 系統(tǒng)都統(tǒng)一使用相同的位模式來表示所有字符,所以從一個(gè)系統(tǒng)轉(zhuǎn)到另一個(gè)系統(tǒng)時(shí),不會出現(xiàn)字符轉(zhuǎn)換不正確的問題。
管理國際數(shù)據(jù)庫中的字符數(shù)據(jù)的最簡單方法是始終使用 Unicode nchar、nvarchar 和 nvarchar(max) 數(shù)據(jù)類型,而不使用它們對應(yīng)的非 Unicode 數(shù)據(jù)類型:char、varchar 和 text。這樣,客戶端與所有其他客戶端所看到的數(shù)據(jù)中的字符將是相同的。如果所有使用國際數(shù)據(jù)庫的應(yīng)用程序還使用 Unicode 變量來代替非 Unicode 變量,則不需要在系統(tǒng)中的任何位置執(zhí)行字符轉(zhuǎn)換。
注意 未來版本的 Microsoft SQL Server 中將刪除 ntext 數(shù)據(jù)類型。
Unicode 碼位及它們所代表的字符與用于可視呈現(xiàn)的“字形”是分開的。ISO 標(biāo)準(zhǔn) (ISO/IEC 9541-1) 將字形定義為“與具體設(shè)計(jì)無關(guān)的可識別抽象圖形符號”。因此,一個(gè)字符不必總是由相同的字形乃至唯一的字形來表示。所選擇的字體決定將使用什么字形來表示特定碼位或一系列碼位。
有關(guān)詳細(xì)信息,請參閱 Unicode Consortium 網(wǎng)站。
編碼
Unicode 將碼位映射到字符,但實(shí)際上并不指定數(shù)據(jù)在內(nèi)存、數(shù)據(jù)庫或網(wǎng)頁中的表示方式。這便是 Unicode 數(shù)據(jù)編碼發(fā)揮作用的地方。有許多不同的 Unicode 編碼。多半選擇一種 Unicode 數(shù)據(jù)類型即可,不必為這些細(xì)節(jié)操心;不過,在以下情況下了解編碼有重要意義:
?應(yīng)對可能以不同方式對 Unicode 進(jìn)行編碼的應(yīng)用程序時(shí)
?向其他平臺(非 Microsoft Windows)或 Web 服務(wù)器發(fā)送數(shù)據(jù)時(shí)
?導(dǎo)入其他編碼的數(shù)據(jù)或?qū)?shù)據(jù)導(dǎo)出為其他編碼時(shí)
Unicode 標(biāo)準(zhǔn)定義了其單一字符集的多種編碼:UTF-7、UTF-8、UTF-16 和 UTF-32。本部分對這些常見的編碼進(jìn)行說明:
?UCS-2
?UTF-16
?UTF-8
SQL Server 通常以 UCS-2 編碼方案存儲 Unicode。不過,許多客戶端以另一種編碼方案(如 UTF-8)來處理 Unicode。這種情況在基于 Web 的應(yīng)用程序中經(jīng)常出現(xiàn)。在 Microsoft Visual Basic 應(yīng)用程序中,字符串以 UCS-2 編碼方案來處理。因此,不需要顯式地指定 Visual Basic 應(yīng)用程序與 SQL Server 實(shí)例之間的編碼方案轉(zhuǎn)換。
SQL Server 2005 使用 Unicode (UTF-16) 來對 XML 數(shù)據(jù)進(jìn)行編碼。類型為 xml 的列中的數(shù)據(jù)以內(nèi)部格式存儲為二進(jìn)制大型對象 (BLOB),以支持 XML 模型特征,如文檔順序和遞歸結(jié)構(gòu)。因此,從服務(wù)器檢索的 XML 數(shù)據(jù)會以 UTF-16 格式輸出;如果想要為檢索的數(shù)據(jù)使用其他編碼,則應(yīng)用程序必須對所檢索的 UTF-16 數(shù)據(jù)執(zhí)行必要的轉(zhuǎn)換?!禨QL Server 2005 聯(lián)機(jī)叢書》中的 XML 最佳實(shí)踐提供了如何為從 varchar(max) 列中檢索的 XML 數(shù)據(jù)顯式地聲明編碼的示例。
使用 UTF-16 編碼是因?yàn)樗梢蕴幚?2 字節(jié)或 4 字節(jié)字符,并且處理是依照面向字節(jié)的協(xié)議進(jìn)行的。這些特性使得 UTF-16 非常適合于遍歷使用不同編碼和字節(jié)排序系統(tǒng)的不同計(jì)算機(jī)。因?yàn)?XML 數(shù)據(jù)通常在網(wǎng)絡(luò)上得到廣泛共享,所以在數(shù)據(jù)庫中及在將 XML 數(shù)據(jù)導(dǎo)出到客戶端時(shí)保持默認(rèn)的 UTF-16 存儲格式是有意義的。
UCS-2
UCS-2 是 UTF-16 的前身。UCS-2 與 UTF-16 的不同之處是,UCS-2 是一種固定長度編碼,它以 16 位值(2 個(gè)字節(jié))表示所有字符,因此不支持補(bǔ)充字符。UCS-2 常與 UTF-16 發(fā)生混淆,UTF-16 用于在內(nèi)部表示 Microsoft Windows 操作系統(tǒng)(Windows NT、Windows 2000、Windows XP 和 Windows CE)中的文本,但 UCS-2 受到的限制更多。
注意 有關(guān)在 Windows 操作系統(tǒng)中使用 Unicode 的最新信息,請參閱 Microsoft Developer Network (MSDN) 庫中的 Unicode。建議 Windows 應(yīng)用程序在內(nèi)部使用 UTF-16,僅在必須使用其他格式時(shí)再通過接口作為“薄層”的一部分進(jìn)行轉(zhuǎn)換。
在 Microsoft SQL Server 2000 和 Microsoft SQL Server 2005 中以 Unicode 存儲的信息使用 UCS-2 編碼,無論使用的是哪個(gè)字符,該編碼都將每個(gè)字符存儲為兩個(gè)字節(jié)。因此,對拉丁語字母“A”的處理方式與對西里爾文字母 Sha ())、希伯來語字母 Lamed (ì)、泰米爾語字母 Rra (?) 或日語平假名字母 E (?|) 的處理方式是相同的。每個(gè)字母都有一個(gè)唯一的碼位(對于上述字母,碼位分別為 U+0041、U+0248、U+05DC、U+0BB1 和 U+3048,每個(gè)四位十六進(jìn)制數(shù)表示 UCS-2 使用的那兩個(gè)字節(jié))。
因?yàn)?UCS-2 只考慮了 65,536 個(gè)不同碼位的編碼,其本身無法處理補(bǔ)充字符,只能將補(bǔ)充字符視為未定義的 Unicode 代理項(xiàng)字符,這些字符組對后定義補(bǔ)充字符。不過,SQL Server 可以存儲補(bǔ)充字符而不會有字符丟失或損壞的風(fēng)險(xiǎn)。通過創(chuàng)建自定義 CLR 函數(shù),可以擴(kuò)展 SQL Server 處理代理項(xiàng)對的能力。有關(guān)處理代理項(xiàng)對和補(bǔ)充字符的詳細(xì)信息,請參閱本文后面的“補(bǔ)充字符和代理項(xiàng)對”部分。
注意 補(bǔ)充字符定義為“具有補(bǔ)充碼位的 Unicode 編碼字符”。補(bǔ)充碼位的范圍在 U+10000 和 U+10FFFF 之間。
UTF-8
UTF-8 是一種旨在以與計(jì)算機(jī)上的字節(jié)排序無關(guān)的方式來處理 Unicode 數(shù)據(jù)的編碼方案。在處理 ASCII 及其他要求使用 8 位編碼的面向字節(jié)的系統(tǒng)(例如,必須覆蓋大量使用不同編碼、不同字節(jié)順序和不同語言的計(jì)算機(jī)的郵件服務(wù)器)時(shí),UTF-8 會有幫助。盡管 SQL Server 2005 不以 UTF-8 格式存儲數(shù)據(jù),但它仍支持使用 UTF-8 來處理可擴(kuò)展標(biāo)記語言 (XML) 數(shù)據(jù)。有關(guān)詳細(xì)信息,請參閱本文的 SQL Server 2005 中的 XML 支持部分。
其他數(shù)據(jù)庫系統(tǒng)(例如,Oracle 和 Sybase SQL Server)通過使用 UTF-8 存儲來支持 Unicode。視服務(wù)器的實(shí)現(xiàn)方式而定,從技術(shù)上講實(shí)現(xiàn)數(shù)據(jù)庫引擎可能比較容易,因?yàn)榉?wù)器上的現(xiàn)有文本管理代碼在一次處理一個(gè)字節(jié)的數(shù)據(jù)時(shí)并不要求進(jìn)行重大更改。不過,在 Windows 環(huán)境中,UTF-8 存儲有幾個(gè)缺點(diǎn):
?組件對象模型 (COM) 僅在其 API 和接口中支持 UTF-16/UCS-2。因此,如果數(shù)據(jù)以 UTF-8 格式存儲,必須始終進(jìn)行轉(zhuǎn)換。僅在使用 COM 時(shí)會出現(xiàn)此問題;SQL Server 數(shù)據(jù)庫引擎通常不會調(diào)用 COM 接口。
?Windows XP 和 Windows Server 2003 的內(nèi)核均采用 Unicode。UTF-16 是 Windows 2000、Windows XP 和 Windows Server 2003 的標(biāo)準(zhǔn)編碼。不過,Windows 2000、Windows XP 和 Windows Server 2003 都可以識別 UTF-8。因此,在數(shù)據(jù)庫中使用 UTF-8 存儲格式需要進(jìn)行許多額外的轉(zhuǎn)換。通常,轉(zhuǎn)換所需的額外資源不會影響 SQL Server 數(shù)據(jù)庫引擎,但可能會影響許多客戶端操作。
?執(zhí)行許多字符串操作時(shí),UTF-8 的速度可能都會較慢。排序、比較及幾乎任何字符串操作的速度可能都會下降,因?yàn)樽址膶挾炔还潭ā?/p>
?UTF-8 往往需要 2 個(gè)以上的字節(jié),并且增加的大小會占用更多的磁盤和內(nèi)存空間。
盡管有這些缺點(diǎn),但考慮到 XML 已成為一項(xiàng)重要的 Internet 通信標(biāo)準(zhǔn)這一事實(shí),您可能希望考慮將默認(rèn)編碼設(shè)置為 UTF-8。

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

Oracle and DB2 are two commonly used relational database management systems, each of which has its own unique SQL syntax and characteristics. This article will compare and differ between the SQL syntax of Oracle and DB2, and provide specific code examples. Database connection In Oracle, use the following statement to connect to the database: CONNECTusername/password@database. In DB2, the statement to connect to the database is as follows: CONNECTTOdataba

"Usage of Division Operation in OracleSQL" In OracleSQL, division operation is one of the common mathematical operations. During data query and processing, division operations can help us calculate the ratio between fields or derive the logical relationship between specific values. This article will introduce the usage of division operation in OracleSQL and provide specific code examples. 1. Two ways of division operations in OracleSQL In OracleSQL, division operations can be performed in two different ways.

Database technology competition: What are the differences between Oracle and SQL? In the database field, Oracle and SQL Server are two highly respected relational database management systems. Although they both belong to the category of relational databases, there are many differences between them. In this article, we will delve into the differences between Oracle and SQL Server, as well as their features and advantages in practical applications. First of all, there are differences in syntax between Oracle and SQL Server.

Analysis of the Impact of MySQL Connection Number on Database Performance With the continuous development of Internet applications, databases have become an important data storage and management tool to support application systems. In the database system, the number of connections is an important concept, which is directly related to the performance and stability of the database system. This article will start from the perspective of MySQL database, explore the impact of the number of connections on database performance, and analyze it through specific code examples. 1. What is the number of connections? The number of connections refers to the number of client connections supported by the database system at the same time. It can also be managed

How does C++ software implement Chinese language support? With the process of globalization, more and more software needs to support multiple languages, including Chinese. In C++ development, implementing Chinese language support is not complicated and can be easily completed with only some basic skills and tools. This article will introduce how to implement Chinese language support in C++ software and provide specific code examples. 1. Use Unicode encoding. In order to support Chinese, you must first ensure that the software uses Unicode encoding internally. Unicode is a standard

The difference between Oracle and SQL and analysis of application scenarios In the database field, Oracle and SQL are two frequently mentioned terms. Oracle is a relational database management system (RDBMS), and SQL (StructuredQueryLanguage) is a standardized language for managing relational databases. While they are somewhat related, there are also some significant differences. First of all, by definition, Oracle is a specific database management system, consisting of

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.
