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,可参考:https://dev.mysql.com/doc/refman/5.6/en/innodb-table-monitor.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数据类型的验证的更多相关文章

  1. 【转】MySQL数据类型和常用字段属性总结

    来源:http://www.jb51.net/article/55853.htm 这里先总结数据类型.MySQL中的数据类型大的方面来分,可以分为:日期和时间.数值,以及字符串.下面就分开来进行总结. ...

  2. MySQL数据类型和常用字段属性总结

    前言 好比C++中,定义int类型需要多少字节,定义double类型需要多少字节一样,MySQL对表每个列中的数据也会实行严格控制,这是数据驱动应用程序成功的关键.MySQL提供了一组可以赋给表中各个 ...

  3. 转!!MYSQL数据类型

    这篇文章主要介绍了MySQL数据类型和常用字段属性总结,本文总结了日期和时间数据类型.数值数据类型.字符串数据类型等,需要的朋友可以参考下     前言 好比C++中,定义int类型需要多少字节,定义 ...

  4. MySQL数据类型和属性

    日期和时间数据类型 MySQL数据类型 含义 date 3字节,日期,格式:2014-09-18 time 3字节,时间,格式:08:42:30 datetime 8字节,日期时间,格式:2014-0 ...

  5. 详解MySQL数据类型

    原文地址http://www.cnblogs.com/xrq730/p/5260294.html,转载请注明出处,谢谢! 前言 很久没写文章,也有博友在我的有些文章中留言,希望我可以写一些文章,公司项 ...

  6. MySQL(数据类型和完整约束)

    MySQL数据类型 MySQL支持多种数据类型,主要有数值类型.日期/时间类型和字符串类型. 1.数值数据类型 包括整数类型TINYINT.SMALLINT.MEDIUMINT.INT.BIGINT. ...

  7. mysql数据库从删库到跑路之mysql数据类型

    一 介绍 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选的 详细参考: http://www.runoob.com/mysql/mysql-data ...

  8. Mysql 数据类型及选择原则

    MySQL中的数据类型大的方面来分,可以分为:日期和时间.数值,以及字符串.下面就分开来进行总结. 数据库类型的选择对数据库的性能影响很大 1 . 数据类型会影响存储空间的开销 2 . 数据类型会影响 ...

  9. MySQL 数据类型(Day41)

    一.介绍 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的高度,但宽度是可选的. mysql数据类型概览 #1.数字:(默认都是有符号,宽度指的是显示宽度,与存储无关) ...

随机推荐

  1. PHP 之 CURL 模拟登陆并获取数据

    1.CURL模拟登陆的流程和步骤 2.tempnam 创建一个临时文件 3.使用CURL模拟登陆到PHP100论坛 <?php $cookie_file = tempnam('./temp',' ...

  2. 李洪强iOS经典面试题156 - Runtime详解(面试必备)

    李洪强iOS经典面试题156 - Runtime详解(面试必备)   一.runtime简介 RunTime简称运行时.OC就是运行时机制,也就是在运行时候的一些机制,其中最主要的是消息机制. 对于C ...

  3. 【Beta】Daily Scrum Meeting第二次

    1.任务进度 学号 已完成 接下去要做 502 系负责人及所负责专业的表 写出PHP测试的demo:将okHttp的请求放在非UI线程中执行 509 PHP更该用户信息:更新系负责人所负责系:删除任务 ...

  4. OpenGL Insights 阅读有感 - Tile Based架构下的性能调校 翻译

    Performance Tunning for Tile-Based Architecture Tile-Based架构下的性能调校 by Bruce Merry GameKnife译 译序 在大概1 ...

  5. Spring的通知(Advice)

    Spring提供了5种Advice类型: Interception Around:JointPoint前后调用 Before:JointPoint前调用 After Returning:JointPo ...

  6. windows 环境和linux环境下 ping命令的区别:

    Ping 是Windows自带的一个DOS命令.利用它可以检查网络是否能够连通,用好它可以很好地帮助我们分析判定网络故障.该命令可以加许多参数使用,键入Ping按回车即可看到详细说明.Ping 命令可 ...

  7. centos 7.2 安装PHP7.1+apache2.4.23

    安装准备:   http://ftp.cuhk.edu.hk/pub/packages/apache.org//httpd/httpd-2.4.23.tar.gz 下载apache http://cn ...

  8. 如果你想真正了解Struts2,不妨可以进来看看

    首先我们就一起来认识认识Struts2到底是什么?作为框架,它又是用来处理哪些问题的呢?正所谓脚踏实地走,即时离梦想会远一点,但却很真实,那我们就一步一步的来了解Struts2吧!      一.既然 ...

  9. Jquery 仿 android Toast效果

    JS代码如下: /** * 模仿android里面的Toast效果,主要是用于在不打断程序正常执行的情况下显示提示数据 * @param config * @return */var Toast = ...

  10. 建模元件有哪些在MapleSim中

    信号库:包含通用信号模块.布尔.控制器.离散信号模块.信号源.线性信号模块.非线性信号模块.时间离散信号模块.查询表.信号转换器.数学运算.关系元件.特殊信号模块,应用案例. 电子库:包含电阻.运算放 ...