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. | bar                  |  

  6. +----------------------+

  7. | 00000000000000001234 |  

  8. +----------------------+

 

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,完全是浪费!

点赞
收藏

评论区

加载中...

相关推荐

VirturalBox中搭建CentOS开发环境实录(二)

安装MySQL_Yum__安装:_官方安装步骤:http://dev.mysql.com/doc/refman/5.7/en/linuxinstallationyumrepo.html(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fdev.

mysql使用utf8mb4经验吐血总结

mysql使用utf8mb4经验吐血总结1\.utf8与utf8mb4异同先看官方手册https://dev.mysql.com/doc/refman/5.6/en/charsetunicodeutf8mb4.html(https://www.o

HIVE 时间操作函数

日期函数UNIX时间戳转日期函数: from\_unixtime语法:   from\_unixtime(bigint unixtime\, string format\)返回值: string说明: 转化UNIX时间戳(从19700101 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式举例:hive   selec

MyBatis(四):mybatis中使用in查询时的注意事项

准备工作1)创建测试表jobitemCREATETABLE"jobitem"("id"bigint(20)NOTNULLAUTO_INCREMENTCOMMENT'唯一键pk',"appId"varchar(32)NOTNULLCOMMENT'yarn任务id(appl

MySQL 锁(5)

InnoDB引擎有8类锁https://dev.mysql.com/doc/refman/5.7/en/innodblocking.html(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fdev.mysql.com%2Fdoc%2Frefman%2F5.7%2Fen%2

Mysql 分区表

DROPTABLEIFEXISTS\frank\_test\;CREATETABLE\frank\_test\(\id\bigint(20)NOTNULLAUTO\_INCREMENTCOMMENT'主键id',\gid\bigint(20)DEFAULT'0'COMMENT'基础表id'