前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >VARCHAR 详解[通俗易懂]

VARCHAR 详解[通俗易懂]

作者头像
全栈程序员站长
发布2022-09-19 22:26:45
发布2022-09-19 22:26:45
71600
代码可运行
举报
运行总次数:0
代码可运行

大家好,又见面了,我是你们的朋友全栈君。

varchar(20):20指的是表中的a字段能存储的最大字符个数

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.

代码语言:javascript
代码运行次数:0
运行
复制
mysql> create table t1( a varchar(20));
Query OK, 0 rows affected (0.27 sec)
代码语言:javascript
代码运行次数:0
运行
复制
mysql> show create table t1;
+-------+---------------------------------------------------------------
| Table | Create Table
+-------+---------------------------------------------------------------
| t1    | CREATE TABLE `t1` (
  `a` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------------------
1 row in set (0.00 sec)
代码语言:javascript
代码运行次数:0
运行
复制
mysql> insert into t1 select repeat("a",21);
ERROR 1406 (22001): Data too long for column 'a' at row 1
代码语言:javascript
代码运行次数:0
运行
复制
mysql> insert into t1 select repeat("a",20);
Query OK, 1 row affected (0.09 sec)
Records: 1 Duplicates: 0 Warnings: 0
代码语言:javascript
代码运行次数:0
运行
复制
mysql> insert into t1 select repeat("我",21);
ERROR 1406 (22001): Data too long for column 'a' at row 1
代码语言:javascript
代码运行次数:0
运行
复制
mysql> insert into t1 select repeat("我",20);
Query OK, 1 row affected (0.11 sec)
Records: 1 Duplicates: 0 Warnings: 0
代码语言:javascript
代码运行次数:0
运行
复制
mysql> insert into t1 select "我我我我我我我我我我我我我我我我我我我1";
Query OK, 1 row affected (0.09 sec)
Records: 1 Duplicates: 0 Warnings: 0
代码语言:javascript
代码运行次数:0
运行
复制
mysql> insert into t1 select "我我我我我我我我我我我我我我我我我我我11";
ERROR 1406 (22001): Data too long for column 'a' at row 1

varchar 存储极限:

表为utf8字符集:

代码语言:javascript
代码运行次数:0
运行
复制
mysql> show create table a;
+-------+-------------------------------------------------------------------------------------------+
| Table | Create Table                                                                              |
+-------+-------------------------------------------------------------------------------------------+
| a     | CREATE TABLE `a` (
  `a` varchar(21844) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
代码语言:javascript
代码运行次数:0
运行
复制
mysql> create table a( a varchar(65535));
ERROR 1074 (42000): Column length too big for column 'a' (max = 21845); use BLOB or TEXT instead

mysql> create table a( a varchar(65532));
ERROR 1074 (42000): Column length too big for column 'a' (max = 21845); use BLOB or TEXT instead

mysql> create table a( a varchar(21845));
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes st
eck the manual. You have to change some columns to TEXT or BLOBs

mysql> create table a( a varchar(21844));
Query OK, 0 rows affected (0.31 sec)

表为latin1字符集:

代码语言:javascript
代码运行次数:0
运行
复制
mysql> create table a1 ( a varchar(65535))charset=latin1;
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs,
eck the manual. You have to change some columns to TEXT or BLOBs

mysql> create table a1 ( a varchar(65534))charset=latin1;
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs,
eck the manual. You have to change some columns to TEXT or BLOBs

mysql> create table a1 ( a varchar(65533))charset=latin1;
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs,
eck the manual. You have to change some columns to TEXT or BLOBs

mysql> create table a1 ( a varchar(65532))charset=latin1;
Query OK, 0 rows affected (0.33 sec)

不能插入汉字:

代码语言:javascript
代码运行次数:0
运行
复制
mysql> insert into a1 select repeat("我",65532);
ERROR 1366 (HY000): Incorrect string value: '\xE6\x88\x91\xE6\x88\x91...' for column 'a' at row 1
mysql> insert into a1 select "我";
ERROR 1366 (HY000): Incorrect string value: '\xE6\x88\x91' for column 'a' at row 1


mysql> insert into a1 select repeat("a",65532);
Query OK, 1 row affected (0.17 sec)
Records: 1  Duplicates: 0  Warnings: 0

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/167050.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档