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 |

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章