因為看到 int(10)、int(5)、tinyint(5)、int(11) unsigned、smallint(5) 這些寫法,感覺頭真的好痛。
不知道這是多久以前版本的寫法了,還是根本沒這寫法,因為我也接觸不深。
所以弄了一個測試的環境,一開始 table 長這樣子,測試 MySQL integer type 的實際資料情形。
mysql> SHOW CREATE TABLE data_type;
+-----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| data_type | CREATE TABLE `data_type` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`int_normal` int(11) DEFAULT NULL,
`int_unsigned_normal` int(10) unsigned DEFAULT NULL,
`int_11` int(11) DEFAULT NULL,
`int_10` int(10) DEFAULT NULL,
`int_5` int(5) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=COMPRESSED |
+-----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
然後塞各種測試資料及驗證。
mysql> insert into data_type (int_normal, int_unsigned_normal, int_11, int_10, int_5) VALUE (0,0,0,0,0);
Query OK, 1 row affected (0.00 sec)
mysql> insert into data_type (int_normal, int_unsigned_normal, int_11, int_10, int_5) VALUE (1,1,1,1,1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into data_type (int_normal, int_unsigned_normal, int_11, int_10, int_5) VALUE (-1,-1,-1,-1,-1);
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> SHOW WARNINGS;
+---------+------+--------------------------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------------------------+
| Warning | 1264 | Out of range value for column 'int_unsigned_normal' at row 1 |
+---------+------+--------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> insert into data_type (int_normal, int_unsigned_normal, int_11, int_10, int_5) VALUE (2147483647,2147483647,2147483647,2147483647,2147483647);
Query OK, 1 row affected (0.00 sec)
mysql> insert into data_type (int_normal, int_unsigned_normal, int_11, int_10, int_5) VALUE (-2147483648,-2147483648,-2147483648,-2147483648,-2147483648);
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> SHOW WARNINGS;
+---------+------+--------------------------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------------------------+
| Warning | 1264 | Out of range value for column 'int_unsigned_normal' at row 1 |
+---------+------+--------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> insert into data_type (int_normal, int_unsigned_normal, int_11, int_10, int_5) VALUE (4294967295,4294967295,4294967295,4294967295,4294967295);
Query OK, 1 row affected, 4 warnings (0.00 sec)
mysql> SHOW WARNINGS;
+---------+------+-----------------------------------------------------+
| Level | Code | Message |
+---------+------+-----------------------------------------------------+
| Warning | 1264 | Out of range value for column 'int_normal' at row 1 |
| Warning | 1264 | Out of range value for column 'int_11' at row 1 |
| Warning | 1264 | Out of range value for column 'int_10' at row 1 |
| Warning | 1264 | Out of range value for column 'int_5' at row 1 |
+---------+------+-----------------------------------------------------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM data_type;
+----+-------------+---------------------+-------------+-------------+-------------+
| id | int_normal | int_unsigned_normal | int_11 | int_10 | int_5 |
+----+-------------+---------------------+-------------+-------------+-------------+
| 1 | 0 | 0 | 0 | 0 | 0 |
| 2 | 1 | 1 | 1 | 1 | 1 |
| 3 | -1 | 0 | -1 | -1 | -1 |
| 4 | 2147483647 | 2147483647 | 2147483647 | 2147483647 | 2147483647 |
| 5 | -2147483648 | 0 | -2147483648 | -2147483648 | -2147483648 |
| 6 | 2147483647 | 4294967295 | 2147483647 | 2147483647 | 2147483647 |
+----+-------------+---------------------+-------------+-------------+-------------+
6 rows in set (0.00 sec)
mysql>
所以,還是附上 MySQL 5.6 Manual 關於括號裡面的參數:
1. 基本上沒有跟著 ZEROFILL 是沒用的
2. 找一個最適合的資料型態用,不然會爆表還是會爆表
3. 我已經對這些人放棄治療了