Mysql的BigInt(20),Int(20)以及Bigint(32)有区别吗?

原文: http://stackoverflow.com/questions/3135804/types-in-mysql-bigint20-vs-int20-etcc

http://dev.mysql.com/doc/refman/5.1/en/numeric-types.html

 

Types in MySQL: BigInt(20) vs Int(20) etcc

 

See http://dev.mysql.com/doc/refman/5.1/en/numeric-types.html

INT is a four-byte signed integer. BIGINT is an eight-byte signed integer.

The 20 in INT(20) and BIGINT(20) means almost nothing. It's a hint for display width, it has nothing to do with storage. Practically, it affects only the ZEROFILL option:

 

Sql代码   收藏代码
  1. CREATE TABLE foo ( bar INT(20) ZEROFILL );  
  2. INSERT INTO foo (bar) VALUES (1234);  
  3. SELECT bar from foo;  
  4.   
  5. +----------------------+  
  6. | bar                  |  
  7. +----------------------+  
  8. | 00000000000000001234 |  
  9. +----------------------+  
 

 

The number in parentheses in a type declaration is display width , which is unrelated to the range of values that can be stored in a data type. Just because you can declare Int(20) does not mean you can store values up to 10^20 in it:

[...] 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. ...

The display width does not constrain the range of values that can be stored in the column, nor the number of digits that are displayed for values having a width exceeding that specified for the column. For example, a column specified as SMALLINT(3) has the usual SMALLINT range of -32768 to 32767, and values outside the range allowed by three characters are displayed using more than three characters.

For a list of the maximum and minimum values that can be stored in each MySQL datatype, see here .

 

Mysql的数字类型(1B,2B,3B,4B,8B都有)

一个INT,占4个字节,跟JAVA中的int一样,即使是有符号也能表达21亿 这么大的数据。 所以平时绝大多数情况,包括大型网站的UID,都用不了这么大的数据,好友关系表可能会超过,关系表可以考虑用BIGINT。还有就是平时时间戳需要用BIGINT。总之,不要轻易用上BIGINT,完全是浪费!

转载于:https://www.cnblogs.com/yuhuameng/p/11429543.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Bigint类型可以存储比int类型更大的整数,Bigint类型可以存储范围从-9223372036854775808到9223372036854775807之间的数字,而int类型只能存储范围从-2147483648到2147483647之间的数字。 ### 回答2: MySQL中,bigint和int是整数数据类型的两个选项。它们之间的区别如下: 1. 数据范围:bigint的取值范围更广,可以存储的整数范围从-2^63到2^63-1,而int的取值范围从-2^31到2^31-1。这意味着bigint可以存储更大的整数值。 2. 存储空间:bigint需要更多的存储空间来存储相同数量的数据。bigint使用8个字节(64位),而int只使用4个字节(32位)。因此,如果要存储更大的整数数据集,bigint会占用更多的磁盘空间。 3. 性能:由于bigint使用更多的存储空间,它可能会在大量数据处理时影响查询的性能。相比之下,int数据类型可以更快地执行查询操作。 4. 应用场景:由于bigint可以存储更大的整数值,所以在处理需要大范围整数的应用程序时更常见。例如,在存储用户ID时,bigint可能更合适,因为用户ID可能会非常庞大。 总的来说,bigint和int之间的主要区别是存储范围和存储空间。根据具体的应用场景和需求,我们可以选择合适的数据类型来存储整数数据。 ### 回答3: BigInt和Int都是MySQL中的整数数据类型,不同之处在于它们的存储范围和占用空间。 1. 存储范围: - Int是一个4字节(32位)有符号整数,其存储范围为-2,147,483,648到2,147,483,647。 - BigInt是一个8字节(64位)有符号整数,其存储范围为-9,223,372,036,854,775,808到9,223,372,036,854,775,807。 2. 占用空间: - 由于BigInt的存储范围比Int更大,所以它需要更多的存储空间。Int占用4个字节(32位),而BigInt占用8个字节(64位)。 3. 适用场景: - 如果你的数据范围可以在Int的存储范围内完成,那么使用Int可能是更好的选择,因为它占用更少的空间。 - 如果你需要存储非常大的整数或需要更大的存储范围,那么使用BigInt是更合适的选择。 总结:BigInt提供了更大的存储范围,但相应地占用更多的存储空间。选择使用哪个数据类型取决于你的数据的范围和你对空间利用率的考虑。如果你不确定应该使用哪个数据类型,可以根据你的需求来评估并选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值