前言:
整型是MySQL中最經常使用的字段類型之一,一般用於存儲整數,其中int是整型中最經常使用的,對於int類型你是否真正瞭解呢?本文會帶你熟悉int類型相關知識,也會介紹其餘整型字段的使用。mysql
整數類型 | 字節 | 有符號範圍 | 無符號範圍 |
---|---|---|---|
TINYINT | 1 | -128 ~ 127 | 0 ~ 255 |
SMALLINT | 2 | -32768 ~ 32767 | 0 ~ 65535 |
MEDIUMINT | 3 | -8388608 ~ 8388607 | 0 ~ 16777215 |
INT/INTEGER | 4 | -2147483648 ~ 2147483647 | 0 ~ 4294967295 |
BIGINT | 8 | -9223372036854775808 ~ 9223372036854775807 | 0 ~ 18446744073709551615 |
表格一共有四列分別表示:字段類型, 佔用字節數, 有符號範圍, 無符號範圍。<br />咱們拿int類型爲例:<br />int類型, 佔用字節數爲4byte, 學過計算機原理的同窗應該知道, 字節(byte)並不是是計算機存儲的最小單位, 還有比字節(byte)更小的單位, 也就是位(bit),一個位就表明一個0或1; 8個位組成一個字節; 通常字節用大寫B來表示byte, 位用小寫b來表示bit.sql
計算機存儲單位的換算:
1B=8b
1KB=1024B
1MB=1024KBshell
那麼根據int類型容許存儲的字節數是4個字節, 咱們就能換算出int UNSIGNED(無符號)類型的能存儲的最小值爲0, 最大值爲4294967295(即4B=32b, 最大值即爲32個1組成,即4294967295換算成二進制則是32個1)。數據庫
mysql> CREATE TABLE test_int ( -> col1 TINYINT, -> col2 SMALLINT, -> col3 MEDIUMINT, -> col4 INT, -> col5 BIGINT -> ) ENGINE = INNODB DEFAULT CHARSET = utf8; Query OK, 0 rows affected (0.01 sec) mysql> show create table test_int\G *************************** 1. row *************************** Table: test_int Create Table: CREATE TABLE `test_int` ( `col1` tinyint(4) DEFAULT NULL, `col2` smallint(6) DEFAULT NULL, `col3` mediumint(9) DEFAULT NULL, `col4` int(11) DEFAULT NULL, `col5` bigint(20) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec) mysql> insert into test_int values (1234,123456,12345678,12345678901,12345678901234567890); Query OK, 1 row affected, 5 warnings (0.00 sec) mysql> insert into test_int values (-1234,-123456,-12345678,-12345678901,-12345678901234567890); Query OK, 1 row affected, 5 warnings (0.01 sec) mysql> show warnings; +---------+------+-----------------------------------------------+ | Level | Code | Message | +---------+------+-----------------------------------------------+ | Warning | 1264 | Out of range value for column 'col1' at row 1 | | Warning | 1264 | Out of range value for column 'col2' at row 1 | | Warning | 1264 | Out of range value for column 'col3' at row 1 | | Warning | 1264 | Out of range value for column 'col4' at row 1 | | Warning | 1264 | Out of range value for column 'col5' at row 1 | +---------+------+-----------------------------------------------+ 5 rows in set (0.01 sec) mysql> select * from test_int; +------+--------+----------+-------------+----------------------+ | col1 | col2 | col3 | col4 | col5 | +------+--------+----------+-------------+----------------------+ | 127 | 32767 | 8388607 | 2147483647 | 9223372036854775807 | | -128 | -32768 | -8388608 | -2147483648 | -9223372036854775808 | +------+--------+----------+-------------+----------------------+
從上述測試中咱們能夠看出:有符號時,各類整型類型最大的存儲範圍,當存儲數字大小不在存儲範圍時,MySQL會產生告警,但數字能夠插入,默認截取爲可存儲的最大值或最小值。ide
咱們常常聽到這句話:int(M)中的M表明最大顯示寬度,"最大顯示寬度"咱們第一反應是該字段的值最大能容許存放的值的寬度,覺得咱們建了int(1),就不能存放數據10了, 其實不是這個意思。<br />整數列的顯示寬度與mysql須要用多少個字符來顯示該列數值,與該整數須要的存儲空間的大小都沒有關係,好比,無論設定了顯示寬度是多少個字符,int都是佔用4個字節,bigint都要佔用8個字節。即int(5)和int(10)可存儲的範圍同樣。<br />整型字段有個ZEROFILL屬性(0填充),在數字長度不夠的數據前面填充0,以達到設定的長度。加上ZEROFILL後M才表現出不一樣,當使用ZEROFILL時,默認會自動加unsigned(無符號)屬性。好比 INT(3) ZEROFILL,你插入到數據庫裏的是10,則實際插入爲010,也就是在前面補充加了一個0,下面咱們來測試下:測試
mysql> CREATE TABLE test_int_zerofill ( -> col1 INT(5) ZEROFILL, -> col2 INT ZEROFILL, -> col3 INT(5) -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.01 sec) mysql> show create table test_int_zerofill\G *************************** 1. row *************************** Table: test_int_zerofill Create Table: CREATE TABLE `test_int_zerofill` ( `col1` int(5) unsigned zerofill DEFAULT NULL, `col2` int(10) unsigned zerofill DEFAULT NULL, `col3` int(5) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec) mysql> insert into test_int_zerofill values (12,12,12); Query OK, 1 row affected (0.00 sec) mysql> select * from test_int_zerofill; +-------+------------+------+ | col1 | col2 | col3 | +-------+------------+------+ | 00012 | 0000000012 | 12 | +-------+------------+------+ 1 row in set (0.00 sec)
那麼有同窗可能會問zerofill有什麼應用場景呢,比較經常使用的應該是月份或日期前補0,這樣顯示的會規範些code
CREATE TABLE `t_zerofill` ( `year` year(4) DEFAULT NULL, `month` int(2) unsigned zerofill DEFAULT NULL, `day` int(2) unsigned zerofill DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; mysql> insert into t_zerofill values (2019,6,5); Query OK, 1 row affected (0.01 sec) mysql> insert into t_zerofill values (2019,6,18); Query OK, 1 row affected (0.00 sec) mysql> insert into t_zerofill values (2019,10,1); Query OK, 1 row affected (0.00 sec) mysql> insert into t_zerofill values (2019,11,11); Query OK, 1 row affected (0.01 sec) mysql> select * from t_zerofill; +------+-------+------+ | year | month | day | +------+-------+------+ | 2019 | 06 | 05 | | 2019 | 06 | 18 | | 2019 | 10 | 01 | | 2019 | 11 | 11 | +------+-------+------+ 4 rows in set (0.00 sec)
通過上面的介紹,關於不一樣整型字段的選取變得容易不少。本着最小化存儲的原則,固然是能選TINYINT不選SMALLINT,能選MEDIUMINT不選INT了,不過一切都要知足業務的前提下儘可能選取佔用字節更少的類型。對於肯定只存儲正整數的字段,能夠加上unsigned屬性,這樣會使存儲範圍更大,好比當字段有AUTO_INCREMENT屬性時,咱們能夠爲int類型加上unsigned屬性。it