在工做中常常要與mysql打交道,可是對mysql的各個字段類型一直都是隻知其一;不知其二,所以寫本文總結記錄一番。mysql
對於int類型的一些基礎知識其實上圖已經說的很明白了,在這裏想討論下經常使用的int(11)表明什麼意思,很長時間以來我都覺得這表明着限制int的長度爲11位,直到有天看到篇文章才明白,11表明的並非長度,而是字符的顯示寬度,在字段類型爲int時,不管你顯示寬度設置爲多少,int類型能存儲的最大值和最小值永遠都是固定的,這裏貼一些原文片斷git
The number in the parenthesis does not determines the max and min values that can be stored in the integer field. The max and min values that can be stored are always fixed.The display width of the column does not affects the maximum value that can be stored in that column. A column with INT(5) or INT(11) can store the same maximum values. Also, if you have a column INT(20) that does not means that you will be able to store 20 digit values (BIGINT values). The column still will store only till the max values of INT.sql
那麼照文中所說,因此不管怎麼設置int類型的顯示寬度,int所能存儲的最大值和最小值是固定的,那麼這個顯示寬度到底有什麼用呢?
當int字段類型設置爲無符號且填充零(UNSIGNED ZEROFILL)時,當數值位數未達到設置的顯示寬度時,會在數值前面補充零直到知足設定的顯示寬度,爲何會有無符號的限制呢,是由於ZEROFILL屬性會隱式地將數值轉爲無符號型,所以不能存儲負的數值。spa
具體用如下代碼解釋。code
首先建立一張表:blog
CREATE TABLE int_demo ( id INT(11) NOT NULL AUTO_INCREMENT, a INT(11) NOT NULL, b INT(11) UNSIGNED ZEROFILL NOT NULL, c INT(5) DEFAULT NULL, d INT(5) UNSIGNED ZEROFILL NOT NULL, e INT(15) DEFAULT NULL, PRIMARY KEY (`id`) )
插入兩條數據圖片
INSERT INTO int_demo (a, b, c, d, e) VALUES (1, 1, 1, 1, 1); INSERT INTO int_demo (a, b, c, d, e) VALUES (1234567890, 1234567890, 1234567890, 1234567890, 1234567890);
select * from int_demo;
id | a | b | c | d | e |
---|---|---|---|---|---|
1 | 1 | 00000000001 | 1 | 00001 | 1 |
2 | 1234567890 | 01234567890 | 1234567890 | 1234567890 | 1234567890 |
註釋:若是用navicate軟件查詢出來並不會顯示左邊的0,但把數據導出時可看到真實的數據,猜想是軟件對數據格式進行了處理?it
從上個例子咱們能夠得出如下幾個結論:table