MySQL 中的 VARCHAR(255) 和 TINYTEXT 字符串类型有什么区别?

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

What's the difference between VARCHAR(255) and TINYTEXT string types in MySQL?

mysqltypes

提问by planetp

What's the difference between VARCHAR(255) and TINYTEXT string types in MySQL?

MySQL 中的VARCHAR(255) 和 TINYTEXT 字符串类型有什么区别?

Each of them allows to store strings with a maximum length of 255 characters. Storage requirements are also the same. When should I prefer one over another?

它们中的每一个都允许存储最大长度为 255 个字符的字符串。存储要求也相同。我什么时候应该更喜欢一个?

采纳答案by Quassnoi

You cannot assign a DEFAULTvalue to a TINYTEXTand you cannot create an unprefixed index on the latter.

您不能为 aDEFAULT赋值,TINYTEXT也不能在后者上创建无前缀的索引。

Internally, additional objects are allocated in memory to handle TEXT(incl. TINYTEXT) columns which can cause memory fragmentation on the large recordsets.

在内部,额外的对象在内存中分配以处理TEXT(包括TINYTEXT)列,这可能导致大记录集上的内存碎片。

Note that this only concerns the column's internal representation in the recordsets, not how they are stored on the disk.

请注意,这仅涉及记录集中列的内部表示,而不涉及它们在磁盘上的存储方式。

回答by Donny Kurnia

Using VARCHARyou can set the column to NULLor NOT NULLand you can set DEFAULTvalue, but not with TEXT. Use VARCHARif you need one or both feature, NULLand DEFAULT.

使用VARCHAR您可以将列设置为NULLorNOT NULL并且您可以设置DEFAULT值,但不能使用TEXTVARCHAR如果您需要一项或两项功能,请使用,NULL并且DEFAULT.

回答by nilesh

in varchar you have to set the length of a character whereas in tanytext there is nothing like this it saves the memory of data base for ex:

在 varchar 中,您必须设置字符的长度,而在 tanytext 中没有这样的东西,它可以节省数据库的内存,例如:

for address you have to define the varchar(50) than your address may be 50 charecter or less the worse condition is your character more than the 50 character this is the limitation of varchar if character is less than 50 than it occupy the 50 character memory in this case memory is increases

对于地址,您必须定义 varchar(50) 比您的地址可能是 50 个字符或更少更糟糕的情况是您的字符超过 50 个字符这是 varchar 的限制,如果字符小于 50 则它占用 50 个字符的内存在这种情况下,内存增加

so use tanytext it define the character length depend upon the size of character so memory is saved

所以使用 tanytext 它定义字符长度取决于字符的大小,因此可以节省内存