惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
雷峰网
雷峰网
Last Week in AI
Last Week in AI
T
Tailwind CSS Blog
V
Visual Studio Blog
Jina AI
Jina AI
博客园 - 司徒正美
The Cloudflare Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
小众软件
小众软件
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
美团技术团队
博客园 - 【当耐特】
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
WordPress大学
WordPress大学
爱范儿
爱范儿
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏

WangDeer

非机动车交通规则测验 微软 50 周年壁纸是在 macOS上设计的? 「转」西二旗折叠:蜂巢经济学启示录 难得空明 Ubuntu Clean ApidocJs apigroup Support Chinese 5-3 会话 2021-03-25 「PHP架构师」面试准备 Laravel 上传 Docx 文件,表单验证不通过 4-4 发送信号 4-3 信号集 4-2 编写中断信号处理程序 2022-03-10 面试复盘 4-1 什么是中断信号(软中断) 3-7 进程查看 3-6 SUID特权进程 3-5多进程编写 3-4进程调度 3-3进程exec 3-2进程退出和回收 3-1进程标识与fork 2-3解释器文件 2-2命令行参数与环境参数表 2-1程序与进程 1-1课程介绍 终极复盘 第八章 传承篇 第七章 自由篇 第六章 品牌篇 第五章 作品篇
int(10)里面的10是什么意思?
王二 · 2022-06-24 · via WangDeer

crud 惯了,从来没深入研究过这些(一直以为是能存储的字节数)。

查了一下官方文档:Numeric Type Attributes

MySQL supports an extension for optionally specifying the display width of integer data types in parentheses following the base keyword for the type. For example, INT(4) specifies an INT with a display width of four digits. This optional display width may be used by applications to display integer values having a width less than the width specified for the column by left-padding them with spaces. (That is, this width is present in the metadata returned with result sets. Whether it is used is up to the application.)

MySQL 有一个支持在字段类型关键字后边的 () 里指定 integer 数据类型的显示宽度的扩展。例如 INT(4) 指定一个显示宽度为4位数的整数。这个选项一般用在当字段值的宽度小于字段指定的宽度时,用空格对值进行左填充。(也就是说,此宽度存在于随结果集返回的元数据中。是否使用取决于应用程序。)

The display width does not constrain the range of values that can be stored in the column. Nor does it prevent values wider than the column display width from being displayed correctly. For example, a column specified as SMALLINT(3) has the usual SMALLINT range of -32768 to 32767, and values outside the range permitted by three digits are displayed in full using more than three digits.

显示宽度既不限制该字段可以存储的值的范围,也不会阻止比显示宽度宽的值的正确显示。例如一个字段指定为 SMALLINT(3),通常 SMALLINT 的范围为 -32768 到 32767,超出三位数宽度的值将会以三位数以上的数字全部显示。

When used in conjunction with the optional (nonstandard) ZEROFILL attribute, the default padding of spaces is replaced with zeros. For example, for a column declared as INT(4) ZEROFILL, a value of 5 is retrieved as 0005.

当与可选属性 ZEROFILL 一起使用时,默认的空格填充将替换为用 0 填充。例如一个字段声明为 INT(4) ZEROFILL,那么值 5 将被检索为 0005

NOTE

The ZEROFILL attribute is ignored for columns involved in expressions or UNION queries.

If you store values larger than the display width in an integer column that has the ZEROFILL attribute, you may experience problems when MySQL generates temporary tables for some complicated joins. In these cases, MySQL assumes that the data values fit within the column display width.

注意

ZEROFILL 属性在表达式和联合查询中会被忽略。

如果你在一个设置了 ZEROFILL 属性的整数字段中,存储的值大于显示宽度,你可能会在一些复杂的联合查询中生成临时表是碰到问题。在这些情况下,MySQL 假设数据值适合字段的显示宽度。

Excuse me 🤔️? 那这有啥用?

As of MySQL 8.0.17, the ZEROFILL attribute is deprecated for numeric data types, as is the display width attribute for integer data types. You should expect support for ZEROFILL and display widths for integer data types to be removed in a future version of MySQL. Consider using an alternative means of producing the effect of these attributes. For example, applications can use the LPAD() function to zero-pad numbers up to the desired width, or they can store the formatted numbers in CHAR columns.

MySQL 8.0.17 开始,数字数据类型不推荐使用 ZEROFILL 属性,整数数据类型也不推荐使用 display width 属性。你应该预料到在 MySQL 的未来版本中会删除对整数数据类型的 ZEROFILLdisplay width 的支持,考虑用其他方法来实现这些属性的功能。例如用 LPAD() 函数将数字填充为指定宽度,或者将格式化后的数字存储在 CHAR 类型的字段中。

弃用了我就放心了。