Home > Database > Mysql Tutorial > body text

What is the maximum length of a MySQL VARCHAR column?

王林
Release: 2023-08-24 11:49:07
forward
1169 people have browsed it

MySQL VARCHAR 列的最大长度是多少?

#In fact, the VARCHAR data type stores variable-length character data in single-byte and multi-byte characters. The syntax for this data type is VARCHAR(n), where n is the maximum number of characters that must be specified when creating the table. Prior to MySQL 5.03, the value of n could be in the range 0 to 255, but in MySQL 5.03 and later, the value could be in the range 0 to 65,535.

The maximum number of characters stored in a VARCHAR depends on the maximum row size and the character set used. If we use ASCII character set then it can store 65,535 characters as ASCII uses 1 byte per character. On the other hand, if we use utf8 character set, the character limit will be 21,844 because utf8 will use 3 bytes per character. The maximum row size limit is 65,535 bytes, which means that including all columns, the row size must not exceed 65,535 bytes.

Example

mysql> Create Table var_test(FName Varchar(32765) NOT NULL, LName Varchar(32766)
       NOT NULL);
       Query OK, 0 rows affected (0.25 sec)
Copy after login

The above query creates a table with two columns: FName Varchar(32765) and LName Varchar(32766). So the total length is 32765 2 32766 2 = 65535 (using 2 bytes per column to store the length).

mysql> Create Table var_test1(FName varchar(32766) NOT NULL, LName Varchar(32766)
       NOT NULL);
       ERROR 1118 (42000): Row size too large. The maximum row size for the used
       table
       type, not counting BLOBs, is 65535. You have to change some columns to TEXT
       or BLOBs
Copy after login

Now, MySQL will return an error when the length is increased by 1 byte, as shown in the query above.

The above is the detailed content of What is the maximum length of a MySQL VARCHAR column?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!