VARCHAR 详解[通俗易懂]

VARCHAR 详解[通俗易懂]varchar(20):20指的是表中的a字段能存储的最大字符个数Incontrastto CHAR, VARCHAR valuesarestoredasa

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

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

In contrast to CHARVARCHAR 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.


mysql> create table t1( a varchar(20)); Query OK, 0 rows affected (0.27 sec)

 

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)
mysql> insert into t1 select repeat("a",21);
ERROR 1406 (22001): Data too long for column 'a' at row 1
mysql> insert into t1 select repeat("a",20);
Query OK, 1 row affected (0.09 sec)
Records: 1 Duplicates: 0 Warnings: 0

 

mysql> insert into t1 select repeat("",21);
ERROR 1406 (22001): Data too long for column 'a' at row 1

 

mysql> insert into t1 select repeat("",20);
Query OK, 1 row affected (0.11 sec)
Records: 1 Duplicates: 0 Warnings: 0

 

mysql> insert into t1 select "我我我我我我我我我我我我我我我我我我我1";
Query OK, 1 row affected (0.09 sec)
Records: 1 Duplicates: 0 Warnings: 0

 

mysql> insert into t1 select "我我我我我我我我我我我我我我我我我我我11";
ERROR 1406 (22001): Data too long for column 'a' at row 1

 

varchar 存储极限:

表为utf8字符集:

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)

 

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字符集:

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)

 

不能插入汉字:

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.net/167050.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • BatchNorm1d

    BatchNorm1d参考:https://zhuanlan.zhihu.com/p/100672008https://www.jianshu.com/p/2b94da24af3b#python3.8#-*-coding:utf-8-*-#—#@Software:PyCharm#@File:test2.py#@Author:—#@Institution:BeiJing,China#@E-mail:lgdyangninghua@163.com#@Site:

    2022年5月12日
    49
  • img 转化成iso镜像的办法「建议收藏」

    img 转化成iso镜像的办法「建议收藏」最近在使用KVM启用虚拟机,然后将里面的环境和配置配置成我们公司需要的环境,再打包成iso镜像,之后再次生成新的虚拟机。但是KVM启动出的镜像生成的是img镜像,需要将img镜像转换成iso镜像

    2022年8月3日
    6
  • 【转载】男人选妻有什么实用主义标准?「建议收藏」

    【转载】男人选妻有什么实用主义标准?「建议收藏」 在现实生活中,有相当一部分事业和生活都非常优秀的剩女们,总是没有不懂男人们在选择妻子时的心里到底在想些什么?更让她们不能接受的是,为什么男人讨老婆总是喜欢找一些看起来貌不出众,或学历与家景比自己差很多的女孩为妻。反而是像自己无论是学历文凭、家庭背景、从事职业甚至收入水平都明显高人一截的女人,反倒成了没男人追求最终被剩下的那个女人了。其实,男人找女朋友跟最终选择结婚对象时的想法是不一样的,…

    2022年5月4日
    36
  • mysql innodb 索引使用指南「建议收藏」

    mysql innodb 索引使用指南「建议收藏」mysql innodb 索引使用指南

    2022年4月21日
    47
  • redission jedis_redis could not get a resource

    redission jedis_redis could not get a resourceJedis操作Redis

    2025年10月15日
    2
  • 常用的DNS服务器_国内目前最快的DNS

    常用的DNS服务器_国内目前最快的DNS北京联通      202.106.0.20   202.106.196.115阿里              223.5.5.5 223.6.6.6114              114.114.114.114南方电信      180.153.225.136百度              180.76.76.76360                 101.226….

    2025年8月30日
    10

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号