天天看點

MySQL VARCHAR的最大大小是多少?

本文翻譯自:What is the MySQL VARCHAR max size?

I would like to know what the max size is for a MySQL VARCHAR type.

我想知道MySQL VARCHAR類型的最大大小是多少。

I read that the max size is limited by the row size which is about 65k.

我讀到最大大小受行大小限制,大約65k。

I tried setting the field to

varchar(20000)

but it says that that's too large.

我嘗試将字段設定為

varchar(20000)

但它表示太大。

I could set it to

varchar(10000)

.

我可以将其設定為

varchar(10000)

What is the exact max I can set it to?

我可以将其設定為多少?

#1樓

參考:https://stackoom.com/question/UFK8/MySQL-VARCHAR的最大大小是多少

#2樓

您可以使用

TEXT

類型 ,但不限于64KB。

#3樓

you can also use MEDIUMBLOB/LONGBLOB or MEDIUMTEXT/LONGTEXT

您還可以使用MEDIUMBLOB / LONGBLOB或MEDIUMTEXT / LONGTEXT

A BLOB type in MySQL can store up to 65,534 bytes, if you try to store more than this much data MySQL will truncate the data.

MySQL中的BLOB類型最多可存儲65,534個位元組,如果您嘗試存儲的資料量過多,則MySQL将截斷該資料。

MEDIUMBLOB can store up to 16,777,213 bytes, and LONGBLOB can store up to 4,294,967,292 bytes.

MEDIUMBLOB最多可以存儲16,777,213位元組,而LONGBLOB最多可以存儲4,294,967,292位元組。

#4樓

Keep in mind that MySQL has a maximum row size limit

請記住,MySQL具有最大行大小限制
The internal representation of a MySQL table has a maximum row size limit of 65,535 bytes, not counting BLOB and TEXT types. MySQL表的内部表示形式的最大行大小限制為65,535位元組,不包括BLOB和TEXT類型。 BLOB and TEXT columns only contribute 9 to 12 bytes toward the row size limit because their contents are stored separately from the rest of the row. BLOB和TEXT列僅對行大小限制貢獻9到12個位元組,因為它們的内容與行的其餘部分分開存儲。 Read more about Limits on Table Column Count and Row Size. 閱讀有關表列數和行大小限制的更多資訊。

Maximum size a single column can occupy, is different before and after MySQL 5.0.3

單列可占用的最大大小,在MySQL 5.0.3前後不同
Values in VARCHAR columns are variable-length strings. VARCHAR列中的值是可變長度的字元串。 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. 在MySQL 5.0.3之前,長度可以指定為0到255之間的值,而在5.0.3和更高版本中,長度可以指定為0到65535之間的值 。 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. 在MySQL 5.0.3及更高版本中,VARCHAR的有效最大長度取決于最大行大小(65,535位元組,在所有列之間共享)和所使用的字元集。

However, note that the limit is lower if you use a multi-byte character set like utf8 or utf8mb4.

但是,請注意,如果使用多位元組字元集(如utf8或utf8mb4),則該限制會降低。

Use

TEXT

types inorder to overcome row size limit.

使用

TEXT

類型以克服行大小限制。
The four TEXT types are TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT. 四種TEXT類型是TINYTEXT,TEXT,MEDIUMTEXT和LONGTEXT。 These correspond to the four BLOB types and have the same maximum lengths and storage requirements. 這些對應于四種BLOB類型,并且具有相同的最大長度和存儲要求。

More details on BLOB and TEXT Types

有關BLOB和TEXT類型的更多詳細資訊
  • Ref for MySQLv8.0 https://dev.mysql.com/doc/refman/8.0/en/blob.html MySQLv8.0的參考https://dev.mysql.com/doc/refman/8.0/en/blob.html
  • Ref for MySQLv5.7 http://dev.mysql.com/doc/refman/5.7/en/blob.html 參考MySQLv5.7 http://dev.mysql.com/doc/refman/5.7/en/blob.html
  • Ref for MySQLv5.6 http://dev.mysql.com/doc/refman/5.6/en/blob.html 參考MySQLv5.6 http://dev.mysql.com/doc/refman/5.6/en/blob.html
  • Ref for MySQLv5.5 http://dev.mysql.com/doc/refman/5.5/en/blob.html 參考MySQLv5.5 http://dev.mysql.com/doc/refman/5.5/en/blob.html
  • Ref for MySQLv5.1 http://dev.mysql.com/doc/refman/5.1/en/blob.html MySQLv5.1的參考http://dev.mysql.com/doc/refman/5.1/en/blob.html
  • Ref for MySQLv5.0 http://dev.mysql.com/doc/refman/5.0/en/blob.html MySQLv5.0的參考http://dev.mysql.com/doc/refman/5.0/en/blob.html

