在 MySQL 中使用 INT(1) 和 TINYINT(1) 有区别吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8045327/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:35:08  来源:igfitidea点击:

Is there a difference in using INT(1) vs TINYINT(1) in MySQL?

mysqltypesinteger

提问by animuson

I'm under the assumption that INT(1) is the exact same thing as TINYINT(1) but I really have no idea. Whenever I've had values that can only be a single integer (e.g. a value 0-9), I've always just used INT(1) to say it's an integer and it will only be one character, which I assume means that it could only be a value 0 through 9 (please explain this to me if I'm wrong). I've always just ignored the other types of INT that you can cast the number as. I'm no MySQL savvy and tend to avoid the more complicated things you can do with it.

我假设 INT(1) 与 TINYINT(1) 完全相同,但我真的不知道。每当我有只能是单个整数的值(例如值 0-9)时,我总是使用 INT(1) 来表示它是一个整数并且它只会是一个字符,我认为这意味着它只能是 0 到 9 的值(如果我错了,请向我解释这一点)。我总是忽略您可以将数字转换为其他类型的 INT。我不精通 MySQL,并且倾向于避免使用它可以做的更复杂的事情。

So my question, is there any difference between the various integer types INT, TINYINT, SMALLINT, MEDIUMINT, and BIGINT if you define a length of 1 for each type;?If not, should I use them anyways (I can see using them for more semantic meaning, TINYINT being more specific than just INT)? If so, could I easily (and/or should I) just go through my database and change all my INT(1) fields to TINYINT(1) fields?

所以我的问题是,如果为每种类型定义长度为 1,那么各种整数类型 INT、TINYINT、SMALLINT、MEDIUMINT 和 BIGINT 之间有什么区别吗?如果没有,我是否应该使用它们(我可以看到使用它们来获得更多语义,TINYINT 比 INT 更具体)?如果是这样,我是否可以轻松地(和/或应该)通过我的数据库并将我的所有 INT(1) 字段更改为 TINYINT(1) 字段?

采纳答案by Phil

The number in parentheses for integer column types is the "display width". This does not effect the storage requirements as they are pre-defined.

整数列类型括号中的数字是“显示宽度”。这不会影响存储要求,因为它们是预定义的。

Further reading

进一步阅读

回答by Cody

Here you'll understand it in a better way!

在这里你会更好地理解它!

tinyint: 1 byte, -128 to +127 / 0 to 255 (unsigned)
smallint: 2 bytes, -32,768 to +32,767 / 0 to 65,535 (unsigned)
mediumint: 3 bytes, -8,388,608 to 8,388,607 / 0 to 16,777,215 (unsigned)
int/integer: 4 bytes, -2,147,483,648 to +2,147,483,647 / 0 to 4,294,967,295 (unsigned)
bigint: 8 bytes, -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 / 0 to 18,446,744,073,709,551,615 (unsigned)

回答by HoldOffHunger

To summarize the accepted answered :

总结接受的答案:

The number in parentheses indicates the *number of characters to display that field*, **not** the storage size of the field.
括号中的数字表示*显示该字段的字符数*,**不是**该字段的存储大小。

But if you want to know the storage size, you should check the MySQL source documents.

但是如果你想知道存储大小,你应该查看MySQL源文档。

Source: MySQL Docs: Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT

来源:MySQL 文档:整数类型(精确值) - INTEGER、INT、SMALLINT、TINYINT、MEDIUMINT、BIGINT

Direct quote from source documentation :

直接引用源文档:

TINYINT: 1 byte, -128 to 127 signed, 0 to 255 unsigned

SMALLINT: 2 bytes, -32768 to 32767 signed, 0 to 65535 unsigned

MEDIUMINT: 3 bytes, -8388608 to 8388607 signed, 0 to 16777215 unsigned

INT: 4 bytes, -2147483648 to 2147483647 signed, 0 to 4294967295 unsigned

BIGINT: 8 bytes, -2^63 to 2^63-1 signed, 0 to 2^64-1 unsigned

TINYINT:1 字节,-128 到 127 有符号,0 到 255 无符号

SMALLINT:2 个字节,-32768 到 32767 有符号,0 到 65535 无符号

MEDIUMINT:3 个字节,-8388608 到 8388607 有符号,0 到 16777215 无符号

INT:4 个字节,-2147483648 到 2147483647 有符号,0 到 4294967295 无符号

BIGINT: 8 字节,-2^63 到 2^63-1 有符号,0 到 2^64-1 无符号