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

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • pycharm 替换快捷键_pycharm到包快捷键

    pycharm 替换快捷键_pycharm到包快捷键查找/替换(Search/Replace)F3下一个Shift+F3前一个Ctrl+R替换(需要同时替换的变量名等)Ctrl+Shift+F整个项目中全局查找Ctrl+Shift+R整个项目中全局替换

  • pycharm怎么缩小代码_word自动编号缩进调整

    pycharm怎么缩小代码_word自动编号缩进调整PyCharm-自动缩进代码(Auto-IndentLines)1.Ctrl+A全选代码。2.Code->Auto-IndentLines自动缩进快捷键Ctrl+Alt+I。Referenceshttps://yongqiang.blog.csdn.net/

  • SpringMVC工作原理(含案例图解)

    SpringMVC工作原理(含案例图解)SpimgMVC工作原理第1步:浏览器发送指定的请求都会交给DispatcherServlet,他会委托其他模块进行真正的业务和数据处理第2步:DispatcherServlet会查找到HandleMapping,根据浏览器的请求找到对应的Controller,并将请求交给目标Controller第3步:目标Controller处理完业务后,返回一个ModelAndView给Dispa…

  • 点赞投票群二维码_朋友圈集赞活动链接怎么做

    点赞投票群二维码_朋友圈集赞活动链接怎么做一、了解点赞起源历史点赞自2010年诞生以来,大拇指点“赞”按钮迅速被人们接受,而后成为facebook的另一种标志.后面到百度,qq空间,新浪微博,腾讯微博都添加了点赞按钮,商家们想借此提升人气,策划了各种“点赞有礼”的活动.为了获得更多的奖品的人们结队成群,所以诞生了“点赞族”.道行高者更称为“点赞狂魔”.微信因为有针对商家的公众平台,以及qq用户的支持.所以成为点赞活动参与者的首选.二、微信…

  • C#获取进程的主窗口句柄「建议收藏」

    C#获取进程的主窗口句柄「建议收藏」publicclassUser32API{  privatestaticHashtableprocessWnd=null;  publicdelegateboolWNDENUMPROC(IntPtrhwnd,uintlParam);  staticUser32API()  {    if(processWnd==nu

  • Using LockWorkstation in Windows XP

    Using LockWorkstation in Windows XPUsingLockWorkstationinWindowsXPBYDelphiscn(cnBlaster#hotmail.com)http://blog.csdn.net/delphiscn问题提交:花落孤独相关连接:http://blog.csdn.net/delphiscn/archive/2005/06/25/403157.aspx提交日期:2005-12-063:53…

发表回复

您的电子邮箱地址不会被公开。

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