MySQL varchar(N)

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
云数据库 RDS PostgreSQL,高可用系列 2核4GB
简介:

 VARCHAR(N)CHAR(N)

两则之间最大区别是在于前者是可变长度,后者是定长,面试中经常问及N代表什么意思,,

The CHAR and VARCHAR types are declared with a length that indicates the maximum number of characters you want to store. For example, CHAR(30) can hold up to 30 characters.

自己理解为可以存储的字符数,如char(30)可以存储30个字符

In contrast to CHAR, VARCHAR values are stored as a 1-byte or 2-byte length prefix plus data. The length prefix indicates the number of bytes in the value. A column uses one length byte if values require no more than 255 bytes, two length bytes if values may require more than 255 bytes. 这个存储的时候需要额外的1或者2byte,最大字节255

The following table illustrates the differences between CHAR and VARCHAR by showing the result of storing various string values into CHAR(4) and VARCHAR(4) columns (assuming that the column uses a single-byte character set such as latin1).

wKioL1ZQpw-iUY9zAAAzyicM-3g331.png

以上这个存储的字节数是以latin1为标准举例说明的

1
2
3
4
5
6
7
8
9
There is a hard limit of 4096 columns per table, but the effective maximum may be less for a given
table. The exact limit depends on several interacting factors.
  Every table (regardless of storage engine) has a maximum row size of 65,535 bytes. Storage
engines may place additional constraints on this limit, reducing the effective maximum row size.
The maximum row size constrains the number (and possibly size) of columns because the total
length of all columns cannot exceed this size. For example, utf8 characters require up to three
bytes per character, so for a CHAR(255) CHARACTER SET utf8 column, the server must allocate
255 × 3 = 765 bytes per value. Consequently, a table cannot contain more than 
65,535 / 765 = 85 such columns.

一个表最大栏位可以有4096个,每个记录可以最大有65535字节存储。
举例char(255) uft8,每个栏位可以765字节,可以有85个栏位。

1
2
3
Storage  for  var iable-length columns includes length bytes, which are assessed against 
the row size.For example, a VARCHAR( 255 ) CHARACTER SET utf8 column takes two bytes to store the length
of the value, so  each  value can take up to  767  bytes.

但是对于varchar类型需要有额外的2byte存储,所以每个栏位需要767byte。

1
2
3
4
5
6
BLOB and TEXT columns count from one to four plus eight bytes  each  toward the row-size 
limit because their contents are stored separately from the rest of the row.
Declaring columns NULL can reduce the maximum number of columns permitted. For MyISAM
tables, NULL columns require additional space  in  the row to record whether their values 
are NULL.Each NULL column takes one bit extra, rounded up to the  nearest  byte. 
The maximum row length  in  bytes can be calculated  as  follows:每行最大byte计算方法
1
2
3
4
5
6
7
row length =  1
+ (sum of column lengths)
+ (number of NULL columns + delete_flag +  7 )/ 8
+ (number of  var iable-length columns)
delete_flag  is  1  for  tables  with  static  row format. Static tables  use  a bit  in  the row 
record  for  a flag that indicates whether the row has been deleted. delete_flag  is  0  for 
dynamic  tables because the flag  is  stored  in  the  dynamic  row header.

以下是关于Table max columns 及 row max bytes解释说明

These calculations do not apply for InnoDB tables. Storage size is the same for NULL and NOT
NULL columns.
The following statement to create table t1 succeeds because the columns require 32,765 + 2 bytes
and 32,766 + 2 bytes, which falls within the maximum row size of 65,535 bytes:
Table Column-Count and Row-Size Limits
3944

1
2
3
4
mysql> CREATE TABLE t1
-> (c1 VARCHAR( 32765 ) NOT NULL, c2 VARCHAR( 32766 ) NOT NULL)
-> ENGINE = MyISAM CHARACTER SET latin1;
Query OK,  0  rows affected ( 0.02  sec)

The following statement to create table t2 fails because the columns are NULL and MyISAM requires
additional space that causes the row size to exceed 65,535 bytes:

1
2
3
4
5
6
mysql> CREATE TABLE t2
-> (c1 VARCHAR( 32765 ) NULL, c2 VARCHAR( 32766 ) NULL)
-> ENGINE = MyISAM CHARACTER SET latin1;
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

The following statement to create table t3 fails because although the column length is within the
maximum length of 65,535 bytes, two additional bytes are required to record the length, which
causes the row size to exceed 65,535 bytes:

1
2
3
4
5
6
mysql> CREATE TABLE t3
-> (c1 VARCHAR( 65535 ) NOT NULL)
-> ENGINE = MyISAM CHARACTER SET latin1;
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

