MySQL整型數據溢出的處理辦法。本站提示廣大學習愛好者:(MySQL整型數據溢出的處理辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是MySQL整型數據溢出的處理辦法正文
明天接到一個同伙德律風說是覺的數據庫被他人更改了,湧現數據纰謬的成績 。經由良久的排查是數據類型溢出了(產生成績的版本是MySQL 5.1)。後來經由過程給同伙那裡把MySQL 5.1進級到MySQL 5.5去處理這個成績。 這也讓我有興致去懂得一下MySQL分歧版本數據類型溢出的處置機制。
先看一下MySQL支撐的整型數及年夜小,存儲空間:
pe
Storage
Minimum Value
Maximum Value
存儲年夜小
(Bytes)
(Signed/Unsigned)
(Signed/Unsigned)
byte
TINYINT
1
-128
127
1 byte
0
255
SMALLINT
2
-32768
32767
2 bytes
0
65535
MEDIUMINT
3
-8388608
8388607
3 bytes
0
16777215
INT
4
-2147483648
2147483647
4 bytes
0
4294967295
BIGINT
8
-9223372036854775808
9223372036854775807
8 bytes
0
18446744073709551615
別的請記住mysql的數據處置會轉成bigint處置,所以這裡就用bigint幾個測試:
SELECT CAST(0 AS UNSIGNED) - 1;
SELECT 9223372036854775807 + 1;
MySQL 5.1 下:
mysql> SELECT CAST(0 AS UNSIGNED) - 1;
+-------------------------+
| CAST(0 AS UNSIGNED) - 1 |
+-------------------------+
| 18446744073709551615 |
+-------------------------+
1 row in set (0.01 sec)
mysql> SELECT 9223372036854775807 + 1;
+-------------------------+
| 9223372036854775807 + 1 |
+-------------------------+
| -9223372036854775808 |
+-------------------------+
1 row in set (0.01 sec)
MySQL 5.5, 5.6, 5.7下:
mysql> SELECT CAST(0 AS UNSIGNED) - 1;
ERROR 1690 (22003): BIGINT UNSIGNED value is out of range in '(cast(0 as unsigned) - 1)'
mysql>
mysql>
mysql>
mysql> SELECT 9223372036854775807 + 1;
ERROR 1690 (22003): BIGINT value is out of range in '(9223372036854775807 + 1)'
地點處置這類數據是必定要當心溢出(如晚期有作弊沖Q幣就是應用這個辦法處置)
這個成績有能夠會湧現積分新聞,積分相加, 或是一些錢相干的營業中湧現, 主庫5.1 ,從庫MySQL 5.5情形也會湧現分歧步的成績。
建議:這類營業體系盡量的進級到MySQL 5.5後版本
更多概況參考: http://dev.mysql.com/doc/refman/5.7/en/out-of-range-and-overflow.html