1. 程式人生 > >MYSQL 字段類型之TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT

MYSQL 字段類型之TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT

tiny lin lte 參數 evel 業務 關系 modify integer

1. tinyint: 只能存儲-128到127大小的數字, 不在乎後面的定義,如tinyint(9). 需1字節存儲空間。 當加有UNSIGNED後,數字可以從0增加到255大小。
dba@localhost : test 22:36:25> create table ti(id tinyint(9), no tinyint(1));
Query OK, 0 rows affected (0.03 sec)
 
dba@localhost : test 22:36:44> insert into ti values(129,130);
Query OK, 1 row affected, 2 warnings (0.00 sec)
 
dba@localhost : test 22:37:31> show warnings;
+---------+------+---------------------------------------------+
| Level   | Code | Message                                     |
+---------+------+---------------------------------------------+
| Warning | 1264 | Out of range value for column ‘id‘ at row 1 |
| Warning | 1264 | Out of range value for column ‘no‘ at row 1 |
+---------+------+---------------------------------------------+
2 rows in set (0.00 sec)
這裏在插入數據時會報出警告,是因為SQL_MODE的關系。MYSQL這裏默認設置為空, 允許超出範圍的數據插入, 只給出一個警告。
可以查詢下這個表:
dba@localhost : test 22:37:39> select * from ti;
+------+------+
| id   | no   |
+------+------+
|  127 |  127 |
+------+------+
1 row in set (0.00 sec)
 
這個時候是允許插入負數的。
dba@localhost : test 22:49:40> insert into ti values(-9,9);
Query OK, 1 row affected (0.00 sec)
 
dba@localhost : test 22:50:06> select * from ti;
+------+------+
| id   | no   |
+------+------+
|  127 |  127 |
|   -9 |    9 |
+------+------+
2 rows in set (0.00 sec)
 
為了不讓數據庫產生負數的值, 我們可以修改下這個字段的類型參數:
dba@localhost : test 22:46:25> alter table ti modify no tinyint unsigned;
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0
 
dba@localhost : test 22:50:18> insert into ti values(-9,-9);
Query OK, 1 row affected, 1 warning (0.00 sec)
 
dba@localhost : test 22:51:20> show warnings;
+---------+------+---------------------------------------------+
| Level   | Code | Message                                     |
+---------+------+---------------------------------------------+
| Warning | 1264 | Out of range value for column ‘no‘ at row 1 |
+---------+------+---------------------------------------------+
1 row in set (0.00 sec)
 
dba@localhost : test 22:51:27> select * from ti;
+------+------+
| id   | no   |
+------+------+
|  127 |  127 |
|   -9 |    9 |
|   -9 |    0 |
+------+------+
3 rows in set (0.00 sec)
 
可以看到NO字段插入-9, 直接被截成0了。
 
在對TIINYINT增加了UNSIGNED後, 從-128 至 127 變成了 0 至 255.
 
dba@localhost : test 22:51:37> insert into ti values(1,258);
Query OK, 1 row affected, 1 warning (0.00 sec)
 
dba@localhost : test 22:54:08> select * from ti;
+------+------+
| id   | no   |
+------+------+
|  127 |  127 |
|   -9 |    9 |
|   -9 |    0 |
|    1 |  255 |
+------+------+
4 rows in set (0.00 sec)
 
此類型的字段應用場景:  如果有業務字段只存儲性別、狀態、類型等少量值的時候,就可以使用此類型的字段。
 
2. smallint: 只能存儲-32768到32767大小的數字, 不在乎後面的定義,如smallint(9). 需2字節存儲空間。對字段加上UNSIGNED後,數字大小就可以從0增加到65535大小。
dba@localhost : test 23:00:21> create table ts(id smallint(1), no smallint(10) unsigned);
Query OK, 0 rows affected (0.01 sec)
 
 
dba@localhost : test 23:01:51> insert into ts values(-32768,32768);
Query OK, 1 row affected (0.00 sec)
 
