时间:2021-05-23
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 1SQL>insert into test values(null,'一二三四五六七八九十1');ERROR 1406 (22001): Data too long for column 'name' at row 1SQL>insert into test values(null,'一二三四五六七八九十一');ERROR 1406 (22001): Data too long for column 'name' at row 1SQL>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 1SQL>insert into test values(null,'一二三四五六七八九十1');ERROR 1406 (22001): Data too long for column 'name' at row 1SQL>insert into test values(null,'一二三四五六七八九十一');ERROR 1406 (22001): Data too long for column 'name' at row 1SQL>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 1SQL>insert into test values(null,'一二三四五1');ERROR 1406 (22001): Data too long for column 'name' at row 1SQL>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)声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
MySQL数据库中varchar最大长度是多少?其实这不是一个固定的数字,varchar的长度是有限制规则的。本文我们就来介绍一下MySQL数据库中varcha
在MySQL中,char、varchar和text类型的字段都可以用来存储字符类型的数据,char、varchar都可以指定最大的字符长度,但text不可以。它
一、演变:MySQL数据库的varchar类型在5.0.3以下的版本中的最大长度限制为255,其数据范围可以是0~255。在MySQL5.0.3及以上的版本中,
被问到一个问题:MySQL中varchar最大长度是多少?这不是一个固定的数字。下面简要说明一下限制规则。 1、限制规则 字段的限制在字段定义的时候有以
PostgreSql数据库中varchar类型与sqlserver中字段用法有差别,PostgreSql中如果字段设置为varchar类型长度为10,则无论存字