mysql int zerofill_Mysql 中int[M]—zerofill-阿里云开发者社区
我們在定義數字類型的數據類型的時候,往往考慮該數字類型的數據能否裝的下該字段的最大值,如狀態位的字段:tinyint,表的主鍵:int,或者bigint,今天在看到開發同學提交表結構設計文檔中看到數值類型的字段定義為int(255),int(20),int(2)當時一下還沒有反映過來,我們知道在mysql中數字類型用定長字節存儲:
Column Type
Bytes On Disk
tinyint
1 bytes
smallint
2 bytes
mediumint
3 bytes
int
4 bytes
bigint
8 bytes
估計他們這樣定義是想為了存儲更多范圍的數據;
那這個int[M]中M是什么意義喃,在定義數值型數據類型的時候,可以在關鍵字括號內指定整數值(如:int(M),M的最大值為255)顯示最大顯示寬度,顯示寬度M與數據所占用空間,數值的范圍無關。 如果在定義字段的時候指定zerofill,那么當數值的顯示寬度小于指定的列寬度時候,則默認補充的空格用0代替。
Zerofill: root@test 10:24:26>create table int_12(id int(12) zerofill);
Query OK, 0 rows affected (0.11 sec)
root@test 10:28:07>insert into int_12 values(1);
Query OK, 1 row affected (0.00 sec)
root@test 10:28:18>select * from int_12;
+————–+
| id?????????? |
+————–+
| 000000000001 |
+————–+
1 row in set (0.00 sec)
Nozerofill:
root@test 11:13:31>desc int_12_no;
+——-+———+——+—–+———+——-+
| Field | Type??? | Null | Key | Default | Extra |
+——-+———+——+—–+———+——-+
| id??? | int(12) | YES? |???? | NULL??? |?????? |
root@test 11:14:16>insert into int_12_no values(1);
Query OK, 1 row affected (0.00 sec)
root@test 11:14:32>select * from int_12_no;
+——+
| id?? |
+——+
|??? 1 |
+——+
1 row in set (0.00 sec)
存儲結構:
root@test 11:14:38>select id,hex(id) from int_12_no;
+——+———+
| id?? | hex(id) |
+——+———+
|??? 1 | 1?????? |
+——+———+
1 row in set (0.00 sec)
root@test 11:15:17>select id,hex(id) from int_12;
+————–+———+
| id?????????? | hex(id) |
+————–+———+
| 000000000001 | 1?????? |
+————–+———+
1 row in set (0.00 sec)
可以看到通過hex函數導出內部的存儲結構是一樣的,所以占用的空間大小是相同的;
數值范圍:
root@test 11:17:42>create table test_4(id int(4) zerofill);
Query OK, 0 rows affected (0.12 sec)
root@test 11:18:17>insert into test_4 values(12345);
Query OK, 1 row affected (0.00 sec)
root@test 11:18:29>select * from test_4;
+——-+
| id??? |
+——-+
| 12345 |
+——-+
1 row in set (0.00 sec)
可以看到,當數值的范圍超過指定顯示范圍后,并沒有出現截斷的情況。
總結
以上是生活随笔為你收集整理的mysql int zerofill_Mysql 中int[M]—zerofill-阿里云开发者社区的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bootstrap项目更改为vue_取代
- 下一篇: python collection co