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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 缓存雪崩、击穿、穿透,该如何避免?[通俗易懂]

    缓存雪崩、击穿、穿透,该如何避免?

    2022年2月12日
    36
  • robotium官网[通俗易懂]

    robotium官网[通俗易懂]官网地址:https://code.google.com/p/robotium/

    2022年7月25日
    4
  • python爬虫文件代码大全-23个Python爬虫开源项目代码

    python爬虫文件代码大全-23个Python爬虫开源项目代码今天为大家整理了23个Python爬虫项目。整理的原因是,爬虫入门简单快速,也非常适合新入门的小伙伴培养信心。所有链接指向GitHub,祝大家玩的愉快1、WechatSogou[1]–微信公众号爬虫。基于搜狗微信搜索的微信公众号爬虫接口,可以扩展成基于搜狗搜索的爬虫,返回结果是列表,每一项均是公众号具体信息字典。2、DouBanSpider[2]–豆瓣读书爬虫。可以爬下豆瓣读书标签下的所有…

    2022年5月13日
    135
  • 好用的pycharm插件_手机flash player插件

    好用的pycharm插件_手机flash player插件PyCharm无敌插件推荐(一)插件安装途径Step1在文件(File)菜单中选择设置(Settings)Step2在编辑器(Editor)菜单中选择插件(Plugins)二级1.Chinese(Simplified)LanguagePack这是一款JetBrains官方自己推出的汉化插件,对于不是很擅长的英语的初学者十分友好。能够帮助初学者尽快熟悉PyCharm。…

    2022年8月26日
    4
  • 高中688个高频单词_高频英语单词800

    高中688个高频单词_高频英语单词800给一非空的单词列表,返回前 k 个出现次数最多的单词。返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率,按字母顺序排序。示例 1:输入: [“i”, “love”, “leetcode”, “i”, “love”, “coding”], k = 2输出: [“i”, “love”]解析: “i” 和 “love” 为出现次数最多的两个单词,均为2次。 注意,按字母顺序 “i” 在 “love” 之前。 示例 2:输入: [“the”, “day”, “is

    2022年8月9日
    3
  • strcmp函数的使用_用c语言实现strcmp

    strcmp函数的使用_用c语言实现strcmpAction(){/**********************************Author:旺仔*object:strcmp*date:2015-12-09*fuc:我输

    2022年8月3日
    3

发表回复

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

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