MySQL int(M)瞭解


MySQL int(M)瞭解

在MySQL創建表時,通常會看到某個int字段,定義爲 a int(10) b int(11)....
疑問,那麼這裏的10,11..,是什麼?

查詢了下,只有在加 zerofill時,才能看出效果,否則,select結果出來一樣,但是,
我們姑且叫了"寬度"
create table t1(Id int(3) zerofill) engine=InnoDB;
insert into t1 select 1;

insert into t1 select 11;

insert into t1 select 111;

insert into t1 select 1111;

查詢如下:
mysql> select * from t1;
+------+
| Id   |
+------+
|  001 |
|  011 |
|  111 |
| 1111 |
+------+
7 rows in set (0.00 sec)
create table t2(Id int(3))engine=InnoDB;
insert into t2 select 1;
insert into t2 select 11;
insert into t2 select 111;
查詢結果如下:
mysql> select * from t2;
+------+
| Id   |
+------+
|    1 |
|   11 |
|  111 |
+------+
3 rows in set (0.00 sec)

綜上所述:當你輸入11時,會默認給你存儲011,那麼int(3)中3就是一數據長度(寬度),當你不足3位時,幫你補全,超過

3,也無影響;如int(1)和int(10)存儲格式上有點區別,使用時沒有不同,只在zerofill下才可看出點點效果.

注意:不論是int(1),int(10)...數據庫存儲或數據佔的空間是4個字節長度,和這個1..10沒有關係;有關係是在定義

int類型是有無unsigned(符號),存儲數據範圍不同;




發佈了20 篇原創文章 · 獲贊 0 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章