当前位置:网站首页>[MySQL string data type optimization] the difference between char and varchar
[MySQL string data type optimization] the difference between char and varchar
2022-06-09 18:15:00 【InfoQ】
Preface
char and varchar difference 、 Details and usage scenarios char And varchar contrast
- char(size),
Fixed lengthcharacter string , Maximum 255 character .
- varchar(size),
Variable lengthcharacter string , Maximum ==65535 byte ==( Notice that here are bytes , Characters occupy different bytes in different encoding rules ), If the table code is not definedThe default is utf8【utf8 Maximum coding 21844 character ;gdk Code up to 32766 character 】
- ( Pay attention to characters, not bytes )
- char Assign fixed characters ,char(4) Occupy four characters , Save a character , It will also occupy four characters in memory .
- varchar
Allocate space according to actual use, Still need to use 1-3 Bytes to record the length of the stored content ,Total occupancy = L( The actual data size )+(1-3) Bytes.
Code samples
CREATE TABLE t4(
`name` CHAR(255));
# Create a table t4, Definition name The length of is 255 character
INSERT INTO t4 VALUES('ABCD');
INSERT INTO t4 VALUES(' I am you D');
# Add English... To the table 、 Chinese is OK
CREATE TABLE t5(
`name` VARCHAR(32766)) CHARSET gbk;
# Define code as gdk, Maximum length is 32766 character , because gbk Chinese accounts for 2 Bytes , English 1 Bytes
CREATE TABLE t6(
`name` VARCHAR(21844));
# Define the code as the default utf8, Maximum length is 21844 character , because utf-8 Chinese accounts for 3 Bytes , English 1 Bytes
char And varchar Details of the use of
- char(4), This 4 Indicates the number of characters , Non byte number , There are four letters in both Chinese and English ( The occupied character space is fixed ).
- varchar(4), This 4 Also represents the number of characters , But the letters or Chinese characters stored here are
Calculate according to the code when defining the table( The occupied character space is not fixed ).
char and varchar The use of
Fixed data length , recommend char. for example md5 password ( Fix 32 by )、 Zip code 、 cell-phone number 、 ID number, etc .2.The length is uncertain , Use varchar. E.g. message 、 Articles, etc .
边栏推荐
- GCN图卷积神经网络概述
- Function anti shake for performance optimization in JS
- MySQL 8.0.29 decompressed version installation and configuration method graphic tutorial
- Operating instructions for abbexa AEC chromogen Kit
- Squeeze-and-Excitation Networks学习笔记
- Transformer模型新SOTA--fully attentional networks (FANs) 学习笔记
- ZigBee组网从未如此简单!
- 美化Aria主题媲美收费主题Mirages
- Macro definition CV with parameters in opencv_ Role of assert()
- 利用go破解带密码的rar压缩文件
猜你喜欢

Abbexa PCR 超级混合液使用说明书

Process control -- > > process termination

Abbexa plasmid miniprep kit detection procedure

在线沙龙 | 开源小秀场——数据库技术应用实践

What are the main applications of conductive slip rings

Interpretation of new shares | ranked second in the event content marketing industry, and wanted to push SaaS products on the cloud to create a competitive barrier

Scala基本语法学习-1

Operation manual of abbexa PCR super mixture

如何实现自定义富文本编辑器标签

redis源码学习-03_动态字符串SDS
随机推荐
刷脸认证如何实现人脸又快又准完成校验?
Development and practice of the martyr's family search system
ZigBee组网从未如此简单!
NLP text representation word bag model and TF-IDF
10分钟快速入门RDS【华为云至简致远】
logrotate
js中性能优化之函数节流
ElasticSerach
【高等数学笔记】格林公式、高斯公式、斯托克斯公式、场论
空闲内存的管理
How to realize face verification quickly and accurately?
Introduction to Multivariate Statistics
【数据处理】pandas读取sql数据
NLP-RNN
Redis基础与高级
KVM虚拟化基本原理
智充推出NET ZERO SERIES储能充一体机,携手比亚迪共创净零未来
直播预告 | 在阿里云 ESSD 云盘上部署 PolarDB for PostgreSQL 集群
线程中断
mysql版本驱动问题