1. 程式人生 > >MySQL資料型別數值型之整數型之tinyint

MySQL資料型別數值型之整數型之tinyint

1.1 tinyint說明

型別

長度

不指定時長度為

佔用位元組

範圍

有符號

無符號

tinyint

3

4

1(8bit)

-128~127

-128~127

0~255


id           tinyint(M)       [UNSIGNED]   [ZEROFILL]

欄位名   欄位型別(長度)      [無符號]        [前導填充]


unsigned

01:tinyint(M)後面加上unsigned後,就是無符號(tinyint的範圍就是0~255)

02:tinyint(M)後面不加上unsigned,就是有符號(tinyint的範圍就是-128~127)


zerofill

01:進行前導零填充

02:tinyint(M)加上zerofile後,同時也會把unsigned引數也帶上(tinyint範圍0~255)

1.2 實踐檢驗

##建立chenliang

mysql> create database if not exists chenliang;

Query OK, 1 row affected (0.03 sec)

mysql> show databases like "chenliang";

+----------------------+

| Database (chenliang) |

+----------------------+

| chenliang                 |

+----------------------+

1 row in set (0.03 sec)


##進入chenliang

mysql> use chenliang;

Database changed

mysql> select database();

+------------+

| database()  |

+------------+

| chenliang   |

+------------+

1 row in set (0.01 sec)


測試一:測試欄位加上UNSIGNED後情況下插入資料的情況

##建立test1測試表這裡指定了UNSIGNED,也就是無符號

mysql> CREATE TABLE IF NOT EXISTS test1(

-> id tinyint(3) UNSIGNED

-> );

Query OK, 0 rows affected (0.06 sec)


mysql> desc test1;

+-------+----------------------+------+-----+---------+-------+

 | Field   | Type                         | Null   | Key  | Default  | Extra   |

+-------+----------------------+------+-----+---------+-------+

 | id        | tinyint(3) unsigned  | YES   |         | NULL     |            |

+-------+----------------------+------+-----+---------+-------+

1 row in set (0.03 sec)


****測試01:測試插入範圍0~255的整數和走近255的整數

mysql> INSERT INTO test1 values(0); <==插入數值0,正常(沒有超過範圍)

Query OK, 1 row affected (0.06 sec)


mysql> INSERT INTO test1 values(255); <==插入數值255,正常(沒有超過範圍)

Query OK, 1 row affected (0.05 sec)


mysql> INSERT INTO test1 values(256); <=插入數值256,錯誤(超過了tinyint的範圍)

ERROR 1264 (22003): Out of range value for column 'id' at row 1


mysql> select * from test1;

+------+

| id  |

+------+

| 0  |

| 255 |

+------+

2 rows in set (0.00 sec)


***測試02:測試-128~127範圍的資料是否能夠正常插入

mysql> INSERT INTO test1 values(-1); <==插入負數,報錯

ERROR 1264 (22003): Out of range value for column 'id' at row 1


mysql> INSERT INTO test1 values(-128); <==插入-128,報錯

ERROR 1264 (22003): Out of range value for column 'id' at row 1


mysql> INSERT INTO test1 values(-129); <==插入-129,報錯

ERROR 1264 (22003): Out of range value for column 'id' at row 1


mysql> select * from test1;

+-------+

| id  |

+-------+

| 0   |

| 255  |

+-------+

2 rows in set (0.00 sec)


測試二:測試欄位加ZEROFILL上後情況下插入資料的情況

##建立test2表,(這裡指定了zerofill,也就是前導零填充

mysql> CREATE TABLE IF NOT EXISTS test2(

-> id tinyint(3) ZEROFILL

-> );

Query OK, 0 rows affected (0.07 sec)

mysql> desc test2;

+-------+------------------------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+------------------------------+------+-----+---------+-------+

| id | tinyint(3) unsigned zerofill | YES | | NULL | |

+-------+------------------------------+------+-----+---------+-------+

1 row in set (0.00 sec)

^只指定unsigned,不會有zerofill

^只指定zerofill時,會帶上unsigned

****測試01:測試插入範圍0~255的整數和走近255的整數

mysql> INSERT INTO test2 values(0);

Query OK, 1 row affected (0.01 sec)

mysql> INSERT INTO test2 values(255);

Query OK, 1 row affected (0.08 sec)

mysql> INSERT INTO test2 values(256);

ERROR 1264 (22003): Out of range value for column 'id' at row 1

mysql> select * from test2;

+------+

| id |

+------+

| 000 |

| 255 |

+------+

2 rows in set (0.00 sec)

***測試02測試-128~127範圍的資料是否能夠正常插入

mysql> INSERT INTO test2 values(-1);

ERROR 1264 (22003): Out of range value for column 'id' at row 1

mysql> INSERT INTO test2 values(-128);

ERROR 1264 (22003): Out of range value for column 'id' at row 1

mysql> INSERT INTO test2 values(-129);

ERROR 1264 (22003): Out of range value for column 'id' at row 1

mysql> select * from test2;

+------+

| id |

+------+

| 000 |

| 255 |

+------+

2 rows in set (0.00 sec)

測試一:測試欄位不加unsigned 和ZEROFILL上後情況下插入資料的情況

##建立test3表(不加unsigned和zerofill)

mysql> CREATE TABLE test3(

-> id tinyint(3)

-> );

Query OK, 0 rows affected (0.06 sec)

mysql> desc test3;

+-------+------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+------------+------+-----+---------+-------+

| id | tinyint(3) | YES | | NULL | |

+-------+------------+------+-----+---------+-------+

1 row in set (0.01 sec)

****測試01:測試插入整數

mysql> INSERT INTO test3 values(0);

Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO test3 values(128);

ERROR 1264 (22003): Out of range value for column 'id' at row 1

mysql> select * from test3;

+------+

| id |

+------+

| 0 |

+------+

1 row in set (0.01 sec)


***測試02測試插入負數

mysql> INSERT INTO test3 values(-1);

Query OK, 1 row affected (0.10 sec)

mysql> INSERT INTO test3 values(-128);

Query OK, 1 row affected (0.02 sec)

mysql> INSERT INTO test3 values(-129);

ERROR 1264 (22003): Out of range value for column 'id' at row 1

mysql> select * from test3;

+------+

| id |

+------+

| 0 |

| -1 |

| -128 |

+------+

3 rows in set (0.00 sec)

1.3 tinyint總結

格式:

id tinyint(M) [UNSIGNED] [ZEROFILL]

欄位名 欄位型別(長度) [無符號] [前導填充]


unsigned

01:tinyint(M)後面加上unsigned後,就是無符號(tinyint的範圍就是0~255)

02:tinyint(M)後面不加上unsigned,就是有符號(tinyint的範圍就是-128~127)

zerofill

01:進行前導零填充(插入數值1,表中顯示的是001,因為tinyint的長度為3)

02:tinyint(M)加上zerofile後,同時也會把unsigned引數也帶上(tinyint範圍0~255)