MySQL VARCHAR字段最大長度到底是多少


varchar(n),n表示什么?
MySQL5.0.3之前varchar(n)這里的n表示字節數

MySQL5.0.3之后varchar(n)這里的n表示字符數,比如varchar(200),不管是英文還是中文都可以存放200個

 

n最大可以是多少
MySQL行長度
MySQL要求一個行定義長度不能超過65535個字節,不包括text、blob等大字段類型,varchar長度受此長度限制,和其他非大字段加起來不能超過65535個字節.

超過以上限制則會報錯:

 

 

drop table if EXISTS test1111;
create table test1111(
id char(255) null,
content varchar(21830) null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

 

[Err] 1118 - Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs

varchar(n)占用幾個字節
varchar(n)占用幾個字節跟字符集有關系:

字符類型若為gbk,每個字符占用2個字節

字符類型若為utf8,每個字符最多占用3個字節

varchar最大長度可以是多少
根據字符集,字符類型若為gbk,每個字符占用2個字節,最大長度不能超過32766,字符類型若為utf8,每個字符最多占用3個字節,最大長度不能超過21845,若超過這個限制,則會自動將varchar類型轉為mediumtext或longtext,例如:

 

drop table if EXISTS test1111;
create table test1111(
id char(255) null,
content varchar(63000) null
);
desc test1111;

結果:
CREATE TABLE `test1111` (
`id` char(255) DEFAULT NULL,
`content` mediumtext
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM