標籤:官方文檔 補充 idt val 資料 limits ext sig variable
mysql的資料類型和sqlserver的資料類型基本一致,但是,mysql有個容易讓人感到困惑的地方,那就是mysql的int、char、varchar這兩種資料類型都要寫成int(n)、char(n)、varchar(n)這種格式的,那麼這裡的這個n到底是什麼意思呢?很多人都會認為,這裡的n是最大儲存長度,比如說。int(10)就能儲存一個10位的數字,int(9)就只能儲存一個長度是9位的數字。事實上不是這樣的。首先來說一下int裡面的n是什麼意思。
按照mysql的官方解釋:
int(M): M indicates the maximum display width for integer types.翻譯成中文的意思就是,這裡括弧裡面的M代表了最大的顯示長度,而不是儲存的長度。
1、整型MySQL資料類型 含義(有符號)tinyint(m) 1個位元組 範圍(-128~127)smallint(m) 2個位元組 範圍(-32768~32767)mediumint(m) 3個位元組 範圍(-8388608~8388607)int(m) 4個位元組 範圍(-2147483648~2147483647)bigint(m) 8個位元組 範圍(+-9.22*10的18次方)
根據上面的資訊可以得出結論:不管是int(10)還是int(9),最終都佔四個位元組,既然占的位元組數是一個定值,那麼最終能儲存的資料範圍也是一個定值。所以“int(10)就能儲存一個10位的數字,int(9)就只能儲存一個長度是9位的數字。是不對的。”
原來,在 int(M) 中,M 的值跟 int(M) 所佔多少儲存空間並無任何關係。 int(3)、int(4)、int(8) 在磁碟上都是佔用 4 btyes 的儲存空間。說白了,除了顯示給使用者的方式有點不同外,int(M) 跟 int 資料類型是相同的。
另外,int(M) 只有跟 zerofill 結合起來,才能使我們清楚的看到不同之處。
mysql> drop table if exists t;mysql> create table t(id int zerofill);mysql> insert into t(id) values(10);mysql> select * from t;+------------+| id |+------------+| 0000000010 |+------------+mysql> alter table t change column id id int(3) zerofill;mysql> select * from t;+------+| id |+------+| 010 |+------+mysql>mysql> alter table t change column id id int(4) zerofill;mysql> select * from t;+------+| id |+------+| 0010 |+------+mysql>mysql> insert into t(id) values(1000000);mysql> select * from t;+---------+| id |+---------+| 0010 || 1000000 |+---------+
從上面的測試可以看出,“(M)”指定了 int 型數值顯示的寬度,如果欄位資料類型是 int(4),則:當顯示數值 10 時,在左邊要補上 “00”;當顯示數值 100 是,在左邊要補上“0”;當顯示數值 1000000 時,已經超過了指定寬度“(4)”,因此按原樣輸出。 在使用 MySQL 資料類型中的整數類型(tinyint、smallint、 mediumint、 int/integer、bigint)時,非特殊需求下,在資料類型後加個“(M)”,我想不出有何意義。
但是,char(n)、varchar(n)又和int(n)裡面的n是不一樣的含義。我們再看一下官方的解釋。
第一:討論char(n)
The length of a CHAR column is fixed to the length that you declare when you create the table. The length can be any value from 0 to 255. When CHAR values are stored, they are right-padded with spaces to the specified length.When CHAR values are retrieved, trailing spaces are removed unless the PAD_CHAR_TO_FULL_LENGTH [581]SQL mode is enabled.
手冊中指出,長度的限制是0-255,經過驗證,長度的限制是0-255個字元。也就是說,沒有明確標出位元組的限制數。
也就是說,char(2)最多可以儲存兩個字元長度的字串,char(5)最多可以儲存五個字元長度的字串。char(n)中的n限定了所能儲存的字元個數。
第二:討論varchar(m)
Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535.The effective maximum length of a VARCHARis subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used. See Section E.10.4, “Table Column-Count and Row-Size Limits”.
官方文檔指出,varchar(m)中的m限定了varchar所能儲存的位元組個數。
下面補充一下資料類型
1、整型
1 MySQL資料類型 含義(有符號)2 tinyint(m) 1個位元組 範圍(-128~127)3 smallint(m) 2個位元組 範圍(-32768~32767)4 mediumint(m) 3個位元組 範圍(-8388608~8388607)5 int(m) 4個位元組 範圍(-2147483648~2147483647)6 bigint(m) 8個位元組 範圍(+-9.22*10的18次方)
取值範圍如果加了unsigned,則最大值翻倍,如tinyint unsigned的取值範圍為(0~256)。
int(m)裡的m是表示SELECT查詢結果集中的顯示寬度,並不影響實際的取值範圍,沒有影響到顯示的寬度,不知道這個m有什麼用。
2、浮點型(float和double)
1 MySQL資料類型 含義2 float(m,d) 單精確度浮點型 8位精度(4位元組) m總個數,d小數位3 double(m,d) 雙精確度浮點型 16位精度(8位元組) m總個數,d小數位
設一個欄位定義為float(5,3),如果插入一個數123.45678,實際資料庫裡存的是123.457,但總個數還以實際為準,即6位。
3、定點數
浮點型在資料庫中存放的是近似值,而定點類型在資料庫中存放的是精確值。
1 decimal(m,d) 參數m<65 是總個數,d<30且 d<m 是小數位。
4、字串(char,varchar,_text)
1 MySQL資料類型 含義2 char(n) 固定長度,最多255個字元3 varchar(n) 可變長度,最多65535個字元4 tinytext 可變長度,最多255個字元5 text 可變長度,最多65535個字元6 mediumtext 可變長度,最多2的24次方-1個字元7 longtext 可變長度,最多2的32次方-1個字元
5、日期和時間資料類型
1 MySQL資料類型 含義2 date 3位元組,日期,格式:2014-09-183 time 3位元組,時間,格式:08:42:304 datetime 8位元組,日期時間,格式:2014-09-18 08:42:305 timestamp 4位元組,自動儲存記錄修改的時間6 year 1位元組,年份
Mysql資料類型