我表里一个列name是char(8),我插入一条记录name是“tom”,我感觉我查length(name)结果应该是8,因为不足8的,后边被补5个空格,这就叫固定长度。
但是事实我查length(name)结果是3,不应该啊?

解决方案 »

  1.   

    还真没注意这个问题,原来的理解和楼主是一样的,但官网文档上是这样写的
    https://dev.mysql.com/doc/refman/5.7/en/char.html
    If a given value is stored into the CHAR(4) and VARCHAR(4) columns, the values retrieved from the columns are not always the same because trailing spaces are removed from CHAR columns upon retrieval. The following example illustrates this difference: 
      

  2.   

     The length of a CHAR column is fixed to the length that you declare when you create the table. The length can be any value from 0 to 255. When CHAR values are stored, they are right-padded with spaces to the specified length. When CHAR values are retrieved, trailing spaces are removed unless the PAD_CHAR_TO_FULL_LENGTH SQL mode is enabled.可以试着先执行 set session sql_mode = ’ PAD_CHAR_TO_FULL_LENGTH‘再执行length函数试试看结果