mysql varchar(max)_等价于MySQL中的varchar(max)?
小編典典
varchar的最大長度取決于MySQL中的最大行大小,即64KB(不包括BLOB):
VARCHAR(65535)
但是,請注意,如果使用多字節字符集,則限制會更低:
VARCHAR(21844) CHARACTER SET utf8
這里有些例子:
最大行大小為65535,但是varchar還包括一個或兩個字節來編碼給定字符串的長度。因此,即使它是表中的唯一列,您實際上也無法聲明最大行大小的varchar。
mysql> CREATE TABLE foo ( v VARCHAR(65534) );
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs
但是,如果我們嘗試減少長度,我們會找到最大的長度:
mysql> CREATE TABLE foo ( v VARCHAR(65532) );
Query OK, 0 rows affected (0.01 sec)
現在,如果我們嘗試在表級別使用多字節字符集,則會發現它會將每個字符都計為多個字節。UTF8字符串 不一定
每個字符串都使用多個字節,但是MySQL無法假定您將所有以后的插入都限制為單字節字符。
mysql> CREATE TABLE foo ( v VARCHAR(65532) ) CHARSET=utf8;
ERROR 1074 (42000): Column length too big for column 'v' (max = 21845); use BLOB or TEXT instead
盡管最后一個錯誤告訴我們,InnoDB仍然不喜歡21845的長度。
mysql> CREATE TABLE foo ( v VARCHAR(21845) ) CHARSET=utf8;
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs
如果您計算得出21845 * 3 = 65535,那還是行得通的。而21844 * 3 = 65532,就可以了。
mysql> CREATE TABLE foo ( v VARCHAR(21844) ) CHARSET=utf8;
Query OK, 0 rows affected (0.32 sec)
2020-05-17
總結
以上是生活随笔為你收集整理的mysql varchar(max)_等价于MySQL中的varchar(max)?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: laravel mysql json_m
- 下一篇: python 匹配字符串多个_在Pyth