SQL 为什么 VARCHAR 需要长度规范?

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

Why does VARCHAR need length specification?

sqldatabasevarchar

提问by Fixpoint

Why do we always need to specify VARCHAR(length)instead of just VARCHAR? It is dynamic anyway.

为什么我们总是需要指定VARCHAR(length)而不仅仅是VARCHAR?无论如何它是动态的。

UPD: I'm puzzled specifically by the fact that it is mandatory (e.g. in MySQL).

UPD:我对它是强制性的(例如在 MySQL 中)这一事实感到特别困惑。

回答by Robin Day

The "length" of the VARCHAR is not the length of the contents, it is the maximum length of the contents.

VARCHAR 的“长度”不是内容的长度,而是内容的最大长度。

The max length of a VARCHAR is not dynamic, it is fixed and therefore has to be specified.

VARCHAR 的最大长度不是动态的,它是固定的,因此必须指定。

If you don't want to define a maximum size for it then use VARCHAR(MAX).

如果您不想为其定义最大大小,请使用 VARCHAR(MAX)。

回答by Oded

First off, it does not needed it in all databases. Look at SQL Server, where it is optional.

首先,并不是所有数据库都需要它。查看SQL Server,它是可选的。

Regardless, it defines a maximum size for the content of the field. Not a bad thing in itself, and it conveys meaning (for example - phone numbers, where you do not want international numbers in the field).

无论如何,它定义了字段内容的最大大小。本身并不是一件坏事,它传达了意义(例如 - 电话号码,您不希望在该领域使用国际号码)。

回答by a_horse_with_no_name

You can see it as a constraint on your data. It ensures that you don't store data that violates your constraint. It is conceptionally similar to e.g. a check constraint on a integer column that ensure that only positive values are entered.

您可以将其视为对数据的约束。它确保您不会存储违反约束的数据。它在概念上类似于整数列上的检查约束,确保仅输入正值。

回答by Vipin Jain

There's possible performance impact: in MySQL, temporary tablesand MEMORY tablesstore a VARCHARcolumn as a fixed-length column, padded out to its maximum length.

有可能的性能impact:在MySQL,temporary tablesMEMORY tables存储VARCHAR列一个固定长度列,填补了它的最大长度。

If you design VARCHARcolumns much larger than the greatest size you need, you will consume more memory than you have to. This affects cache efficiency, sorting speed, etc.

如果您设计的VARCHAR列比您需要的最大尺寸大得多,您将消耗更多的内存。这会影响cache efficiency, sorting speed等。

So you give the max length which is under your string come. like if you max length of character 10 so don't give his length 100 or more.

所以你给出了你的字符串下的最大长度。就像你最大长度的字符 10 所以不要给他的长度 100 或更多。

回答by Tom Gullen

The more the database knows about the data it is storing, the more optimisations it can make when searching/adding/updating data with requests.

数据库对它存储的数据了解得越多,它在使用请求搜索/添加/更新数据时可以进行的优化就越多。

回答by NibblyPig

The answer is you don'tneed to, it's optional.

答案是,你并不需要,它是可选的。

It's there if you want to ensure that strings do not exceed a certain length.

如果您想确保字符串不超过特定长度,它就在那里。

回答by Luca Matteis

From Wikipedia:

来自维基百科

Varchar fields can be of any size up to the limit. The limit differs from types of databases, an Oracle 9i Database has a limit of 4000 bytes, a MySQL Database has a limit of 65,535 bytes (for the entire row) and Microsoft SQL Server 2005 8000 bytes (unless varchar(max) is used, which has a maximum storage capacity of 2,147,483,648 bytes).

Varchar 字段可以是任意大小,最多可达限制。限制因数据库类型而异,Oracle 9i 数据库限制为 4000 字节,MySQL 数据库限制为 65,535 字节(对于整行)和 Microsoft SQL Server 2005 8000 字节(除非使用 varchar(max),其最大存储容量为 2,147,483,648 字节)。