Reducing the column length to 65,533 or less permits the statement to succeed.
Each table has an .frm file that contains the table definition. The server uses the following
expression to check some of the table information stored in the file against an upper limit of 64KB:
if (info_length+(ulong) create_fields.elements*FCOMP+288+
n_length+int_length+com_length > 65535L || int_count > 255)
The portion of the information stored in the .frm file that is checked against the expression cannot
grow beyond the 64KB limit, so if the table definition reaches this size, no more columns can be
added.
The relevant factors in the expression are:
info_length is space needed for “screens.” This is related to MySQL's Unireg heritage.
create_fields.elements is the number of columns.
FCOMP is 17.
n_length is the total length of all column names, including one byte per name as a separator.
int_length is related to the list of values for ENUM and SET columns.
com_length is the total length of column and table comments.
Thus, using long column names can reduce the maximum number of columns, as can the inclusion of ENUM or SET columns, or use of column, index, or table comments.
Individual storage engines might impose additional restrictions that limit table column count.
Examples:
InnoDB permits up to 1000 columns.
InnoDB restricts row size to something less than half a database page (approximately 8000
bytes), not including VARBINARY, VARCHAR, BLOB, or TEXT columns.
Windows Platform Limitations
3945
Different InnoDB storage formats (COMPRESSED, REDUNDANT) use different amounts of page
header and trailer data, which affects the amount of storage available for rows.



本文转自 aklaus 51CTO博客,原文链接:http://blog.51cto.com/aklaus/1715295

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
相关文章
|
存储 缓存 关系型数据库
MySQL的varchar水真的太深了——InnoDB记录存储结构
varchar(M) 能存多少个字符,为什么提示最大16383?innodb怎么知道varchar真正有多长?记录为NULL,innodb如何处理?某个列数据占用的字节数非常多怎么办?影响每行实际可用空间的因素有哪些?本篇围绕innodb默认行格式dynamic来说说原理。
1196 6
MySQL的varchar水真的太深了——InnoDB记录存储结构
|
存储 关系型数据库 MySQL
MySQL中varchar的最大长度是多少
MySQL中varchar的最大长度是多少
1132 0
|
10月前
|
存储 关系型数据库 MySQL
MySQL 字段类型探究:深入理解 Varchar(50) 与 Varchar(500)
在MySQL数据库中,`VARCHAR`类型是一种常用的字符串存储类型,它允许定义一个可变长度的字符串。然而,`VARCHAR(50)`和`VARCHAR(500)`之间的差异不仅仅是长度的不同,它们在存储和性能方面也有显著的区别。本文将深入探讨这两种字段类型的区别,以及它们在实际应用中的选择。
403 3
|
10月前
|
存储 关系型数据库 MySQL
MySQL 字段类型深度解析:VARCHAR(50) 与 VARCHAR(500) 的差异
在MySQL数据库中,`VARCHAR`类型是一种非常灵活的字符串存储类型,它允许存储可变长度的字符串。然而,`VARCHAR(50)`和`VARCHAR(500)`之间的差异不仅仅是长度的不同,它们在存储效率、性能和使用场景上也有所不同。本文将深入探讨这两种字段类型的区别及其对数据库设计的影响。
348 2
|
存储 关系型数据库 MySQL
MySQL字段的字符类型该如何选择?千万数据下varchar和char性能竟然相差30%🚀
本篇文章来讨论MySQL字段的字符类型选择并深入实践char与varchar类型的区别以及在千万数据下的性能测试
MySQL字段的字符类型该如何选择?千万数据下varchar和char性能竟然相差30%🚀
|
关系型数据库 MySQL
MySQL中varchar能存多少汉字、数字
MySQL中varchar能存多少汉字、数字
1099 0
|
存储 关系型数据库 MySQL
|
存储 机器学习/深度学习 关系型数据库
mysql中char和varchar的区别
mysql中char和varchar的区别
366 1
|
存储 关系型数据库 MySQL
深入理解MySQL中varchar和text的区别
在MySQL中,varchar和text都是用于存储文本数据的数据类型。varchar是可变长度字符串,存储时按实际长度分配空间,适合存储较短的、长度可变的字符串,如用户名。text类型用于存储大量文本,始终占用足够空间,适合文章内容。varchar在存储和查询时可能更快,可被索引,而text需特殊搜索技术。在数据库设计时,应根据存储需求和性能平衡选择。
1768 0
|
关系型数据库 MySQL 数据库
mysql数据库 字段类型 varchar 值为数字,取最大值时出现的问题
mysql数据库 字段类型 varchar 值为数字,取最大值时出现的问题
210 0

推荐镜像

更多