在SQL語句中int代表你要創(chuàng)建字段的類型,int代表整型,11代表字段的長度,這個代表顯示寬度.
整數(shù)列的顯示寬度與mysql需要用多少個字符來顯示該列數(shù)值,與該整數(shù)需要的存儲空間的大小都沒有關(guān)系,比如,不管設(shè)定了顯示寬度是多少個字符,bigint都要占用8個字節(jié)。
int是整型,(11)是指顯示字符的長度,但要加參數(shù)的,最大為255,比如它是記錄行數(shù)的id,插入10筆資料,它就顯示00000000001 ~~~00000000010,當(dāng)字符的位數(shù)超過11,它也只顯示11位,如果你沒有加那個讓它未滿11位就前面加0的參數(shù),它不會在前面加0
聲明整型數(shù)據(jù)列時,我們可以為它指定個顯示寬度M(1~255),如INT(5),指定顯示寬度為5個字符,如果沒有給它指定顯示寬度,MySQL會為它指定一個默認(rèn)值。顯示寬度只用于顯示,并不能限制取值范圍和占用空間,如:INT(3)會占用4個字節(jié)的存儲空間,并且允許的最大值也不會是999,而是INT整型所允許的最大值。
MySQL有五種整型數(shù)據(jù)列類型,即TINYINT,SMALLINT,MEDIUMINT,INT和BIGINT。它們之間的區(qū)別是取值范圍不同,存儲空間也各不相同。
在整型數(shù)據(jù)列后加上UNSIGNED屬性可以禁止負(fù)數(shù),取值從0開始。
int范圍:
- Type Bytes Minimum Value Maximum Value
- (Signed/Unsigned) (Signed/Unsigned)
- TINYINT 1 -128 127
- 0 255
- SMALLINT 2 -32768 32767
- 0 65535
- MEDIUMINT 3 -8388608 8388607
- 0 16777215 --Vevb.com
- INT 4 -2147483648 2147483647
- 0 4294967295
- BIGINT 8 -9223372036854775808 9223372036854775807
- 0 18446744073709551615
下面為官網(wǎng)的說明,代碼如下:
- Be careful when considering ENUM('T','F') as "true binary".
- Example:
- CREATE TABLE `bits` (
- `val` ENUM('T','F') NOT NULL
- );
- mysql> INSERT INTO `bits` (`val`) VALUES ('W'), ('T'), ('F');
- Query OK, 3 rows affected, 1 warning (0.00 sec)
- Records: 3 Duplicates: 0 Warnings: 1
- mysql> SHOW WARNINGS;
- +---------+------+------------------------------------------+| Level | Code | Message |+---------+------+------------------------------------------+| Warning | 1265 | Data truncated for column 'val' at row 1 |+---------+------+------------------------------------------+1 row in set (0.00 sec)
- mysql> SELECT COUNT(DISTINCT val) FROM bits;
- +---------------------+| COUNT(DISTINCT val) |+---------------------+| 3 |+---------------------+1 row in set (0.00 sec)
- Well, shouldn't a binary type have only two distinct values?
- (Note that it isn't NULL.)
- Explanation from manual (10.4.4. The ENUM Type):
- -----
- If you insert an invalid value into an ENUM (that is, a string not present in the list of permitted values), the empty string is inserted instead as a special error value. This string can be distinguished from a “normal” empty string by the fact that this string has the numeric value 0. More about this later
新聞熱點
疑難解答
圖片精選