mysql中char(n)中的n_varchar(N)和char(N)中的N解析

"root@localhost:mysql6666.sock  [china]>show create table a1;

+-------+-----------------------------------------------------------------------------------------------+

| Table | Create Table                                                                                  |

+-------+-----------------------------------------------------------------------------------------------+

| a1    | CREATE TABLE `a1` (

`name` varchar(21844) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

+-------+-------------------------------------

varchar(N) 类型支持的最大字节长度是65535,65535-2-1=65532(最大支持65532字节),2是标识位,1标识null。

(N)代表的是字符个数,不是字节大小。

utf-8 占3个字节,能够存储65532/3=21844.个字符。如果设置21845则会报错提示行数据大小超限制。

"root@localhost:mysql6666.sock  [china]>create table a3 (name varchar(21845) not null);

ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs

=================================================================================================================

1个汉字占3个字符,1个英文字母或者数字占1个字符。

select length(X)统计字符占的大小。

*************************** 3. row ***************************

length(name): 1

name: d

*************************** 4. row ***************************

length(name): 3

name: 我

--------------------------------------------------------------------------

select char_length(X) 统计的是字符的个数。

*************************** 3. row ***************************

char_length(name): 1

name: d

*************************** 4. row ***************************

char_length(name): 1

name: 我

4 rows in set (0.00 sec)

"root@localhost:mysql6666.sock  [china]>select length(name) from a1;

+--------------+

| length(name) |

+--------------+

|        21844 |

|        21844 |

|            1 |

|            3 |

+--------------+

=====================================================================================================

char(N) N代表字符个数,最大字符个数255,代表能存255个字符。不是字符大小限制在255个字符。

#中文超出个数被截断

"root@localhost:mysql6666.sock  [china]>insert into a11 select repeat('中',255);

Query OK, 1 row affected (0.00 sec)

Records: 1  Duplicates: 0  Warnings: 0

"root@localhost:mysql6666.sock  [china]>insert into a11 select repeat('中',256);

Query OK, 1 row affected, 1 warning (0.00 sec)

Records: 1  Duplicates: 0  Warnings: 1

"root@localhost:mysql6666.sock  [china]>show warnings;

+---------+------+-------------------------------------------+

| Level   | Code | Message                                   |

+---------+------+-------------------------------------------+

| Warning | 1265 | Data truncated for column 'name' at row 1 |

+---------+------+-------

#英文超出个数被截断

"root@localhost:mysql6666.sock  [china]>insert into a11 select repeat('e',255);

Query OK, 1 row affected (0.00 sec)

Records: 1  Duplicates: 0  Warnings: 0

"root@localhost:mysql6666.sock  [china]>insert into a11 select repeat('e',256);

Query OK, 1 row affected, 1 warning (0.00 sec)

Records: 1  Duplicates: 0  Warnings: 1

"root@localhost:mysql6666.sock  [china]>show warnings;

+---------+------+-------------------------------------------+

| Level   | Code | Message                                   |

+---------+------+-------------------------------------------+

| Warning | 1265 | Data truncated for column 'name' at row 1 |

+---------+------+-------------------------------------------+

1 row in set (0.00 sec)

汉字占3个字节的大小,英文占一个字节大小

"root@localhost:mysql6666.sock  [china]>select length(name) from a11;

+--------------+

| length(name) |

+--------------+

|          255 |

|          255 |

|          255 |

|            3 |

|            1 |

|          255 |

|          258 |

|          765 |我

|          765 |

|          255 |e

|          255 |

+--------------+

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值