I believe that this problem will be a lot of people, especially those who have used other databases (such as Oracle), I did not care too much about this issue, plus some books and online articles are not detailed enough, and have not tested, leading me to understand the error. The following is an example of an explanation of the difference between a character and a byte before you begin.
Characters people use the notation, a symbol in the abstract sense. A kanji and English is a character, such as ' 1 ', ' Medium ', ' a ', ' $ ', ' ¥ ',......
Bytes A unit of data stored in a computer, a 8-bit binary number, is a very specific storage space, such as 0x01, 0x45, 0xFA,......
Details can be consulted.Http://www.regexlab.com/zh/encoding.htm Well, if you already understand the difference between characters and bytes, it's much easier.
Environment: centos5.x,mysql5.1.x
UTF8 Character set:
sql>create table test (id int auto_increment,name varchar (+), primary key (ID));
Sql>insert into test values (null, ' 1234567890 ');
Query OK, 1 row Affected (0.00 sec)
Sql>insert into test values (null, ' 1,234,567,890 ');
Query OK, 1 row Affected (0.00 sec)
Sql>insert into test values (null, ' Abcdefghig ');
Query OK, 1 row affected (0.01 sec)
Sql>insert into test values (null,12345678901);
ERROR 1406 (22001): Data too long for column ' name ' at row 1
Sql>insert to test values (null, ' 1,234,567,891 ');
ERROR 1406 (22001): Data too long for column ' name ' at row 1
Sql>insert to test values (null, ' 1,234,567,891 ');
ERROR 1406 (22001): Data too long for column ' name ' at row 1
Sql>select id,name,length (name), Char_length (name) from Test;
+----+--------------------------------+--------------+-------------------+ | id | name | length (name) | char_length (name) | +----+--------------------------------+--------------+-------------------+ | 1 | 1234567890 | 10 | 10 | | 2 | 1,234,567,890 | 30 | &Nbsp; 10 | | 3 | abcdefghig | 10 | 10 | +----+--------------------------------+--------------+-------------------+ 3 rows in set (0.00 SEC)
Under the GBK character set:
Sql>create table test (id int auto_increment,name varchar (ten), primary key (ID));
Sql>insert into test values (null, ' 1234567890 ');
Query OK, 1 row Affected (0.00 sec)
Sql>insert into test values (null, ' 1,234,567,890 ');
Query OK, 1 row Affected (0.00 sec)
Sql>insert into test values (null, ' Abcdefghig ');
Query OK, 1 row affected (0.01 sec)
Sql>insert into test values (null,12345678901);
ERROR 1406 (22001): Data too long for column ' name ' at row 1
Sql>insert into test values (null, ' 1,234,567,891 ');
ERROR 1406 (22001): Data too long for column ' name ' at row 1
Sql>insert into test values (null, ' 1,234,567,891 ');
ERROR 1406 (22001): Data too long for column ' name ' at row 1
Sql>select id,name,length (name), Char_length (name) from test;
+----+----------------------+--------------+-------------------+ | id | name | length ( Name) | char_length (name) | +----+----------------------+--------------+------------ -------+ | 1 | 1234567890 | 10 | 10 | | 2 | 1,234,567,890 | 20 | 10 | | 3 | abcdefghig | 10 | 10 | +----+----------------------+ --------------+-------------------+ 3 rows in set (0.00 sec)
Thus, the length of the varchar definition is a character, even if 1 multibyte characters are 1 characters, such as Chinese and English letters are treated as 1 characters.
So what is the maximum length that varchar can define? This is related to the character set you are currently using. The maximum length is 65535 bytes (this is the maximum row size, which is shared by all columns) and is placed under a different character set, which can be defined with a value that is a difference, such as 21845 under UTF8. It is said that MySQL5 in the length of the varchar is also a character, while the MySQL4 is a byte, unconfirmed, interested in the environment can be measured by themselves.
By the way, the char data type definition is also a character length, with a maximum length of 255.
Sql>create table test (id int auto_increment,name char (5), primary key (ID));
Query OK, 0 rows affected (0.09 sec)
Sql>insert into test values (NULL, ' 123 ');
Query OK, 1 row Affected (0.00 sec)
Sql>insert into test values (null, ' 12345 ');
Query OK, 1 row Affected (0.00 sec)
Sql>insert into test values (NULL, ' 123 ');
Query OK, 1 row Affected (0.00 sec)
Sql>insert into test values (null, ' 12345 ');
Query OK, 1 row Affected (0.00 sec)
Sql>insert into test values (null,123456);
ERROR 1406 (22001): Data too long for column ' name ' at row 1
Sql>insert into test values (null, ' 123,451 ');
ERROR 1406 (22001): Data too long for column ' name ' at row 1
Sql>select id,name,length (name), Char_length (name) from test;
+----+-----------------+--------------+-------------------+ | id | name | length (name) | char_length (name) | +----+-----------------+--------------+-------------------+ | 1 | 123 | 3 | 3 | | 2 | 12345 | 5 | 5 | | 3 | 123 | 9 | 3 | | 4 | 12345 | 15 | 5 | +----+-----------------+--------------+--------- ----------+ 4 rows in set (0.00 SEC)
MySQL's varchar defines whether the length is byte or character