varchar(N)和char(N)中的N解析

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了varchar(N)和char(N)中的N解析相关的知识,希望对你有一定的参考价值。

"[email protected]: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则会报错提示行数据大小超限制。


"[email protected]: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)


"[email protected]:mysql6666.sock  [china]>select length(name) from a1;

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

| length(name) |

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

|        21844 |

|        21844 |

|            1 |

|            3 |

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


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

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

#中文超出个数被截断

"[email protected]:mysql6666.sock  [china]>insert into a11 select repeat(‘中‘,255);

Query OK, 1 row affected (0.00 sec)

Records: 1  Duplicates: 0  Warnings: 0


"[email protected]: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


"[email protected]:mysql6666.sock  [china]>show warnings;

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

| Level   | Code | Message                                   |

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

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

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



#英文超出个数被截断

"[email protected]:mysql6666.sock  [china]>insert into a11 select repeat(‘e‘,255);

Query OK, 1 row affected (0.00 sec)

Records: 1  Duplicates: 0  Warnings: 0


"[email protected]: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


"[email protected]: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个字节的大小,英文占一个字节大小


"[email protected]:mysql6666.sock  [china]>select length(name) from a11;

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

| length(name) |

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

|          255 |

|          255 |

|          255 |

|            3 |

|            1 |

|          255 |

|          258 |

|          765 |我

|          765 |

|          255 |e

|          255 |

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


本文出自 “南山深处” 博客,请务必保留此出处http://kenneyzhou.blog.51cto.com/12427643/1889689

以上是关于varchar(N)和char(N)中的N解析的主要内容,如果未能解决你的问题,请参考以下文章

sqlserver中的nvarchar和varchar的区别

char , varchar和Nvarchar区别

SQLSERVER中nvarchar和varchar类型的区别是啥?

SQL中char,varchar和nvarchar 之间的区别

char nchar varchar nvarchar varchar2区别

SQL中char nchar varchar nvarchar text ntext的区别