varchar2(n) 和 varchar2(n char) 之间的 Oracle SQL 区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16474238/
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-09-19 01:39:23  来源:igfitidea点击:

Oracle SQL difference between varchar2(n) and varchar2(n char)

sqloracle

提问by Ram

Scripts here at work always declare varchar2 columns as varchar2(n char). I do not see any difference and just curious. Thanks!

工作中的脚本总是将 varchar2 列声明为 varchar2(n char)。我没有看到任何区别,只是好奇。谢谢!

回答by Wayan Wiprayoga

Based on this resource

基于此资源

Oracle9i and above allow Varchar2 columns to be defined as a number of bytes VARCHAR2(50 BYTE) or a number of characters VARCHAR2(50 CHAR), the latter is useful if the database is ever converted to run a double-byte character set(such as Japanese), you won't have to edit the column sizes. The default measure, normally BYTE, is set with nls_length_semantics.

If you create a column as Varchar2 (50) but only store 10 bytes, then Oracle will only save 10 bytes to disc. This does not mean that you should just create Varchar2 (4000) columns 'just in case the space is needed', that is a really bad idea which will reduce the performance and maintainability of your application.

Oracle9i 及更高版本允许将 Varchar2 列定义为字节数 VARCHAR2(50 BYTE) 或字符数 VARCHAR2(50 CHAR),如果将数据库转换为运行双字节字符集(例如作为日语),您将不必编辑列大小。默认度量,通常是 BYTE,是用​​ nls_length_semantics 设置的。

如果将列创建为 Varchar2 (50) 但仅存储 10 个字节,则 Oracle 将仅将 10 个字节保存到磁盘。这并不意味着您应该只创建 Varchar2 (4000) 列“以防万一需要空间”,这是一个非常糟糕的主意,它会降低应用程序的性能和可维护性。

回答by jim mcnamara

The syntax is VARCHAR2(n)and VARCHAR2(n BYTE|CHAR)

语法是VARCHAR2(n)VARCHAR2(n BYTE|CHAR)

The default for (n) and (n BYTE) are usually the same. (n CHAR) may not be equivalent to (n|BYTE) or (n) unless you set NLS_LENGTH_SEMANTICSparameter to what you want, CHAR or BYTE. This setting is for character sets that use multibyte characters. Generally not for UTF8, for example.

(n) 和 (n BYTE) 的默认值通常相同。(n CHAR) 可能不等价于 (n|BYTE) 或 (n) 除非您将NLS_LENGTH_SEMANTICS参数设置为所需的 CHAR 或 BYTE。此设置适用于使用多字节字符的字符集。例如,通常不适用于 UTF8。

DO NOT change it, since there is existing code that works.

不要更改它,因为现有代码有效。

I would guess that (n) == (n CHAR) == (n BYTE) or your system.

我猜 (n) == (n CHAR) == (n BYTE) 或您的系统。