How to check the length field Mysql, Mysql in length (), char_length () difference

1, today, an interesting thing happened, data transmission is greater than the standard length of a given field, I cranked up the field length, data transmission over. Who knows, other people's data does not meet the standard, you want to delete the re-engage, then you how long it deleted data, or the query data out long.

  A: spoilers about, in fact, use char_length () check out, you can put these deleted, and then turn up field length of a little tune on the line. Note, I practice fields are Chinese characters Ha, do not write a tutorial written murder.

2, first look, Mysql in length (), char_length () difference.

  1), length (): mysql inside the length () function is used to obtain a built-in function of the length of the string.
  2), char_length (): See string length in mysql there is also a built-in function is a function char_length ().
  3), the difference between these two functions is:

    a), length (): in bytes, the encoding utf8, three bytes of a character, a number or letter one byte. Under gbk coding, two bytes of a character, a number or letter one byte.
    b), char_length (): units of characters, either letters or numbers or characters are regarded as a character.


3, extend it, varchar type of change after MySQL5.0.3 version.

  1)、MySQL 5.0.3 之前:0--255字节,如:varchar(20)中的20表示字节数,如果存放utf-8编码的话只能放6个汉字。varchar(n),这里的n表示字节数。
    MySQL 5.0.3 之后:0--65535字节,varchar(20)表示字符数,不管什么编码,既汉字也能放20个。但最多占65532字节(两个字节存放长度,小于255字节用1个字节存放长度),varchar(n)这里的n表示字符数,比如varchar(200),不管是英文还是中文都可以存放200个。其他具体区别,可自行网上查询。

4、length()<>char_length(),可以用来检验是否含有中文字符。

utf-8编码中判定某个字段为全英文,length(字段) = char_length(字段)即可。

 

Guess you like

Origin www.cnblogs.com/biehongli/p/12389418.html