数据库
    mysql中经常用到的字段类型就是varchar和char。一般还会指定长度用来规定最长可以支持多长的内容。那么问题就来了。对于utf8字符集来讲。一个字符所占的空间是1-6字节不等的。那么varchar(100)是指的字节数还是字符数呢?


    可以通过mysql官方文档来看:
    http://dev.mysql.com/doc/refman/5.0/en/char.html
    

The CHAR and VARCHAR types are declared with a length that indicates the maximum number of characters you want to store. For example, CHAR(30) can hold up to 30 characters.

    即支持的是30个字符(characters),而不是30字节(bytes)

    经测试,结果也是符合文档的。varchar(30)可以存储30个汉字


    附加问题:如果传入了超出长度的内容,会有什么结果。

If strict SQL mode is not enabled and you assign a value to a CHAR or VARCHAR column that exceeds the column's maximum length, the value is truncated to fit and a warning is generated.

     文档中提到。默认情况下会截断而不返回错误。如果需要返回错误,需要开启strict mode










Tags: ,
分页: 1/1 第一页 1 最后页 [ 显示模式: 摘要 | 列表 ]