探究MySQL中varchar的定义长度
程序员文章站
2024-03-31 14:20:58
utf8字符集下:
sql>create table test(id int auto_increment,name varchar(10),primary...
utf8字符集下:
sql>create table test(id int auto_increment,name varchar(10),primary key(id)); sql>insert into test values(null,'1234567890'); query ok, 1 row affected (0.00 sec) sql>insert into test values(null,'一二三四五六七八九十'); query ok, 1 row affected (0.00 sec) sql>insert into test values(null,'abcdefghig'); query ok, 1 row affected (0.01 sec) sql>insert into test values(null,12345678901); error 1406 (22001): data too long for column 'name' at row 1 sql>insert into test values(null,'一二三四五六七八九十1'); error 1406 (22001): data too long for column 'name' at row 1 sql>insert into test values(null,'一二三四五六七八九十一'); error 1406 (22001): data too long for column 'name' at row 1 sql>select id,name,length(name),char_length(name) from test; +----+--------------------------------+--------------+-------------------+ | id | name | length(name) | char_length(name) | +----+--------------------------------+--------------+-------------------+ | 1 | 1234567890 | 10 | 10 | | 2 | 一二三四五六七八九十 | 30 | 10 | | 3 | abcdefghig | 10 | 10 | +----+--------------------------------+--------------+-------------------+ 3 rows in set (0.00 sec)
gbk字符集下:
sql>create table test(id int auto_increment,name varchar(10),primary key(id)); sql>insert into test values(null,'1234567890'); query ok, 1 row affected (0.00 sec) sql>insert into test values(null,'一二三四五六七八九十'); query ok, 1 row affected (0.00 sec) sql>insert into test values(null,'abcdefghig'); query ok, 1 row affected (0.01 sec) sql>insert into test values(null,12345678901); error 1406 (22001): data too long for column 'name' at row 1 sql>insert into test values(null,'一二三四五六七八九十1'); error 1406 (22001): data too long for column 'name' at row 1 sql>insert into test values(null,'一二三四五六七八九十一'); error 1406 (22001): data too long for column 'name' at row 1 sql>select id,name,length(name),char_length(name) from test; +----+----------------------+--------------+-------------------+ | id | name | length(name) | char_length(name) | +----+----------------------+--------------+-------------------+ | 1 | 1234567890 | 10 | 10 | | 2 | 一二三四五六七八九十 | 20 | 10 | | 3 | abcdefghig | 10 | 10 | +----+----------------------+--------------+-------------------+ 3 rows in set (0.00 sec)
由此可见,varchar定义的长度的单位是字符,哪怕是1个多字节字符也是1个字符,如中文和英文字母都被当作1个字符来对待。
那么varchar能够定义的最大长度是多少呢?这个和你当前所使用的字符集有关。抛开字符,其最大长度为65535字节(这是最大行大小,由所有列共享),而放在不同的字符集下,能够定义的最大长度就会有所不同,如utf8下是21845。据说mysql5中varchar的长度也为字符,而mysql4中的则为字节,未经证实,感兴趣的有环境可以自己测下。
顺便补充一下,char数据类型定义的长度也为字符,其最大长度为255。
sql>create table test(id int auto_increment,name char(5),primary key(id)); query ok, 0 rows affected (0.09 sec) sql>insert into test values(null,'123'); query ok, 1 row affected (0.00 sec) sql>insert into test values(null,'12345'); query ok, 1 row affected (0.00 sec) sql>insert into test values(null,'一二三'); query ok, 1 row affected (0.00 sec) sql>insert into test values(null,'一二三四五'); query ok, 1 row affected (0.00 sec) sql>insert into test values(null,123456); error 1406 (22001): data too long for column 'name' at row 1 sql>insert into test values(null,'一二三四五1'); error 1406 (22001): data too long for column 'name' at row 1 sql>select id,name,length(name),char_length(name) from test; +----+-----------------+--------------+-------------------+ | id | name | length(name) | char_length(name) | +----+-----------------+--------------+-------------------+ | 1 | 123 | 3 | 3 | | 2 | 12345 | 5 | 5 | | 3 | 一二三 | 9 | 3 | | 4 | 一二三四五 | 15 | 5 | +----+-----------------+--------------+-------------------+ 4 rows in set (0.00 sec)
推荐阅读
-
MySQL中无GROUP BY情况下直接使用HAVING语句的问题探究
-
探究MySQL中varchar的定义长度
-
MySQL中给自定义的字段查询结果添加排名的方法
-
ORACLE中VARCHAR2类型的字段长度是按照byte来定义的-一个容易被忽略的问题 博客分类: 数据库 Oracle字符字节varchar2char
-
Mysql中实现提取字符串中的数字的自定义函数分享
-
mysql如何处理varchar与nvarchar类型中的特殊字符
-
深入研究mysql中的varchar和limit(容易被忽略的知识)
-
Mysql中实现提取字符串中的数字的自定义函数分享
-
探究MySQL中索引和提交频率对InnoDB表写入速度的影响
-
mysql如何处理varchar与nvarchar类型中的特殊字符