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. (转)Eclipse和MyEclipse安装和使用git(egit)图解笔记

    Eclipse.MyEclipse使用git插件(egit)图解 (转)原文来自:http://www.xuebuyuan.com/446322.html 在开发Java.JavaEE等相关程序时,我 ...

  2. Python yield函数理解

    Python中的yield函数的作用就相当于一个挂起,是不被写入内存的,相当于一个挂起的状态,用的时候迭代,不用的时候就是一个挂起状态,挂起状态会以生成器的状态表现

  3. 【翻译】Express web应用开发 第一章

    本章节是一个对初学者友好的Express介绍.你将学习到Express的基础知识.核心概念和实现一个Express应用的组成部分.现阶段我们不需要做太多的编码,本章节会让你熟悉和习惯Express,为 ...

  4. vscode过滤pyc文件

    在工作区设置里添加如下代码: { "files.exclude": { "**/.git": true, "**/.svn": true, ...

  5. 我的emacs配置

    我的emacs配置文件 ;; .emacs ;; ============================== Basic Configure START ====================== ...

  6. C# File.Delete文件时 提示:文件引起的访问被拒绝解决方案

    new FileInfo(f).Attributes = FileAttributes.Normal; File.Delete(f);

  7. 跟服务器交互的登录Demo

    服务器写死 账号密码,演示登录 服务器代码: 开发工具MyEclipse public class LoginServlet extends HttpServlet { /** * The doGet ...

  8. 反向输出及sort排序

    建立条件:#include "algorithm"引用这个头文件 1.reverse 的用法,反向排序,由自己输入5个数: 1 2 3 4 5 for (int i = 0; i ...

  9. 详解Java GC的工作原理+Minor GC、FullGC

    详解Java GC的工作原理+Minor GC.FullGC 引用地址:http://www.blogjava.net/ldwblog/archive/2013/07/24/401919.html J ...

  10. Hadoop 2.6.0 Namenode HA,ResourceManager HA

    先启动所有的zookeeper zkServer.sh start 在所有节点上启动JournalNode: sbin/hadoop-daemon.sh start journalnode 格式化第一 ...