mysql 1351,MySQL下varchar最大存储字节数

MySQL中的varchar最大能放多少字符呢?看官方文档有如下说明:

Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535. The effective maximum length of aVARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used. See Section C.10.4, “Limits on Table Column Count and Row Size”.

varchar类型的列是可变长的字符串。长度可以是0-65535。varchar有效的最大长度受限于最大的行大小(所以列加起来最大65535个字节)和字符集。

另外官方文档还有如下说明:

The following table illustrates the differences between CHAR and VARCHAR by showing the result of storing various string values into CHAR(4) and VARCHAR(4) columns (assuming that the column uses a single-byte character set such as latin1).

Value

CHAR(4)

Storage Required

VARCHAR(4)

Storage Required

''

'    '

4 bytes

''

1 byte

'ab'

'ab  '

4 bytes

'ab'

3 bytes

'abcd'

'abcd'

4 bytes

'abcd'

5 bytes

'abcdefgh'

'abcd'

4 bytes

'abcd'

5 bytes

通过上表可知道:

对于单字节的varchar(4)中的4是4个字符,而存储占用空间会多一个字节。如果是utf-8的则根据对应的字符占不同的字节数。

有了上面的理论知识,来实践一下:

先查询自己的数据库默认编码

mysql> show variables like 'character_set_database';+------------------------+-------+| Variable_name| Value |+------------------------+-------+| character_set_database | utf8|+------------------------+-------+1 row in set (0.09 sec)

建表

mysql> create table test_varchar(v1 varchar(21844));Query OK, 0 rows affected (0.03 sec)

如果长度设置成21845则会报错

mysql> create table test_varchar(v1 varchar(65535));ERROR 1074 (42000): Column length too big for column 'v1' (max = 21845); use BLOB or TEXT insteadmysql> create table test_varchar(v1 varchar(21845));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中varchar在utf-8最大长度为:21844

喜欢 (1)or分享 (0)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值