mysql 中的字符串长度有限制吗?

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

Are there any limits on length of string in mysql?

sqlmysqlruby-on-rails

提问by Nikhil Garg

I am using MySQL data base with Rails. I have created a field of type string. Are there any limits to its length? What about type text?
Also as textis variable sized, I believe there would be extra costs associated with using text objects. How important can they get, if at all?

我在 Rails 中使用 MySQL 数据库。我创建了一个类型的字段string。它的长度有什么限制吗?类型text呢?
同样text是可变大小,我相信使用文本对象会产生额外的成本。他们有多重要,如果有的话?

回答by alternative

String, in general, should be used for short text. For example, it is a VARCHAR(255)under MySQL.

通常,字符串应该用于短文本。例如,它是VARCHAR(255)MySQL 下的一个。

Text uses the larger text from the database, like, in MySQL, the type TEXT.

Text 使用数据库中较大的文本,例如 MySQL 中的 type TEXT

For information on how this works and the internals in MySQL and limits and such, see the other answer by Pekka.

有关其工作原理以及 MySQL 内部结构和限制等的信息,请参阅 Pekka 的其他答案。

If you are requesting, say, a paragraph, I would use text. If you are requesting a username or email, use string.

如果你要求,比如说,一个段落,我会使用文本。如果您要请求用户名或电子邮件,请使用字符串。

回答by Paul Preibisch

CHAR

字符

A fixed-length string that is always right-padded with spaces to the specified length when stored The range of Length is 1 to 255 characters. Trailing spaces are removed when the value is retrieved. CHARvalues are sorted and compared in case-insensitive fashion according to the default character set unless the BINARYkeyword is given.

一个固定长度的字符串,存储时总是用空格填充到指定长度。长度的范围是 1 到 255 个字符。检索值时将删除尾随空格。CHAR除非BINARY给出关键字,否则根据默认字符集以不区分大小写的方式对值进行排序和比较。

VARCHAR

VARCHAR

A variable-length string. Note: Trailing spaces are removed when the value is stored (this differs from the ANSI SQL specification)
The range of Length is 1 to 255 characters. VARCHARvalues are sorted and compared in case-insensitive fashion unless the BINARYkeyword is given

一个可变长度的字符串。注意:存储值时将删除尾随空格(这与 ANSI SQL 规范不同)
Length 的范围是 1 到 255 个字符。VARCHAR除非BINARY给出关键字,否则以不区分大小写的方式对值进行排序和比较

TINYBLOB, TINYTEXT

TINYBLOB, TINYTEXT

A TINYBLOBor TINYTEXTcolumn with a maximum length of 255 (28- 1) characters

最大长度为 255 (2 8- 1) 个字符的ATINYBLOBTINYTEXT

BLOB, TEXT

斑点、文本

A BLOBor TEXTcolumn with a maximum length of 65,535 (216- 1) characters , bytes = 64 KiB

最大长度为 65,535 (2 16- 1) 个字符的ABLOBTEXT列,字节 = 64 KiB

MEDIUMBLOB, MEDIUMTEXT

中等斑点,中等文本

A MEDIUMBLOBor MEDIUMTEXTcolumn with a maximum length of 16,777,215 (224- 1)characters , bytes = 16 MiB

最大长度为 16,777,215 (2 24- 1) 个字符的AMEDIUMBLOBMEDIUMTEXT列,字节 = 16 MiB

LONGBLOB, LONGTEXT

长块,长文本

A LONGBLOBor LONGTEXTcolumn with a maximum length of 4,294,967,295 (232- 1) characters , bytes = 4 GiB

最大长度为 4,294,967,295 (2 32- 1) 个字符的ALONGBLOBLONGTEXT列,字节 = 4 GiB

See MySQL Data Types Quick Reference Tablefor more info.

有关更多信息,请参阅MySQL 数据类型快速参考表

also you can see MYSQL - String Type Overview

你也可以看到MYSQL - 字符串类型概述

回答by Pekka

See the mySQL manual on String Types.

请参阅有关字符串类型的 mySQL 手册。

Varchar (String):

变量(字符串):

Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 255 before MySQL 5.0.3, and 0 to 65,535 in 5.0.3 and later versions. The effective maximum length of a VARCHAR in MySQL 5.0.3 and later is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.

VARCHAR 列中的值是可变长度的字符串。在 MySQL 5.0.3 之前,长度可以指定为 0 到 255 之间的值,在 5.0.3 及更高版本中可以指定为 0 到 65,535。MySQL 5.0.3 及更高版本中 VARCHAR 的有效最大长度受最大行大小(65,535 字节,在所有列之间共享)和使用的字符集的约束。

Text: See storage requirements

文本:请参阅存储要求

If you want a fixed size text field, use CHARwhich can be 255 characters in length maximum. VARCHARand TEXTboth have variable length.

如果您需要固定大小的文本字段,请使用CHAR最多 255 个字符的长度。VARCHAR并且TEXT两者都有可变长度。