系統學習----數據類型介紹

數值類型

tinyint 1bytes
smallint 2bytes
mediumint 3bytes
int|intergint 4bytes
bigint 8bytes
float   4bytes  單精度
double 8bytes   雙精度

示例1: int數據類型 (int寬度並不能限制)

create table test(id int(4));
insert test VALUES(11111111);
select * from test;
alter table test change a a int(5);

#創建表一個是默認寬度的int,一個是指定寬度的int(5) mysql> create table t1 (id1 int,id2 int(5)); Query OK, 0 rows affected (0.02 sec)
#像t1中插入數據1,1 mysql> insert into t1 values (1,1); Query OK, 1 row affected (0.01 sec)
#可以看出結果上並沒有異常 mysql> select * from t1;

+‐‐‐‐‐‐+‐‐‐‐‐‐+  
| id1  | id2  |  
+‐‐‐‐‐‐+‐‐‐‐‐‐+  
|    1 |    1 |  
+‐‐‐‐‐‐+‐‐‐‐‐‐+  
row in set (0.00 sec) 

#那麼當我們插入了比寬度更大的值,會不會發生報錯呢? mysql> insert into t1 values (111111,111111); Query OK, 1 row affected (0.00 sec)
#答案是否定的,id2仍然顯示了正確的數值,沒有受到寬度限制的影響

mysql> select * from t1;  
+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+  
| id1        | id2    |  
+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+  
| 0000000001 |  00001 |  
| 0000111111 | 111111 |  
+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+  
rows in set (0.00 sec) 

示例2:小數示例類型; 小數位會被限制,並且遵循四捨五入

#創建表的三個字段分別爲float,double和decimal參數表示一共顯示5位,小數部分佔2位 mysql> create table t2 (id1 float(5,2),id2 double(5,2),id3 decimal(5,2)); Query OK, 0 rows affected (0.02 sec)
#向表中插入1.23,結果正常 mysql> insert into t2 values (1.23,1.23,1.23); Query OK, 1 row affected (0.00 sec)

mysql> select * from t2;  
+‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐+  
| id1  | id2  | id3  |  
+‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐+  
| 1.23 | 1.23 | 1.23 | 
+‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐+  
row in set (0.00 sec) 

#向表中插入1.234,會發現4都被截斷了 mysql> insert into t2 values (1.234,1.234,1.234); Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> select * from t2;  
+‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐+  
| id1  | id2  | id3  |  
+‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐+  
| 1.23 | 1.23 | 1.23 |  
| 1.23 | 1.23 | 1.23 |  
+‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐+  
rows in set (0.00 sec) 

#向表中插入1.235發現數據雖然被截斷,但是遵循了四捨五入的規則 mysql> insert into t2 values (1.235,1.235,1.235); Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> select * from t2;  
+‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐+  
| id1  | id2  | id3  |  
+‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐+  
| 1.23 | 1.23 | 1.23 |  
| 1.23 | 1.23 | 1.23 |  
| 1.24 | 1.24 | 1.24 |  
+‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐+  
rows in set (0.00 sec) 

實例3:時間數據類型

在這裏插入圖片描述

mysql> create table t4 (d date,t time,dt datetime);  
Query OK, 0 rows affected (0.02 sec) 
mysql> desc t4;  
+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+  
| Field | Type     | Null | Key | Default | Extra |  
+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+  
| d     | date     | YES  |     | NULL    |       |  
| t     | time     | YES  |     | NULL    |       |  
| dt    | datetime | YES  |     | NULL    |       |  
+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+  
rows in set (0.01 sec) 
mysql> insert into t4 values (now(),now(),now());  
Query OK, 1 row affected, 1 warning (0.01 sec) 
mysql> select * from t4;  
+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+  
| d          | t        | dt                  |  
+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+  
| 2018‐09‐21 | 14:51:51 | 2018‐09‐21 14:51:51 |  
+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+  
row in set (0.00 sec) 
mysql> insert into t4 values (null,null,null);  
Query OK, 1 row affected (0.01 sec) 
mysql> select * from t4;  
+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+  
| d          | t        | dt                  |  
+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+  
| 2018‐09‐21 | 14:51:51 | 2018‐09‐21 14:51:51 |  
| NULL       | NULL     | NULL                |  
+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+  
rows in set (0.00 sec)

當插入NULL值時,TIMESTAMP數據類型會將當前數據插入

示例4:字符串數據類型:超過指定寬度的字符串內容會被截取

varchar數據類型在定義寬度的時候:不管字符串長度爲多少,總是分配寬度值所對應的長度。
char數據類型在定義寬度的時候;字符串存儲長度爲多少,就分配多長的空間。
在這裏插入圖片描述

mysql> create table t9 (v varchar(4),c char(4));  
Query OK, 0 rows affected (0.01 sec) 
mysql> insert into t9 values ('ab  ','ab  ');  
Query OK, 1 row affected (0.00 sec) 
# 在檢索的時候char數據類型會去掉空格  
mysql> select * from t9;  
+‐‐‐‐‐‐+‐‐‐‐‐‐+  
| v    | c    |  
+‐‐‐‐‐‐+‐‐‐‐‐‐+  
| ab   | ab   |  
+‐‐‐‐‐‐+‐‐‐‐‐‐+  
row in set (0.00 sec) 
# 來看看對查詢結果計算的長度  
mysql> select length(v),length(c) from t9;  
+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐+  
| length(v) | length(c) |  
+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐+  
|         4 |         2 |  
+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐+  
row in set (0.00 sec) 

# 當存儲的長度超出定義的長度,會截斷  
mysql> insert into t9 values ('abcd  ','abcd  ');  
Query OK, 1 row affected, 1 warning (0.01 sec) 
mysql> select * from t9;  
+‐‐‐‐‐‐+‐‐‐‐‐‐+  
| v    | c    |  
+‐‐‐‐‐‐+‐‐‐‐‐‐+  
| ab   | ab   |  
| abcd | abcd |  
+‐‐‐‐‐‐+‐‐‐‐‐‐+  
rows in set (0.00 sec)

示例5:set數據類型和enum數據類型

在這裏插入圖片描述

mysql> create table t10 (name char(20),gender enum('female','male'));  
Query OK, 0 rows affected (0.01 sec) 

# 選擇enum('female','male')中的一項作爲gender的值,可以正常插入  
mysql> insert into t10 values ('nezha','male');  
Query OK, 1 row affected (0.00 sec) 

# 不能同時插入'male,female'兩個值,也不能插入不屬於'male,female'的值  
mysql> insert into t10 values ('nezha','male,female');  
ERROR 1265 (01000): Data truncated for column 'gender' at row 1 

mysql> create table t11 (name char(20),hobby set('抽菸','喝酒','燙頭','翻車'));  
Query OK, 0 rows affected (0.01 sec) 

# 可以任意選擇set('抽菸','喝酒','燙頭','翻車')中的項,並自帶去重功能  
mysql> insert into t11 values ('yuan','燙頭,喝酒,燙頭');  
Query OK, 1 row affected (0.01 sec) 

mysql> select * from t11;  
+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+  
| name | hobby        |  
+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+  
| yuan | 喝酒,燙頭     |  
+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+  
row in set (0.00 sec) 

# 不能選擇不屬於set('抽菸','喝酒','燙頭','翻車')中的項,  
mysql> insert into t11 values ('alex','燙頭,翻車,看妹子');  
ERROR 1265 (01000): Data truncated for column 'hobby' at row 1
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章