欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL中数据类型的验证

程序员文章站 2024-02-23 14:19:46
char char (m) m字符,长度是m*字符编码长度,m最大255。 验证如下: mysql> create table t1(name...

char

char (m) m字符,长度是m*字符编码长度,m最大255。

验证如下:

mysql> create table t1(name char(256)) default charset=utf8;
error 1074 (42000): column length too big for column 'name' (max = 255); use blob or text instead
mysql> create table t1(name char(255)) default charset=utf8;
query ok, 0 rows affected (0.06 sec)
mysql> insert into t1 values(repeat('整',255));
query ok, 1 row affected (0.00 sec)
mysql> select length(name),char_length(name) from t1;
+--------------+-------------------+
| length(name) | char_length(name) |
+--------------+-------------------+
| 765 | 255 |
+--------------+-------------------+
1 row in set (0.00 sec) 

varchar

varchar(m),m同样是字符,长度是m*字符编码长度。它的限制比较特别,行的总长度不能超过65535字节。

mysql> create table t1(name varchar(65535));
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
mysql> create table t1(name varchar(65534));
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
mysql> create table t1(name varchar(65533));
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
mysql> create table t1(name varchar(65532));
query ok, 0 rows affected (0.08 sec) 

注意,以上表的默认字符集是latin1,字符长度是1个字节,所以对于varchar,最大只能指定65532字节的长度。

如果是指定utf8,则最多只能指定21844的长度

mysql> create table t1(name varchar(65532)) default charset=utf8;
error 1074 (42000): column length too big for column 'name' (max = 21845); use blob or text instead
mysql> create table t1(name varchar(21845)) default charset=utf8;
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
mysql> create table t1(name varchar(21844)) default charset=utf8;
query ok, 0 rows affected (0.07 sec) 

注意:行的长度最大为65535,只是针对除blob,text以外的其它列。

mysql> create table t1(name varchar(65528),hiredate datetime) default charset=latin1;
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
mysql> create table t1(name varchar(65527),hiredate datetime) default charset=latin1;
query ok, 0 rows affected (0.01 sec) 

确实,datetime占了5个字节。

text,blob

mysql> create table t1(name text(255));
query ok, 0 rows affected (0.01 sec)
mysql> create table t2(name text(256));
query ok, 0 rows affected (0.01 sec)
mysql> show create table t1\g
*************************** 1. row ***************************
table: t1
create table: create table `t1` (
`name` tinytext
) engine=innodb default charset=latin1
1 row in set (0.00 sec)
mysql> show create table t2\g
*************************** 1. row ***************************
table: t2
create table: create table `t2` (
`name` text
) engine=innodb default charset=latin1
1 row in set (0.00 sec) 

通过上面的输出可以看出text可以定义长度,如果范围小于28(即256)则为tinytext,如果范围小于216(即65536),则为text, 如果小于224,为mediumtext,小于232,为longtext。

上述范围均是字节数。

如果定义的是utf8字符集,对于text,实际上只能插入21845个字符

mysql> create table t1(name text) default charset=utf8;
query ok, 0 rows affected (0.01 sec)
mysql> insert into t1 values(repeat('整',21846));
error 1406 (22001): data too long for column 'name' at row 1
mysql> insert into t1 values(repeat('整',21845));
query ok, 1 row affected (0.05 sec) 

decimal

关于decimal,官方的说法有点绕,

values for decimal (and numeric) columns are represented using a binary format that packs nine decimal (base 10) digits into four bytes. storage for the integer and fractional parts of each value are determined separately. each multiple of nine digits requires four bytes, and the “leftover” digits require some fraction of four bytes. the storage required for excess digits is given by the following table. 

还提供了一张对应表

MySQL中数据类型的验证

对于以上这段话的解读,有以下几点:

1. 每9位需要4个字节,剩下的位数所需的空间如上所示。

2. 整数部分和小数部分是分开计算的。

譬如 decimal(6,5),从定义可以看出,整数占1位,整数占5位,所以一共占用1+3=4个字节。

如何验证呢?可通过innodb table monitor

如何启动innodb table monitor,可参考:http://dev.mysql.com/doc/refman/5.7/en/innodb-enabling-monitors.html

mysql> create table t2(id decimal(6,5));
query ok, 0 rows affected (0.01 sec)
mysql> create table t3(id decimal(9,0));
query ok, 0 rows affected (0.01 sec)
mysql> create table t4(id decimal(8,3));
query ok, 0 rows affected (0.01 sec)
mysql> create table innodb_table_monitor (a int) engine=innodb;
query ok, 0 rows affected, 1 warning (0.01 sec) 

结果会输出到错误日志中。

查看错误日志:

MySQL中数据类型的验证

对于decimal(6,5),整数占1位,小数占5位,一共占用空间1+3=4个字节

对于decimal(9,0),整数部分9位,每9位需要4个字节,一共占用空间4个字节

对于decimal(8,3),整数占5位,小数占3位,一共占用空间3+2=5个字节。

至此,常用的mysql数据类型验证完毕~

对于char,varchar和text等字符类型,m指定的都是字符的个数。对于char,最大的字符数是255。对于varchar,最大的字符数与字符集有关,如果字符集是latin1,则最大的字符数是65532(毕竟每一个字符只占用一个字节),对于utf8,最大的字符数是21844,因为一个字符占用三个字节。本质上,varchar更多的是受到行大小的限制(最大为65535个字节)。对于text,不受行大小的限制,但受到自身定义的限制。