varchar字段的长度

mysql> create table t0008(col1 varchar(65535))charset=latin1;

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 t0008(col1 varchar(65534))charset=latin1;
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 t0008(col1 varchar(65533))charset=latin1;
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 t0008(col1 varchar(65532))charset=latin1;
Query OK, 0 rows affected (0.02 sec)

latin1字符集下的表varchar上限为65532,即一个字符一个字节


mysql> create table t0009(col1 varchar(65533))charset=utf8;
ERROR 1074 (42000): Column length too big for column 'col1' (max = 21845); use BLOB or TEXT instead
mysql> create table t0009(col1 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
mysql> create table t0009(col1 varchar(21844))charset=utf8;
Query OK, 0 rows affected (0.00 sec)


utf8字符集下的表varchar上限为21844,即一个字符三个字节 65535-1-2 结果除以3 ==21844

 -1表示第一个字节不存数据,-2表示两个字节存放varchar的长度,除以3是utf8字符特性,一个字符三个字节。

varchar 字段是将实际内容单独存储在聚簇索引之外,内容开头用1到2个字节表示实际长度(长度超过255时需要2个字节),因此最大长度不能超过65535即 2的16次方(0-65535)


varchar:变长,最大65535个字符(既是单列的限制,又是整行的限制)

mysql> create table t0012(id int,name char(20),col3 varchar(N))chaset=utf8;

N的值为:(65535-1-2-4-20*3)/3=21822


mysql> create table t0012(id int,name char(20),col3 varchar(N))charset=latin1;

N的值为:65535-1-2-4-20=65508


length():Return the length of a string in bytes.返回字符串所占的字节数(受编码影响).

 utf8编码下,一个汉字是占三个字节,一个数字或字母占一个字节。其他编码下,一个汉字两个字, 一个数字或字母一个字

char_length():Return number of characters in argument.返回字符串中的字符数.在任何编码下, 不管汉字还是数字或者是字母都算是一个字符

示例:

mysql> show create table t2;
+-------+-------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                            |
+-------+-------------------------------------------------------------------------------------------------------------------------+
| t2    | CREATE TABLE `t2` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(60) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+----------------------------
INSERT INTO t2(id,NAME) VALUES(1,'丹丹');
INSERT INTO t2(id,NAME) VALUES(2,'dandan');
SELECT id,NAME,CHAR_LENGTH(NAME),LENGTH(NAME) FROM t2;
    id  name    char_length(name)  length(name)  
------  ------  -----------------  --------------
     1  丹丹                      2               6
     2  dandan                  6               6

char和varchar比较:

char:固定长度,(索引)效率极高,不存在碎片
凡是固定长度的字符串或者类似固定长度的字符串一律用char。比如身份证号码,手机号码,银行卡号,MD5,哈希值等这是字符串是固定长度的,毫无疑问用char,还有一类是基本固定长度但是略有出入的,比如中国人的姓名等,一般长度可能是2~5个汉字,这类信息也非常适合用char来存储,只要分配一些略大于通常长度即可。
varchar是存储可变长度的字符串,简单的说我们定义表机构的时候指定的字段长度是最大长度,当字符串没有达到最大长度的时候以字符串的实际长度来存储的,不占用多余的存储空间。因此,一般情况下,varchar比char节省存储空间,但当修改后的数据大于当前存储长度时,就会产生碎片

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值