MySQL - 整数列的大小限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14573451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL - Size Limits to Integer Columns
提问by coffeemonitor
I'm using phpMyAdmin to create my table structures.
我正在使用 phpMyAdmin 创建我的表结构。
I can read from the documentation pages on MySQL about size limits for Integer Types: MySQL Integer Types Reference
我可以从 MySQL 的文档页面中阅读关于整数类型的大小限制: MySQL 整数类型参考
So here is where I'm getting a little confused with creating a column.
所以这里是我对创建专栏有点困惑的地方。
I want to create a column in the table: tbl_note_categories
called notescounter
我想在表中创建一列:tbl_note_categories
称为notescounter
I don't foresee myself creating thousands of noteids in the tbl_notes
with any specific categoryid
. But I do believe I'd create hundreds of notes to each categoryid.
我不认为自己会在tbl_notes
任何特定的categoryid
. 但我相信我会为每个 categoryid 创建数百个注释。
I'm at that point of choosing between: tinyint, smallint, mediumint. According the documentation link above, I'm guessing smallint is my best choice.
我正在选择:tinyint、smallint、mediumint。根据上面的文档链接,我猜 smallint 是我最好的选择。
So here's my confusion. PhpMyAdmin asks for a Length/Valuesparameter to be specified.
I'm going to make sure this new column (notescounter
) is unsigned, giving me up to 65536.
Does that mean I need the Length/Values to be (5)?
所以这是我的困惑。PhpMyAdmin 要求指定长度/值参数。我要确保这个新列 ( notescounter
) 是无符号的,最多给我 65536。这是否意味着我需要长度/值是 (5)?
I'm guessing Length is character length, but I'm not sure. (comparing to varchar)
我猜 Length 是字符长度,但我不确定。(与varchar相比)
回答by Bill Karwin
No, this is a common misconception about MySQL. In fact, the "length" has no effect on the size of an integer or the range of values it can store.
不,这是对 MySQL 的常见误解。事实上,“长度”对整数的大小或它可以存储的值范围没有影响。
TINYINT
is always 8 bits and can store 28distinct values.SMALLINT
is always 16 bits and can store 216distinct values.INT
is always 32 bits and can store 232distinct values.BIGINT
is always 64 bits and can store 264distinct values.
TINYINT
总是 8 位,可以存储 2 8 个不同的值。SMALLINT
总是 16 位,可以存储 2 16 个不同的值。INT
总是 32 位,可以存储 2 32 个不同的值。BIGINT
总是 64 位,可以存储 2 64 个不同的值。
There's also a MEDIUMINT, but the engineers who work on MySQL tell me MEDIUMINT always gets promoted to a 32-bit INT internally, so there's actually no benefit to using MEDIUMINT.
还有一个 MEDIUMINT,但是在 MySQL 上工作的工程师告诉我 MEDIUMINT 总是在内部被提升为 32 位 INT,所以使用 MEDIUMINT 实际上没有任何好处。
The length is onlyfor display, and this only matters if you use the ZEROFILL
option.
长度仅用于显示,这仅在您使用该ZEROFILL
选项时才重要。
See an example in my answer to What is the difference (when being applied to my code) between INT(10) and INT(12)?
请参阅我对INT(10) 和 INT(12) 之间有什么区别(应用于我的代码时)的回答中的一个示例?
回答by spencer7593
Yes, you want to specify a length of 5.
是的,您要指定长度为 5。
In MySQL, the "length" attribute on the integer types is optional. It's a MySQL extension which is non-standard).
在 MySQL 中,整数类型的“长度”属性是可选的。这是一个非标准的 MySQL 扩展)。
When it is omitted from the column declaration, MySQL provides a default value. For a SMALLINT UNSIGNED, the default value is 5.
当它从列声明中省略时,MySQL 提供一个默认值。对于 SMALLINT UNSIGNED,默认值为 5。
This value does NOT have any impact on the range of values that can be stored for an integer type. It specifies a "display length", which is returned in resultset metadata, which a client can choose to use or ignore.
该值对整数类型可以存储的值范围没有任何影响。它指定了一个“显示长度”,它在结果集元数据中返回,客户端可以选择使用或忽略。
http://dev.mysql.com/doc/refman/5.5/en/numeric-type-attributes.html
http://dev.mysql.com/doc/refman/5.5/en/numeric-type-attributes.html