varchar 和 nvarchar 有什么区别?

发布于 2024-07-06 11:02:20 字数 82 浏览 10 评论 0原文

只是 nvarchar 支持多字节字符吗? 如果是这样的话,除了存储问题之外,使用 varchars 真的还有什么意义吗?

Is it just that nvarchar supports multibyte characters? If that is the case, is there really any point, other than storage concerns, to using varchars?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(22

如痴如狂 2024-07-13 11:02:21

Varchar(n)nvarchar(n) 之间的主要区别是:

在此处输入图像描述

Varchar (可变长度,非-Unicode字符数据)大小最大为8000。

  1. 它是一种变长数据类型
  2. 用于存储非Unicode字符
  3. 每个字符占用1个字节的空间

在此处输入图像描述

Nvarchar:变量-长度 Unicode 字符数据。

  1. 它是一种可变长度数据类型,
  2. 用于存储Unicode字符。
  3. 数据以 Unicode 编码存储。 每一个
    支持语言。 (例如阿拉伯语、德语、印地语等语言)

The main difference between Varchar(n) and nvarchar(n) is:

enter image description here

Varchar ( Variable-length, non-Unicode character data) size is upto 8000.

  1. It is a variable length data type
  2. Used to store non-Unicode characters
  3. Occupies 1 byte of space for each character

enter image description here

Nvarchar: Variable-length Unicode character data.

  1. It is a variable-length data type
  2. Used to store Unicode characters.
  3. Data is stored in a Unicode encoding. Every
    language is supported. (for example the languages Arabic, German,Hindi,etc and so on)
微凉徒眸意 2024-07-13 11:02:21

我的两分钱

  1. 如果不使用正确的数据类型,索引可能会失败:
    在 SQL Server 中:当您在 VARCHAR 列上有索引并为其提供 Unicode 字符串时,SQL Server 不会使用该索引。 当您将 Bi​​gInt 呈现给包含 SmallInt 的索引列时,也会发生同样的情况。 即使 BigInt 小到足以成为 SmallInt,SQL Server 也无法使用该索引。 相反,您不会遇到此问题(当向索引 BigInt 或 NVARCHAR 列提供 SmallInt 或 Ansi-Code 时)。

  2. 不同 DBMS(数据库管理系统)的数据类型可能有所不同:
    要知道每个数据库的数据类型都略有不同,并且 VARCHAR 并不意味着在所有地方都相同。 虽然 SQL Server 具有 VARCHAR 和 NVARCHAR,但 Apache/Derby 数据库仅具有 VARCHAR,并且 VARCHAR 采用 Unicode。

My two cents

  1. Indexes can fail when not using the correct datatypes:
    In SQL Server: When you have an index over a VARCHAR column and present it a Unicode String, SQL Server does not make use of the index. The same thing happens when you present a BigInt to a indexed-column containing SmallInt. Even if the BigInt is small enough to be a SmallInt, SQL Server is not able to use the index. The other way around you do not have this problem (when providing SmallInt or Ansi-Code to an indexed BigInt ot NVARCHAR column).

  2. Datatypes can vary between different DBMS's (DataBase Management System):
    Know that every database has slightly different datatypes and VARCHAR does not means the same everywhere. While SQL Server has VARCHAR and NVARCHAR, an Apache/Derby database has only VARCHAR and there VARCHAR is in Unicode.

流星番茄 2024-07-13 11:02:21

主要是nvarchar存储Unicode字符,varchar存储非Unicode字符。

“Unicode”是指 16 位字符编码方案,允许将许多其他语言(如阿拉伯语、希伯来语、中文、日语)的字符编码在单个字符集中。

这意味着 unicodes 每个字符使用 2 个字节来存储,而 nonunicodes 每个字符仅使用一个字节来存储。 这意味着与非 unicode 相比,unicode 需要双倍的存储容量。

Mainly nvarchar stores Unicode characters and varchar stores non-Unicode characters.

"Unicodes" means 16-bit character encoding scheme allowing characters from lots of other languages like Arabic, Hebrew, Chinese, Japanese, to be encoded in a single character set.

That means unicodes is using 2 bytes per character to store and nonunicodes uses only one byte per character to store. Which means unicodes need double capacity to store compared to non-unicodes.

℉絮湮 2024-07-13 11:02:21

你说得对。 nvarchar 存储 Unicode 数据,而 varchar 存储单字节字符数据。 除了您已经提到的存储差异(nvarchar 需要两倍于 varchar 的存储空间)之外,您更喜欢 nvarchar 而不是 的主要原因>varchar 将是国际化(即以其他语言存储字符串)。

You're right. nvarchar stores Unicode data while varchar stores single-byte character data. Other than storage differences (nvarchar requires twice the storage space as varchar), which you already mentioned, the main reason for preferring nvarchar over varchar would be internationalization (i.e. storing strings in other languages).

流云如水 2024-07-13 11:02:21

nVarchar 将帮助您存储 Unicode 字符。 如果您想存储本地化数据,这是一种可行的方法。

nVarchar will help you to store Unicode characters. It is the way to go if you want to store localized data.

溺孤伤于心 2024-07-13 11:02:21

我想说,这要看情况。

如果您开发一个桌面应用程序,其中操作系统以 Unicode 运行(就像所有当前的 Windows 系统一样)并且语言本身支持 Unicode(默认字符串是 Unicode,就像 Java 或 C# 中的那样),那么请使用 nvarchar。

如果您开发一个 Web 应用程序,其中字符串采用 UTF-8,语言为 PHP,而 PHP 本身仍然不支持 Unicode(在版本 5.x 中),那么 varchar 可能是更好的选择。

I would say, it depends.

If you develop a desktop application, where the OS works in Unicode (like all current Windows systems) and language does natively support Unicode (default strings are Unicode, like in Java or C#), then go nvarchar.

If you develop a web application, where strings come in as UTF-8, and language is PHP, which still does not support Unicode natively (in versions 5.x), then varchar will probably be a better choice.

带刺的爱情 2024-07-13 11:02:21

如果用一个字节来存储一个字符,就有256种可能的组合,从而可以保存256个不同的字符。 排序规则是定义字符以及比较和排序字符的规则的模式。

1252,即 Latin1 (ANSI),是最常见的。 单字节字符集也不足以存储许多语言使用的所有字符。 例如,某些亚洲语言有数千个字符,因此每个字符必须使用两个字节。

Unicode 标准

当在网络中使用使用多个代码页的系统时,管理通信就会变得困难。 为了标准化,ISO 和 Unicode 联盟引入了Unicode。 Unicode 使用两个字节来存储每个字符。 即可以定义65,536个不同的字符,因此Unicode几乎可以覆盖所有字符。 如果两台计算机使用 Unicode,则每个符号都将以相同的方式表示,并且不需要转换 - 这就是 Unicode 背后的想法。

SQL Server 有两类字符数据类型:

  • 非 Unicode(char、varchar 和 text)
  • Unicode(nchar、nvarchar 和 ntext)

如果我们需要保存来自多个国家/地区的字符数据,请始终使用 Unicode。

If a single byte is used to store a character, there are 256 possible combinations, and thereby you can save 256 different characters. Collation is the pattern which defines the characters and the rules by which they are compared and sorted.

1252, which is the Latin1 (ANSI), is the most common. Single-byte character sets are also inadequate to store all the characters used by many languages. For example, some Asian languages have thousands of characters, so they must use two bytes per character.

Unicode standard

When systems using multiple code pages are used in a network, it becomes difficult to manage communication. To standardize things, the ISO and Unicode consortium introduced the Unicode. Unicode uses two bytes to store each character. That is 65,536 different characters can be defined, so almost all the characters can be covered with Unicode. If two computers use Unicode, every symbol will be represented in the same way and no conversion is needed - this is the idea behind Unicode.

SQL Server has two categories of character datatypes:

  • non-Unicode (char, varchar, and text)
  • Unicode (nchar, nvarchar, and ntext)

If we need to save character data from multiple countries, always use Unicode.

苏璃陌 2024-07-13 11:02:21

我查看了答案,许多人似乎建议使用 nvarchar 而不是 varchar,因为空间不再是问题,所以启用 Unicode 没有什么坏处。额外的存储空间。 嗯,当您想在列上应用索引时,情况并不总是如此。 SQL Server 对可以索引的字段大小有 900 字节的限制。 因此,如果您有 varchar(900),您仍然可以为其建立索引,但不能为 varchar(901) 建立索引。 使用 nvarchar 时,字符数减半,因此您最多可以索引 nvarchar(450)。 因此,如果您确信不需要 nvarchar,我不建议使用它。

一般来说,在数据库中,我建议坚持您需要的大小,因为您始终可以扩展。 例如,工作中的一位同事曾经认为对列使用nvarchar(max)没有什么坏处,因为我们的存储根本没有问题。 后来,当我们尝试对此列应用索引时,SQL Server 拒绝了这一点。 然而,如果他一开始就使用 varchar(5),我们稍后就可以简单地将其扩展为我们需要的内容,而不会出现需要我们制定现场迁移计划来解决此问题的问题。

I had a look at the answers and many seem to recommend to use nvarchar over varchar, because space is not a problem anymore, so there is no harm in enabling Unicode for little extra storage. Well, this is not always true when you want to apply an index over your column. SQL Server has a limit of 900 bytes on the size of the field you can index. So if you have a varchar(900) you can still index it, but not varchar(901). With nvarchar, the number of characters is halved, so you can index up to nvarchar(450). So if you are confident you don't need nvarchar, I don't recommend using it.

In general, in databases, I recommend sticking to the size you need, because you can always expand. For example, a colleague at work once thought that there is no harm in using nvarchar(max) for a column, as we have no problem with storage at all. Later on, when we tried to apply an index over this column, SQL Server rejected this. If, however, he started with even varchar(5), we could have simply expanded it later to what we need without such a problem that will require us to do a field migration plan to fix this problem.

林空鹿饮溪 2024-07-13 11:02:21

虽然 NVARCHAR 存储 Unicode,但您应该考虑通过排序规则的帮助,您也可以使用 VARCHAR 并保存本地语言的数据。

想象一下以下场景。

您的数据库的排序规则是波斯语,您可以在 VARCHAR(10) 数据类型中保存一个值,如 '자대'(Ali 的波斯语书写)。 没有问题,DBMS只用三个字节来存储它。

但是,如果您想将数据传输到另一个数据库并查看正确的结果,您的目标数据库必须具有与目标相同的排序规则,在本示例中为波斯语。

如果您的目标排序规则不同,您会在目标数据库中看到一些问号(?)。

最后,请记住,如果您使用的是用于本地语言的庞大数据库,我建议使用位置而不是使用太多空格。

我相信设计可以有所不同。 这取决于您的工作环境。

Although NVARCHAR stores Unicode, you should consider by the help of collation also you can use VARCHAR and save your data of your local languages.

Just imagine the following scenario.

The collation of your DB is Persian and you save a value like 'علی' (Persian writing of Ali) in the VARCHAR(10) datatype. There is no problem and the DBMS only uses three bytes to store it.

However, if you want to transfer your data to another database and see the correct result your destination database must have the same collation as the target which is Persian in this example.

If your target collation is different, you see some question marks(?) in the target database.

Finally, remember if you are using a huge database which is for usage of your local language, I would recommend to use location instead of using too many spaces.

I believe the design can be different. It depends on the environment you work on.

橘虞初梦 2024-07-13 11:02:21

Jeffrey L Whitledge 的声誉得分约为 47000,建议使用 nvarchar

Solomon Rutzky 的声誉得分约为 33200,建议使用:不要始终使用 NVARCHAR。 这是一种非常危险且往往代价高昂的态度/方法。

varchar 之间的主要性能差异是什么和 nvarchar SQL Server 数据类型?

https://www. sqlservercentral.com/articles/disk-is-cheap-orly-4

两个人的声誉都这么高,一个学习sql server数据库开发人员会选择什么?

如果您的选择不一致,答案和评论中会有很多关于性能问题的警告。

对于 nvarchar 的性能,有赞成/反对的评论。

对于性能,有赞成/反对 varchar 的评论。

我对包含数百列的表有一个特殊要求,这本身可能是不寻常的?

我选择 varchar 是为了避免接近 SQL*server 2012 的 8060 字节表记录大小限制。

对我来说,使用 nvarchar 会超过这个 8060 字节限制。

我还认为我应该将相关代码表的数据类型与主中心表的数据类型相匹配。

我见过以前经验丰富的数据库开发人员在南澳大利亚政府这个工作场所使用 varchar 列,其中表行数将达到数百万或更多(并且在这些非常大的数据库中,很少有 nvarchar 列(如果有的话))表),因此预期的数据行量可能成为此决策的一部分。

Jeffrey L Whitledge with ~47000 reputation score recommends usage of nvarchar

Solomon Rutzky with with ~33200 reputation score recommends: Do NOT always use NVARCHAR. That is a very dangerous, and often costly, attitude / approach.

What are the main performance differences between varchar and nvarchar SQL Server data types?

https://www.sqlservercentral.com/articles/disk-is-cheap-orly-4

Both persons of such a high reputation, what does a learning sql server database developer choose?

There are many warnings in answers and comments about performance issues if you are not consistent in choices.

There are comments pro/con nvarchar for performance.

There are comments pro/con varchar for performance.

I have a particular requirement for a table with many hundreds of columns, which in itself is probably unusual ?

I'm choosing varchar to avoid going close to the 8060 byte table record size limit of SQL*server 2012.

Use of nvarchar, for me, goes over this 8060 byte limit.

I'm also thinking that I should match the data types of the related code tables to the data types of the primary central table.

I have seen use of varchar column at this place of work, South Australian Government, by previous experienced database developers, where the table row count is going to be several millions or more (and very few nvarchar columns, if any, in these very large tables), so perhaps the expected data row volumes becomes part of this decision.

妳是的陽光 2024-07-13 11:02:21

我必须在这里说(我意识到我可能会公开批评自己!),但肯定是 NVARCHAR 实际上更有用的唯一一次(注意这里的更多!)比VARCHAR是当所有依赖系统上和数据库本身内的所有排序规则都相同时......? 如果不是,则无论如何都必须进行排序转换,因此使得 VARCHARNVARCHAR 一样可行。

除此之外,某些数据库系统(例如 SQL Server(2012 年之前))具有页面大小约。 8K。 因此,如果您要存储的可搜索数据不是保存在 TEXTNTEXT 字段中,那么 VARCHAR 提供了完整的 8k 值空间,而 NVARCHAR 仅提供 4k(字节数加倍,空间加倍)。

总而言之,我想,两者的使用取决于:

  • 项目或上下文
  • 基础设施
  • 数据库系统

I have to say here (I realise that I'm probably going to open myself up to a slating!), but surely the only time when NVARCHAR is actually more useful (notice the more there!) than VARCHAR is when all of the collations on all of the dependant systems and within the database itself are the same...? If not then collation conversion has to happen anyway and so makes VARCHAR just as viable as NVARCHAR.

To add to this, some database systems, such as SQL Server (before 2012) have a page size of approx. 8K. So, if you're looking at storing searchable data not held in something like a TEXT or NTEXT field then VARCHAR provides the full 8k's worth of space whereas NVARCHAR only provides 4k (double the bytes, double the space).

I suppose, to summarise, the use of either is dependent on:

  • Project or context
  • Infrastructure
  • Database system
眼中杀气 2024-07-13 11:02:21

遵循Sql Server VARCHAR 和 NVARCHAR 数据类型之间的差异。 在这里你可以看到一个非常描述性的方式。

一般来说,nvarchar 将数据存储为 Unicode,因此,如果要在数据列中存储多语言数据(不止一种语言),则需要 N 变体。

Follow Difference Between Sql Server VARCHAR and NVARCHAR Data Type. Here you could see in a very descriptive way.

In generalnvarchar stores data as Unicode, so, if you're going to store multilingual data (more than one language) in a data column you need the N variant.

水染的天色ゝ 2024-07-13 11:02:21

varchar 相比,nvarchar 使用起来更安全,可以使我们的代码不会出错(类型不匹配),因为 nvarchar 也允许使用 unicode 字符。
当我们在SQL Server查询中使用where条件时,如果我们使用=运算符,有时会抛出错误。 可能的原因是我们的映射列将在 varchar 中定义。 如果我们在 nvarchar 中定义它,这个问题就不会发生。 我们仍然坚持使用 varchar 并避免这个问题,我们最好使用 LIKE 关键字而不是 =

nvarchar is safe to use compared to varchar in order to make our code error free (type mismatching) because nvarchar allows unicode characters also.
When we use where condition in SQL Server query and if we are using = operator, it will throw error some times. Probable reason for this is our mapping column will be difined in varchar. If we defined it in nvarchar this problem my not happen. Still we stick to varchar and avoid this issue we better use LIKE key word rather than =.

你丑哭了我 2024-07-13 11:02:21

varchar适合存储非unicode,这意味着有限的字符。 而 nvarchar 是 varchar 的超集,因此除了我们可以使用 varchar 存储的字符之外,我们还可以在不忽略函数的情况下存储更多字符。

有人评论说,如今存储/空间不再是问题。 即使空间对一个人来说不是问题,识别最佳数据类型也应该是一个要求。

这不仅仅是存储的问题! “数据移动”,你就知道我要走向何方了!

varchar is suitable to store non-unicode which means limited characters. Whereas nvarchar is superset of varchar so along with what characters we can store by using varchar, we can store even more without losing sight of functions.

Someone commented that storage/space is not an issue nowadays. Even if space is not an issue for one, identifying an optimal data type should be a requirement.

It's not only about storage! "Data moves" and you see where I am leading to!

破晓 2024-07-13 11:02:21

与此线程中的一些过时答案相反,varchar 可以存储 Unicode 字符,如果您想使用 UTF-8,这也是一种可行的方法。
解决方案是将数据库排序规则设置为 Latin1_General_100_CI_AS_SC_UTF8

CREATE DATABASE [TestUTF-8] COLLATE Latin1_General_100_CI_AS_SC_UTF8
GO
USE [TestUTF-8]
GO

-- using varchar works
DECLARE @XML xml = '<?xml version="1.0" encoding="utf-8"?>
<test>
    <status>Success ✅</status>
</test>
'

-- using nvarchar will fail (XML parsing: line 1, character 38, unable to switch the encoding)
-- DECLARE @XML xml = N'<?xml version="1.0" encoding="utf-8"?>
-- <test>
--     <status>Success ✅</status>
-- </test>
-- '

SELECT [status] = c.c.value('(./status)[1]', 'varchar(max)') 
FROM @XML.nodes('/test') c(c)

As opposed to some outdated answers in this thread, varchar can store Unicode characters and is also the way to go if you want to use UTF-8.
The solution is to set your database collation to Latin1_General_100_CI_AS_SC_UTF8

CREATE DATABASE [TestUTF-8] COLLATE Latin1_General_100_CI_AS_SC_UTF8
GO
USE [TestUTF-8]
GO

-- using varchar works
DECLARE @XML xml = '<?xml version="1.0" encoding="utf-8"?>
<test>
    <status>Success ✅</status>
</test>
'

-- using nvarchar will fail (XML parsing: line 1, character 38, unable to switch the encoding)
-- DECLARE @XML xml = N'<?xml version="1.0" encoding="utf-8"?>
-- <test>
--     <status>Success ✅</status>
-- </test>
-- '

SELECT [status] = c.c.value('(./status)[1]', 'varchar(max)') 
FROM @XML.nodes('/test') c(c)
黎夕旧梦 2024-07-13 11:02:20

nvarchar 列可以存储任何 Unicode 数据。 varchar 列仅限于 8 位代码页。 有些人认为应该使用varchar,因为它占用的空间更少。 我相信这不是正确的答案。 代码页不兼容是一个痛苦的问题,而 Unicode 是解决代码页问题的良药。 如今,有了便宜的磁盘和内存,真的没有理由再浪费时间在代码页上乱搞了。

所有现代操作系统和开发平台都在内部使用 Unicode。 通过使用 nvarchar 而不是 varchar,您可以避免每次读取或写入数据库时​​都进行编码转换。 转换需要时间,并且容易出错。 从转换错误中恢复是一个不小的问题。

如果您与仅使用 ASCII 的应用程序交互,我仍然建议在数据库中使用 Unicode。 操作系统和数据库排序算法将更好地与 Unicode 配合使用。 Unicode 避免了与其他系统交互时出现的转换问题。 你将为未来做好准备。 对于您必须维护的任何遗留系统,您始终可以验证您的数据是否仅限于 7 位 ASCII,即使同时享受完整 Unicode 存储的一些好处。

An nvarchar column can store any Unicode data. A varchar column is restricted to an 8-bit codepage. Some people think that varchar should be used because it takes up less space. I believe this is not the correct answer. Codepage incompatabilities are a pain, and Unicode is the cure for codepage problems. With cheap disk and memory nowadays, there is really no reason to waste time mucking around with code pages anymore.

All modern operating systems and development platforms use Unicode internally. By using nvarchar rather than varchar, you can avoid doing encoding conversions every time you read from or write to the database. Conversions take time, and are prone to errors. And recovery from conversion errors is a non-trivial problem.

If you are interfacing with an application that uses only ASCII, I would still recommend using Unicode in the database. The OS and database collation algorithms will work better with Unicode. Unicode avoids conversion problems when interfacing with other systems. And you will be preparing for the future. And you can always validate that your data is restricted to 7-bit ASCII for whatever legacy system you're having to maintain, even while enjoying some of the benefits of full Unicode storage.

不喜欢何必死缠烂打 2024-07-13 11:02:20

varchar:可变长度,非Unicode 字符数据。 数据库排序规则确定使用哪个代码页存储数据。

nvarchar:可变长度 Unicode 字符数据。 取决于数据库排序规则进行比较。

掌握了这些知识后,就可以使用与您的输入数据匹配的任何一个(ASCII 与 Unicode)。

varchar: Variable-length, non-Unicode character data. The database collation determines which code page the data is stored using.

nvarchar: Variable-length Unicode character data. Dependent on the database collation for comparisons.

Armed with this knowledge, use whichever one matches your input data (ASCII v. Unicode).

池木 2024-07-13 11:02:20

我总是使用 nvarchar,因为它允许我正在构建的任何内容能够承受我向其抛出的几乎所有数据。 我的CMS系统偶然显示中文,因为我使用了nvarchar。 如今,任何新应用程序都不应该真正关心所需的空间量。

I always use nvarchar as it allows whatever I'm building to withstand pretty much any data I throw at it. My CMS system does Chinese by accident, because I used nvarchar. These days, any new applications shouldn't really be concerned with the amount of space required.

清引 2024-07-13 11:02:20

自 SQL Server 以来2019 varchar 列支持 UTF-8 编码。

因此,从现在开始,区别在于大小。

在数据库系统中,这会转化为速度上的差异。

一般来说,更少的数据 = 更少的 IO + 更少的内存 = 更高的速度。 阅读上面的文章了解数字。

从现在开始使用 UTF8 中的 varchar!

仅当您拥有 大比例 字符范围为 2048 - 16383 和 16384 - 65535 的数据时 - 您才必须测量

Since SQL Server 2019 varchar columns support UTF-8 encoding.

Thus, from now on, the difference is size.

In a database system that translates to difference in speed.

Less data = Less IO + Less Memory = More speed in general. Read the article above for the numbers.

Go for varchar in UTF8 from now on!

Only if you have big percentage of data with characters in ranges 2048 - 16383 and 16384 – 65535 - you will have to measure

淡淡離愁欲言轉身 2024-07-13 11:02:20

这取决于 Oracle 的安装方式。 在安装过程中,会设置 NLS_CHARACTERSET 选项。 您可以通过查询SELECT value$ FROM sys.props$ WHERE name = 'NLS_CHARACTERSET'找到它。

如果您的 NLS_CHARACTERSET 是 UTF8 等 Unicode 编码,那就太好了。 使用 VARCHAR 和 NVARCHAR 几乎相同。 现在停止阅读,直接去读吧。 否则,或者如果您无法控制 Oracle 字符集,请继续阅读。

VARCHAR — 数据以 NLS_CHARACTERSET 编码存储。 如果同一服务器上还有其他数据库实例,您可能会受到它们的限制; 反之亦然,因为您必须共享设置。 这样的字段可以存储可以使用该字符集编码的任何数据,而不能存储其他数据。 例如,如果字符集是 MS-1252,则只能存储英文字母、少数重音字母和其他一些字符(例如 € 和 —)。 您的应用程序仅对少数区域有用,无法在世界其他任何地方运行。 因此,这被认为是一个坏主意。

NVARCHAR — 数据以 Unicode 编码存储。 支持每种语言。 一个好主意。

存储空间怎么样? VARCHAR 通常是高效的,因为字符集/编码是针对特定区域设置定制设计的。 讽刺的是,NVARCHAR 字段以 UTF-8 或 UTF-16 编码存储,基于 NLS 设置。 UTF-8 对于“西方”语言非常有效,同时仍然支持亚洲语言。 UTF-16 对于亚洲语言非常有效,同时仍然支持“西方”语言。 如果担心存储空间,请选择 NLS 设置以使 Oracle 根据需要使用 UTF-8 或 UTF-16。

处理速度怎么样? 大多数新的编码平台本身就使用 Unicode(Java、.NET,甚至几年前的 C++ std::wstring!),因此如果数据库字段是 VARCHAR,它会强制 Oracle 在每次读取或写入时在字符集之间进行转换,这不太好。 使用 NVARCHAR 可以避免转换。

底线:使用 NVARCHAR! 它避免了限制和依赖性,非常适合存储空间,而且通常也最适合性能。

It depends on how Oracle was installed. During the installation process, the NLS_CHARACTERSET option is set. You may be able to find it with the query SELECT value$ FROM sys.props$ WHERE name = 'NLS_CHARACTERSET'.

If your NLS_CHARACTERSET is a Unicode encoding like UTF8, great. Using VARCHAR and NVARCHAR are pretty much identical. Stop reading now, just go for it. Otherwise, or if you have no control over the Oracle character set, read on.

VARCHAR — Data is stored in the NLS_CHARACTERSET encoding. If there are other database instances on the same server, you may be restricted by them; and vice versa, since you have to share the setting. Such a field can store any data that can be encoded using that character set, and nothing else. So for example if the character set is MS-1252, you can only store characters like English letters, a handful of accented letters, and a few others (like € and —). Your application would be useful only to a few locales, unable to operate anywhere else in the world. For this reason, it is considered A Bad Idea.

NVARCHAR — Data is stored in a Unicode encoding. Every language is supported. A Good Idea.

What about storage space? VARCHAR is generally efficient, since the character set / encoding was custom-designed for a specific locale. NVARCHAR fields store either in UTF-8 or UTF-16 encoding, base on the NLS setting ironically enough. UTF-8 is very efficient for "Western" languages, while still supporting Asian languages. UTF-16 is very efficient for Asian languages, while still supporting "Western" languages. If concerned about storage space, pick an NLS setting to cause Oracle to use UTF-8 or UTF-16 as appropriate.

What about processing speed? Most new coding platforms use Unicode natively (Java, .NET, even C++ std::wstring from years ago!) so if the database field is VARCHAR it forces Oracle to convert between character sets on every read or write, not so good. Using NVARCHAR avoids the conversion.

Bottom line: Use NVARCHAR! It avoids limitations and dependencies, is fine for storage space, and usually best for performance too.

青朷 2024-07-13 11:02:20

nvarchar 将数据存储为 Unicode,因此,如果要在数据列中存储多语言数据(不止一种语言),则需要 N 变体。

nvarchar stores data as Unicode, so, if you're going to store multilingual data (more than one language) in a data column you need the N variant.

庆幸我还是我 2024-07-13 11:02:20

varchar 仅用于非 Unicode 字符,而 nvarchar 用于 unicode>非 unicode 字符。 下面给出了它们之间的其他一些区别。

VARCHAR 与 NVARCHAR

VARCHARNVARCHAR
字符数据类型可变长度、非 Unicode 字符可变长度、Unicode 和非 Unicode 字符,例如日语、韩语和中文。
最大长度最多 8,000 个字符最多 4,000 个字符
字符大小每个字符占用 1 字节占用 2 字节每个 Unicode/非 Unicode 字符
存储大小实际长度(以字节为单位)2 倍实际长度(以字节为单位)
用法当数据长度为可变长度或可变长度列且实际数据始终远小于容量时使用由于仅存储,使用仅当您需要 Unicode 支持(例如日语汉字或韩语朝鲜文字符)时。

varchar is used for non-Unicode characters only on the other hand nvarchar is used for both unicode and non-unicode characters. Some other difference between them is given bellow.

VARCHAR vs. NVARCHAR

VARCHARNVARCHAR
Character Data TypeVariable-length, non-Unicode charactersVariable-length, both Unicode and non-Unicode characters such as Japanese, Korean, and Chinese.
Maximum LengthUp to 8,000 charactersUp to 4,000 characters
Character SizeTakes up 1 byte per characterTakes up 2 bytes per Unicode/Non-Unicode character
Storage SizeActual Length (in bytes)2 times Actual Length (in bytes)
UsageUsed when data length is variable or variable length columns and if actual data is always way less than capacityDue to storage only, used only if you need Unicode support such as the Japanese Kanji or Korean Hangul characters.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文