搜索
首页数据库mysql教程Mysql tinyint(1)与tinyint(4)的区别是什么

什么是tinyint(M)?

先来了解下mysql中字符串类型varchar(M) 和数值类型tinyint(M) 的区别?
字符串列类型: varchar(M) 而言,M 是字段中可以存储的最大字符长度,也就是说是字段长度。根据设置,当你插入超出字段长度的数据时,你很可能会收到错误提示,即使没有收到错误提示,你插入的数据也会被自动截断以适应该字段的预定义长度。所以,varchar(20) 和 varchar(40) 是不同的,其真实反映了该字段可以存储的数据长度
数值列类型:其长度修饰符表示最大显示宽度,与该字段物理存储没有任何关系。也就是说,tinyint(1) 和 tinyint(4) 能够存储的数值范围都是-128…127 (or for unsigned values 0…255),他们是相同的数据类型,当然他们还是有一点差异,以下会有说明。
对于 tinyint 数据类型,只占 1 个字节

- 无符号的(unsigned),范围是 0 到 255,默认长度是 3。
- 有符号的(signed),范围是 -128 到 127,默认长度是 4。

范围算法:tinyint占1个字节,一个字节 8 位,也就是1*8=8,可以表示的数字个数是 2的 8 次方(2^8 = 256个数字)。

区别:若使用了 zerofill,当实际长度达不到指定的显示长度时,就会用 0 在前面补齐。(简记zerofill作用就是补零)

测试

先创建一张测试表,对 tinyint 类型都使用 zerofill。

CREATE TABLE `pre_demo` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
  `unsigned_t` tinyint(3) unsigned zerofill NOT NULL DEFAULT '000',
  `signed_t` tinyint(4) unsigned zerofill NOT NULL DEFAULT '0000',
  `t1` tinyint(1) unsigned zerofill NOT NULL DEFAULT '0',
  `t2` tinyint(2) unsigned zerofill NOT NULL DEFAULT '00',
  `t3` tinyint(3) unsigned zerofill NOT NULL DEFAULT '000',
  `t4` tinyint(4) unsigned zerofill NOT NULL DEFAULT '0000',
  `t5` tinyint(5) unsigned zerofill NOT NULL DEFAULT '00000',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

然后,插入测试数据。

NSERT INTO pre_demo VALUES(NULL,8,8,8,8,8,8,8);
INSERT INTO pre_demo VALUES(NULL,123,123,123,123,123,123,123);

最后,查询数据表中的数据。

mysql> SELECT * FROM pre_demo;
+----+------------+----------+-----+-----+-----+------+-------+
| id | unsigned_t | signed_t | t1  | t2  | t3  | t4   | t5    |
+----+------------+----------+-----+-----+-----+------+-------+
|  1 |        008 |     0008 |   8 |  08 | 008 | 0008 | 00008 |
|  2 |        123 |     0123 | 123 | 123 | 123 | 0123 | 00123 |
+----+------------+----------+-----+-----+-----+------+-------+
2 rows in set (0.00 sec)
mysql> SELECT *,LENGTH(id),LENGTH(unsigned_t),LENGTH(t1) FROM pre_demo;
+----+------------+----------+-----+-----+-----+------+-------+------------+--------------------+------------+
| id | unsigned_t | signed_t | t1  | t2  | t3  | t4   | t5    | LENGTH(id) | LENGTH(unsigned_t) | LENGTH(t1) |
+----+------------+----------+-----+-----+-----+------+-------+------------+--------------------+------------+
|  1 |        008 |     0008 |   8 |  08 | 008 | 0008 | 00008 |          1 |                  3 |          1 |
|  2 |        123 |     0123 | 123 | 123 | 123 | 0123 | 00123 |          1 |                  3 |          3 |
+----+------------+----------+-----+-----+-----+------+-------+------------+--------------------+------------+
2 rows in set (0.00 sec)

以上是Mysql tinyint(1)与tinyint(4)的区别是什么的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:亿速云。如有侵权,请联系admin@php.cn删除
在MySQL中使用视图的局限性是什么?在MySQL中使用视图的局限性是什么?May 14, 2025 am 12:10 AM

mysqlviewshavelimitations:1)他们不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinSorsubqueries.2)他们canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

确保您的MySQL数据库:添加用户并授予特权确保您的MySQL数据库:添加用户并授予特权May 14, 2025 am 12:09 AM

porthusermanagementInmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

哪些因素会影响我可以在MySQL中使用的触发器数量?哪些因素会影响我可以在MySQL中使用的触发器数量?May 14, 2025 am 12:08 AM

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)复杂的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)

mysql:存储斑点安全吗?mysql:存储斑点安全吗?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

mySQL:通过PHP Web界面添加用户mySQL:通过PHP Web界面添加用户May 14, 2025 am 12:04 AM

通过PHP网页界面添加MySQL用户可以使用MySQLi扩展。步骤如下:1.连接MySQL数据库,使用MySQLi扩展。2.创建用户,使用CREATEUSER语句,并使用PASSWORD()函数加密密码。3.防止SQL注入,使用mysqli_real_escape_string()函数处理用户输入。4.为新用户分配权限,使用GRANT语句。

mysql:blob和其他无-SQL存储,有什么区别?mysql:blob和其他无-SQL存储,有什么区别?May 13, 2025 am 12:14 AM

mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而alenosqloptionslikemongodb,redis和calablesolutionsoluntionsoluntionsoluntionsolundortionsolunsolunsstructureddata.blobobobsimplobissimplobisslowderperformandperformanceperformancewithlararengelitiate;

mySQL添加用户:语法,选项和安全性最佳实践mySQL添加用户:语法,选项和安全性最佳实践May 13, 2025 am 12:12 AM

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

MySQL:如何避免字符串数据类型常见错误?MySQL:如何避免字符串数据类型常见错误?May 13, 2025 am 12:09 AM

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollat​​ionsEttingsefectery.1)usecharforfixed lengengters lengengtings,varchar forbariaible lengength,varchariable length,andtext/blobforlabforlargerdata.2 seterters seterters seterters seterters

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)