SQL 列数据类型中 BYTE 和 CHAR 之间的差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/81448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 23:26:52  来源:igfitidea点击:

Difference between BYTE and CHAR in column datatypes

sqloracleunicodevarchar

提问by Guido

In Oracle, what is the difference between :

在 Oracle 中,有什么区别:

CREATE TABLE CLIENT
(
 NAME VARCHAR2(11 BYTE),
 ID_CLIENT NUMBER
)

and

CREATE TABLE CLIENT
(
 NAME VARCHAR2(11 CHAR), -- or even VARCHAR2(11)
 ID_CLIENT NUMBER
)

回答by David Sykes

Let us assume the database character set is UTF-8, which is the recommended setting in recent versions of Oracle. In this case, some characters take more than 1 byte to store in the database.

让我们假设数据库字符集是 UTF-8,这是 Oracle 最新版本中的推荐设置。在这种情况下,某些字符需要超过 1 个字节才能存储在数据库中。

If you define the field as VARCHAR2(11 BYTE), Oracle can use up to 11 bytes for storage, but you may not actually be able to store 11 characters in the field, because some of them take more than one byte to store, e.g. non-English characters.

如果您将字段定义为VARCHAR2(11 BYTE),Oracle 最多可以使用 11 个字节进行存储,但实际上您可能无法在该字段中存储 11 个字符,因为其中一些需要超过 1 个字节来存储,例如非英文字符。

By defining the field as VARCHAR2(11 CHAR)you tell Oracle it can use enough space to store 11 characters, no matter how many bytes it takes to store each one. A single character may require up to 4 bytes.

通过定义VARCHAR2(11 CHAR)您告诉 Oracle的字段,它可以使用足够的空间来存储 11 个字符,无论存储每个字符需要多少字节。单个字符最多可能需要 4 个字节。

回答by Matthias Kestenholz

One has exactly space for 11 bytes, the other for exactly 11 characters. Some charsets such as Unicode variants may use more than one byte per char, therefore the 11 byte field might have space for less than 11 chars depending on the encoding.

一个正好有 11 个字节的空间,另一个正好有 11 个字符。某些字符集(例如 Unicode 变体)可能每个字符使用一个以上的字节,因此 11 字节字段可能具有少于 11 个字符的空间,具体取决于编码。

See also http://www.joelonsoftware.com/articles/Unicode.html

另见http://www.joelonsoftware.com/articles/Unicode.html

回答by user15453

Depending on the system configuration, size of CHAR mesured in BYTES can vary. In your examples:

根据系统配置,以 BYTES 为单位测量的 CHAR 大小可能会有所不同。在您的示例中:

  1. Limits field to 11 BYTE
  2. Limits field to 11 CHARacters
  1. 将字段限制为 11 个字节
  2. 将字段限制为 11 个CHAR演员



结论:1 CHAR 不等于 1 BYTE。





回答by Seldaek

I am not sure since I am not an Oracle user, but I assume that the difference lies when you use multi-byte character sets such as Unicode (UTF-16/32). In this case, 11 Bytes could account for less than 11 characters.

我不确定,因为我不是 Oracle 用户,但我认为区别在于您使用多字节字符集,例如 Unicode (UTF-16/32)。在这种情况下,11 个字节可能少于 11 个字符。

Also those field types might be treated differently in regard to accented characters or case, for example 'binaryField(ete) = "été"' will not match while 'charField(ete) = "été"' might (again not sure about Oracle).

此外,对于重音字符或大小写,这些字段类型可能会被区别对待,例如 'binaryField(ete) = "été"' 将不匹配而 'charField(ete) = "été"' 可能(再次不确定 Oracle) .