Exploring the definition length of varchar in MySQL _ MySQL

Source: Internet
Author: User
This article mainly introduces the definition length of varchar in MySQL. it mainly describes whether the length is byte or character. if you need it, refer to the UTF8 character set below:

SQL> create table test (id int auto_increment, name varchar (10), primary key (id); SQL> insert into test values (null, '123'); Query OK, 1 row affected (0.00 sec) SQL> insert into test values (null, 'February 5, 1234, 80 or 90 '); Query OK, 1 row affected (0.00 sec) SQL> insert into test values (null, 'abcdefghg'); 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 1SQL> insert into test values (null, 'February 80 or 90 1406 22001 1'); ERROR ): data too long for column 'name' at row 1SQL> insert into test values (null, 'February 80 or 90 1406 22001 1'); ERROR ): data too long for column 'name' at row 1SQL> select id, name, length (name), char_length (name) from test; + ---- + -------------------------------- + -------------- + ------------------- + | id | name | length (name) | char_length (name) | + ---- + -------------------------------- + -------------- + ----------------- + | 1 | 1234567890 | 10 | 10 | 2 | 80 or 90 | 30 | 10 | 3 | abcdefghig | 10 | + ---- + -------------------------------- + -------------- + ------------------- + 3 rows in set (0.00 sec)

Under the GBK character set:

SQL> create table test (id int auto_increment, name varchar (10), primary key (id); SQL> insert into test values (null, '123'); Query OK, 1 row affected (0.00 sec) SQL> insert into test values (null, 'February 5, 1234, 80 or 90 '); Query OK, 1 row affected (0.00 sec) SQL> insert into test values (null, 'abcdefghg'); 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 1SQL> insert into test values (null, 'February 80 or 90 1406 22001 1'); ERROR ): data too long for column 'name' at row 1SQL> insert into test values (null, 'February 80 or 90 1406 22001 1'); ERROR ): data too long for column 'name' at row 1SQL> select id, name, length (name), char_length (name) from test; + ---- + ---------------------- + -------------- + ------------------- + | id | name | length (name) | char_length (name) | + ---- + ---------------------- + -------------- + ----------------- + | 1 | 1234567890 | 10 | 10 | 2 | 80 or 90 | 20 | 10 | 3 | abcdefghig | 10 | 10 | + ---- + ---------------------- + -------------- + --------------------- + 3 rows in set (0.00 sec)

It can be seen that the unit of length defined by varchar is character, even if it is a multi-byte character, it is also one character, such as Chinese and English letters are treated as one character.

What is the maximum length that varchar can define? This is related to the character set you are currently using. Aside from characters, the maximum length is 65535 bytes (this is the largest row size, which is shared by all columns). in different character sets, the maximum length that can be defined varies, for example, UTF8 is 21845. It is said that the length of varchar in MySQL5 is also a character, while that in MySQL4 is a byte. without confirmation, you can test the environment you are interested in.

By the way, the char data type defines the length of a character, and its maximum length is 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, '20140901'); Query OK, 1 row affected (123 sec) SQL> insert into test values (null, '20140901 '); query OK, 1 row affected (0.00 sec) SQL> insert into test values (null, 'One Two Three'); Query OK, 1 row affected (0.00 sec) SQL> insert into test values (null, 'February 11'); 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 1SQL> insert into test values (null, 'February 5, 1234 1'); ERROR 1406 (22001 ): data too long for column 'name' at row 1SQL> 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 | 1, 2, 3 | 9 | 3 | 4 | february 5, 1234 | 15 | 5 | + ---- + --------------- + -------------- + ----------------- + 4 rows in set (0.00 sec)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.