dba@localhost : test 23:02:30> select * from ts;
+--------+-------+
| id     | no    |
+--------+-------+
| -32768 | 32768 |
+--------+-------+
1 row in set (0.00 sec)
從以上可以看出, 我們存儲了一個帶符號的值,也就是負數,長度為1,這個也超出了我們的設置。 另外, 對字段加上UNSIGNED後,數字就可以存儲超過32767的值。
應用場景: 當需要的值比TINYINT要多,而又不需要超過65535的時候就可以使用這個類型。
 
 
3. mediumint: 只能存儲  -8388608 到 8388607大小的數字, 不在乎後面的定義,如mediumint(9). 需3字節存儲空間。對字段加上UNSIGNED後,數字大小就可以從0增加到16777215大小.
dba@localhost : test 23:13:10> create table tm(id mediumint(1), no mediumint(10) unsigned);
Query OK, 0 rows affected (0.01 sec)
 
dba@localhost : test 23:13:21> insert into tm values(-8388608,16777216);
Query OK, 1 row affected, 1 warning (0.00 sec)
 
dba@localhost : test 23:14:44> select * from tm;
+----------+----------+
| id       | no       |
+----------+----------+
| -8388608 | 16777215 |
+----------+----------+
1 row in set (0.00 sec)
從以上可以看出, 字段長度為1, 而存儲的值遠遠超過1位。字段加UNSIGNED,就可以存儲0到16777215的數字。
應用場景:當需要的值比SMALLINT的值多些,而又不需要超過16777215大小的時候就可以利用這個類型。
 
4. int/integer: 只能存-2147483648 到 2147483647大小的數字, 不在乎後面的定義,如int(9). 需4字節存儲空間。對字段加上UNSIGNED後,數字大小就可以從0增加到4294967295大小.
dba@localhost : test 23:14:52> create table tn(id int(1), no int(10) unsigned);
Query OK, 0 rows affected (0.01 sec)
 
dba@localhost : test 23:22:20> insert into tn values(-2147483648,4294967296);
Query OK, 1 row affected, 1 warning (0.00 sec)
 
dba@localhost : test 23:22:22> select * from tn;
+-------------+------------+
| id          | no         |
+-------------+------------+
| -2147483648 | 4294967295 |
+-------------+------------+
1 row in set (0.00 sec)
從以上可以看出, 字段長度為1, 而存儲的值遠遠超過1位。字段加UNSIGNED,就可以存儲0到4294967295的數字。
應用場景:當需要的值比mediumint的值多些,而又不需要超過4294967295大小的時候就可以利用這個類型。 通常主鍵字段會以INT類型自增的形式。
 
5. bigint: 只能存-9223372036854775808 到 9223372036854775807大小的數字, 不在乎後面的定義,如bigint(9). 需8字節存儲空間。對字段加上UNSIGNED後,數字大小就可以從0增加到18446744073709551615大小.
dba@localhost : test 23:22:28> create table tb(id bigint(1), no bigint(10) unsigned);
Query OK, 0 rows affected (0.01 sec)
 
dba@localhost : test 23:27:40> insert into tb values(-9223372036854775808,18446744073709551616);
Query OK, 1 row affected, 1 warning (0.01 sec)
 
dba@localhost : test 23:28:41> select * from tb;
+----------------------+----------------------+
| id                   | no                   |
+----------------------+----------------------+
| -9223372036854775808 | 18446744073709551615 |
+----------------------+----------------------+
1 row in set (0.00 sec)
從以上可以看出, 字段長度為1, 而存儲的值遠遠超過1位。字段加UNSIGNED,就可以存儲0到18446744073709551615的數字。
應用場景:當需要的值比int的值多些,而又不需要超過18446744073709551615大小的時候就可以利用這個類型。此類型很少用,可以存到20位數字大小。
 
總結:
在MYSQL裏,我們要求越精短越好,因為存儲的容量小了,才能從根本上減少IO的掃描,如果在此字段上建主鍵索引或者建輔助索引,也能有效的減少存儲空間,與IO的壓力。

  

MYSQL 字段類型之TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT