MySQL中数据类型的验证

 更新时间:2016年04月13日 11:10:33   作者:iVictor  
这篇文章主要介绍了MySQL中数据类型的验证 的相关资料,需要的朋友可以参考下

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. 

还提供了一张对应表

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

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) 

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

查看错误日志:

对于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,不受行大小的限制,但受到自身定义的限制。

相关文章

  • Mysql使用索引实现查询优化

    Mysql使用索引实现查询优化

    索引的目的在于提高查询效率,本文给大家介绍Mysql使用索引实现查询优化技巧,涉及到索引的优点等方面的知识点,非常不错,具有参考借鉴价值,感兴趣的朋友一起看下吧
    2016-07-07
  • mysql datetime查询异常问题解决

    mysql datetime查询异常问题解决

    这篇文章主要介绍了mysql datetime查询异常问题解决的相关资料,这里对异常进行了详细的介绍和该如何解决,需要的朋友可以参考下
    2016-11-11
  • Mysql主从GTID与binlog如何使用

    Mysql主从GTID与binlog如何使用

    MySQL的GTID和binlog是实现高效数据复制和恢复的重要机制,GTID保证事务的唯一标识,避免复制冲突;binlog记录数据变更,用于主从复制和数据恢复,两者配合,提高MySQL复制的准确性和管理便捷性
    2024-10-10
  • Mysql桌面工具之SQLyog资源及激活使用方法告别黑白命令行

    Mysql桌面工具之SQLyog资源及激活使用方法告别黑白命令行

    这篇文章主要介绍了Mysql桌面工具之SQLyog资源及激活使用方法告别黑白命令行,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-02-02
  • Linux下安装MySQL5.7.19问题小结

    Linux下安装MySQL5.7.19问题小结

    第一次在自己虚机上安装mysql 中间碰到很多问题 在这里记下来,特此分享到脚本之家平台供大家参考
    2017-08-08
  • MySQL使用中遇到的问题记录

    MySQL使用中遇到的问题记录

    本文给大家汇总介绍了作者在mysql的使用过程中遇到的问题以及最终的解决方案,非常的实用,有需要的小伙伴可以参考下
    2017-11-11
  • mysql不包含模糊查询问题

    mysql不包含模糊查询问题

    这篇文章主要介绍了mysql不包含模糊查询问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • mysql用户权限管理实例分析

    mysql用户权限管理实例分析

    这篇文章主要介绍了mysql用户权限管理,结合实例形式分析了mysql用户权限管理概念、原理及用户权限的查看、修改、删除等操作技巧,需要的朋友可以参考下
    2020-04-04
  • 一文详解MySQL是如何解决幻读的

    一文详解MySQL是如何解决幻读的

    事务A按照一定条件进行数据读取,期间事务B插入了相同搜索条件的新数据,事务A再次按照原先条件进行读取操作修改时,发现了事务B新插入的数据称之为幻读,这篇文章主要给大家介绍了关于MySQL是如何解决幻读的相关资料,需要的朋友可以参考下
    2023-04-04
  • MySQL修改字符集的实战教程

    MySQL修改字符集的实战教程

    这篇文章主要介绍了MySQL修改字符集的方法,帮助大家更好的理解和使用MySQL数据库,感兴趣的朋友可以了解下
    2021-01-01

最新评论