当前位置:网站首页>Mysql——》varchar
Mysql——》varchar
2022-06-10 00:28:00 【小仙。】
推荐链接:
总结——》【Java】
总结——》【JVM】
总结——》【Mysql】
总结——》【Spring】
总结——》【SpringBoot】
Mysql——》varchar
一、varchar介绍
VARCHAR列中的值为可变长字符串。VARCHAR的最大有效长度由最大行大小和使用的字符集确定。
| 版本 | varchar(n) | varchar(20) | 长度范围(字节) | 最大长度(字节) |
|---|---|---|---|---|
| MySQL 5.0.3 之前 | n:字节数 | 最多存储20个字节 utf-8编码:只能放6个汉字 | 0-255 | 255 |
| MySQL 5.0.3 之后 | n:字符数 | 最多存储20个字符 跟编码无关 | 0-65535 | 65535:规定的最大长度 65533:不允许非空字段的时候 65532:允许非空字段的时候 |
1、为什么 varchar 最大长度是 65535 个字节?
Q:为什么 varchar 最大长度是 65535 个字节?
A:因为mysql 规定了每行数据大小不能超过 65535 个字节。
2、varchar 用几个字节存放长度?
Q:varchar 用几个字节存放长度?
A:
小于255字节,用1个字节存放长度
大于255字节,用2个字节存放长度
3、varchar用几个字节标识字段是否为空
Q:一个字段如果允许为 null,用几个字节来标识?
A:用1个字节
二、varchar最大字符长度的测试
varchar 最大字符长度 = (行最大字节数(65535) - null 标识字节数 - 长度前缀字节数(1或2)) / 单字符占用最多字节数
1、场景
创建表:test
字符集:UTF8
字段:name,非空, char(255)
字段:address,非空,varchar(n),n是咱们即将见证的最大字符长度
2、计算
每行数据的最大字节数:**65535 **
char(255)最大字节数:255*3(一个字符最多占 3 个字节)
varchar(n)存放长度字节数:2(超过255字节用2个字节存放长度)
字段为非空:0(如果字段允许null,则需要1个字节存储null标识)
varchar(n)的最大字节数 = **65535 - **255*3 - 2 = 64768
varchar(n)的最大字符数 = 字节数/3
n = 64768/3 = 21589.33
3、测试
-- n=21589.33,创建表成功
create table test1 (name char(255) not null, address varchar(21589.33) not null );
-- n=21589.34,创建表成功
create table test2 (name char(255) not null, address varchar(21589.34) not null );
-- n=21589,创建表成功
create table test3 (name char(255) not null, address varchar(21589) not null );
-- n=21590,创建表失败
create table test4 (name char(255) not null, address varchar(21590) not null );
(1)n=21589.33,创建表成功


(2)n=21589.34,创建表成功


(3)n=21589,创建表成功


(4)n=21590,创建表失败

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
边栏推荐
- Sword finger offer II 019 Delete at most one character to get a palindrome
- 劍指 Offer II 018. 有效的回文
- Rhcsa day 2
- Intranet penetration concise notes
- Sword finger offer II 012 The sum of left and right subarrays is equal
- CSV files of fer2013 are converted to Imagenet format (saved in folders of their respective categories in image format)
- Transformer
- run npm fund for details
- 剑指 Offer II 012. 左右两边子数组的和相等
- 剑指 Offer II 014. 字符串中的变位词
猜你喜欢

Rip experiment

ECA-Net: Efficient Channel Attention for Deep Convolutional Neural Networks
Bubble sorting and optimization clear and intuitive C language

Learning notes of zhouzhihua in machine learning

剑指 Offer II 020. 回文子字符串的个数
Solution to the C language problem of force deduction self divisor
Analysis on the scores of previous real questions [11th] [provincial competition] [group B]

代码案例—— web版表白墙及文件上传

Internal network infiltration tunnel

Rhcsa day 3
随机推荐
Transformer
University of Ulm, Germany | comparative characterization of 3D protein structure
Weights of complete binary tree of past real questions [10th] [provincial competition] [group B]
Spanning tree protocol summary
重发布实验
力扣 两数之和 C语言 题解
剑指 Offer II 015. 字符串中的所有变位词
ospf总结
Sword finger offer II 015 All modifiers in the string
mpls vpn
run npm fund for details
fer2013的csv文件转换为ImageNet格式(以图片格式保存在各自类别的文件夹中)
Learning notes of zhouzhihua in machine learning
Where is the precise position of PS ruler adjustment
The binary tree is expanded into a linked list [the essence of tree processing -- how to handle each sub tree]
Sword finger offer II 012 The sum of left and right subarrays is equal
Encryption, decryption and signature verification
What is the WPS merge cell shortcut
剑指 Offer II 010. 和为 k 的子数组
How to keep the contents of WPS merged cells in one cell