zoukankan      html  css  js  c++  java
  • VARCHAR 详解

    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: 'xE6x88x91xE6x88x91...' for column 'a' at row 1
    mysql> insert into a1 select "";
    ERROR 1366 (HY000): Incorrect string value: 'xE6x88x91' 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





  • 相关阅读:
    众包实验:《分享上海》出书计划
    程序员笔试题(附答案)
    "客户管理系统"终于开发完毕,在这快乐元宵节里深吸一口气(图)
    SOA 初步阅读理解
    大型网站性能优化的通用方法(转)
    读《恒源祥彪悍广告是如何出炉的》后学习到的
    Asp.net生成静态页面最简单方法(源码)
    综合实习报告 写了一下午 图图图。。。。全是图
    程序员笔试题 将人民币金额转换成大写的金额
    扬太集团的网站也做完了,写篇日记算做总结吧
  • 原文地址:https://www.cnblogs.com/zengkefu/p/5618246.html
Copyright © 2011-2022 走看看