码迷,mamicode.com
首页 > 数据库 > 详细

mysql 数据类型_INT

时间:2018-10-16 10:17:50      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:lint   -128   函数   数据存储   sel   ipv6   timestamp   长度限制   时间戳   

整型分类:

1.tinyint 1Bytes -128~127(255)
2.smallint 2Bytes -32768~32676(65535)
3.mdeiumint 3Bytes -8388608~8388607(16777215)
4.int 4Bytes -2147483648~2147483647(4294967295,42亿)
5.bigint 8Bytes -9223372036854775808~9223372036854775807(18446744073709551615)

int转bigint

int列如果是主键不能online DDL 转成bigint,onlineDDL:ptosc(更容易导致主从数据延时,执行过程报错,无法从上一个位置开始,只能重头开始)和gh-ost(推荐,因基于binlog,可以随时停止或继续).

容易误解

int(11),11是修饰符,不是长度限制,int(8)zerofill zerofill也是修饰符,左侧补零.

溢出:

cast(9223372036854775807 as unsigned) 改为不符号

常见特别格式的数据存储类型:IPv4地址推荐用INT存储:

select length(‘255.255.255.255‘)
+---------------------------+
| length(‘255.255.255.255‘) |
+---------------------------+
| 15 |
+---------------------------+
root@localhost [(none)]>select inet_aton(‘255.255.255.255‘); //IPv4最大值,正好是int的无符号数最大值.
+------------------------------+
| inet_aton(‘255.255.255.255‘) |
+------------------------------+
| 4294967295 |
+------------------------------+
root@localhost [(none)]>select inet_ntoa(4294967295);
+-----------------------+
| inet_ntoa(4294967295) |
+-----------------------+
| 255.255.255.255 |
+-----------------------+

IPv6和IPv4共用的方法(数据类型为VARBINARY(16)

唯一的原因是MySQL函数同时适用于IPv6和IPv4地址。 BINARY(16)只适用于存储IPv6地址,并保存一个字节。在处理IPv6和IPv4地址时应使用VARBINARY(16)):

root@localhost [(none)]>SELECT HEX(INET6_ATON(‘fdfe::5a55:caff:fefa:9089‘));
+----------------------------------------------+
| HEX(INET6_ATON(‘fdfe::5a55:caff:fefa:9089‘)) |
+----------------------------------------------+
| FDFE0000000000005A55CAFFFEFA9089 |
+----------------------------------------------+
1 row in set (0.00 sec)
root@localhost [(none)]>SELECT HEX(INET6_ATON(‘192.168.9.1‘));
+--------------------------------+
| HEX(INET6_ATON(‘192.168.9.1‘)) |
+--------------------------------+
| C0A80901 |
+--------------------------------+
1 row in set (0.00 sec)

mysql> SELECT INET6_NTOA(INET6_ATON(‘fdfe::5a55:caff:fefa:9089‘));
-> ‘fdfe::5a55:caff:fefa:9089‘
mysql> SELECT INET6_NTOA(INET6_ATON(‘192.168.9.1‘));
-> ‘192.168.9.1‘

mysql> SELECT INET6_NTOA(UNHEX(‘FDFE0000000000005A55CAFFFEFA9089‘));
-> ‘fdfe::5a55:caff:fefa:9089‘
mysql> SELECT INET6_NTOA(UNHEX(‘C0A80901‘));
-> ‘192.168.9.1‘

此外,时间戳也是推荐使用无符号int也存储.

select unix_timestamp();
root@localhost [(none)]>select unix_timestamp();
+------------------+
| unix_timestamp() |
+------------------+
| 1539644023 |
+------------------+
1 row in set (0.00 sec)

root@localhost [(none)]>select from_unixtime(1539644023);
+---------------------------+
| from_unixtime(1539644023) |
+---------------------------+
| 2018-10-16 06:53:43 |
+---------------------------+
1 row in set (0.00 sec)

mysql 数据类型_INT

标签:lint   -128   函数   数据存储   sel   ipv6   timestamp   长度限制   时间戳   

原文地址:https://www.cnblogs.com/2woods/p/9795811.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!