Even more

Checkout more details on Data Type Storage Requirements which deals with storage requirements for all data types.

檢視有關“ 資料類型存儲要求”的更多詳細資訊,該資訊處理所有資料類型的存儲要求。

#5樓

As per the online docs , there is a 64K row limit and you can work out the row size by using:

根據線上文檔 ,行限制為64K,您可以使用以下方法計算行大小:
row length = 1
             + (sum of column lengths)
             + (number of NULL columns + delete_flag + 7)/8
             + (number of variable-length columns)
           

You need to keep in mind that the column lengths aren't a one-to-one mapping of their size.

您需要記住,列長度不是其大小的一對一映射。

For example,

CHAR(10) CHARACTER SET utf8

requires three bytes for each of the ten characters since that particular encoding has to account for the three-bytes-per-character property of

utf8

(that's MySQL's

utf8

encoding rather than "real" UTF-8, which can have up to four bytes).

例如,

CHAR(10) CHARACTER SET utf8

的十個字元中的每個字元都需要三個位元組,因為該特定編碼必須考慮

utf8

的每個字元三位元組的屬性(這是MySQL的

utf8

編碼,而不是“真實的” UTF- 8,最多可以包含四個位元組)。

But, if your row size is approaching 64K, you may want to examine the schema of your database.

但是,如果行大小接近64K,則可能需要檢查資料庫的架構。

It's a rare table that needs to be that wide in a properly set up (3NF) database - it's possible, just not very common.

在一個适當設定的(3NF)資料庫中,這是一個罕見的表,它需要那麼寬-有可能,隻是不是很常見。

If you want to use more than that, you can use the

BLOB

or

TEXT

types.

如果要使用更多類型,可以使用

BLOB

TEXT

類型。

These do not count against the 64K limit of the row (other than a small administrative footprint) but you need to be aware of other problems that come from their use, such as not being able to sort using the entire text block beyond a certain number of characters (though this can be configured upwards), forcing temporary tables to be on disk rather than in memory, or having to configure client and server comms buffers to handle the sizes efficiently.

這些不計入該行的64K限制(除了很小的管理占用空間),但是您需要意識到使用它們所産生的其他問題,例如無法使用超出一定數量的整個文本塊進行排序字元(盡管可以向上配置),迫使臨時表在磁盤上而不是在記憶體中,或者必須配置用戶端和伺服器的通訊緩沖區來有效地處理大小。

The sizes allowed are:

允許的大小為:
TINYTEXT          255 (+1 byte  overhead)
TEXT          64K - 1 (+2 bytes overhead)
MEDIUMTEXT    16M - 1 (+3 bytes overhead)
LONGTEXT      4G  - 1 (+4 bytes overhead)
           

You still have the byte/character mismatch (so that a

MEDIUMTEXT utf8

column can store "only" about half a million characters,

(16M-1)/3 = 5,592,405

) but it still greatly expands your range.

您仍然存在位元組/字元不比對的情況(是以

MEDIUMTEXT utf8

列可以“僅”存儲大約一百萬個字元,

(16M-1)/3 = 5,592,405

),但是它仍然大大擴充了您的範圍。

#6樓

Source

資源
The max length of a varchar is subject to the max row size in MySQL, which is 64KB (not counting BLOBs): varchar的最大長度取決于MySQL中的最大行大小,即64KB(不包括BLOB): VARCHAR(65535) However, note that the limit is lower if you use a multi-byte character set: VARCHAR(65535)但是,請注意,如果使用多位元組字元集,則該限制會降低: VARCHAR(21844) CHARACTER SET utf8 VARCHAR(21844)字元集utf8

繼續閱讀