php验证mysql内数据_MySQL中数据类型的验证_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.

还提供了一张对应表

1463b21U21X0-19154.jpg

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

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)

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

查看错误日志:

1463b21U21X0-254D.jpg

对于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的内容,更多相关内容请关注PHP中文网(www.php.cn)!

f68f2add0b68e4f9810432fce46917b7.png

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值