1. 程式人生 > >5 MySQL--表--數據類型

5 MySQL--表--數據類型

並且 浮點數 war query medium opened from eat ted

存儲引擎決定了表的類型,而表內存放的數據也要有不同的類型,每種數據類型都有自己的寬度,但寬度是可選的

詳細參考:

  • http://www.runoob.com/mysql/mysql-data-types.html
  • http://dev.mysql.com/doc/refman/5.7/en/data-type-overview.html

mysql常用數據類型概覽

#1. 數字:
    整型:tinyinit  int  bigint
    小數:
        float :在位數比較短的情況下不精準
        double :在位數比較長的情況下不精準
            
0.000001230123123123 存成:0.000001230000 decimal:(如果用小數,則用推薦使用decimal) 精準 內部原理是以字符串形式去存 #2. 字符串: char(10):簡單粗暴,浪費空間,存取速度快 root存成root000000 varchar:精準,節省空間,存取速度慢 sql優化:創建表時,定長的類型往前放,變長的往後放 比如性別 比如地址或描述信息 >255個字符,超了就把文件路徑存放到數據庫中。 比如圖片,視頻等找一個文件服務器,數據庫中只存路徑或url。
#3. 時間類型: 最常用:datetime #4. 枚舉類型與集合類型
enum(‘male‘,‘female‘)
set(‘play‘,‘music‘,‘read‘,‘study‘)
 

1、整數類型

整數類型:TINYINT SMALLINT MEDIUMINT INT BIGINT

作用:存儲年齡,等級,id,各種號碼等

技術分享圖片

規則:
            ========================================
            tinyint[(m)] [unsigned] [zerofill]

                小整數,數據類型用於保存一些範圍的整數數值範圍:
                有符號:
                    
-128 ~ 127 無符號: 0 ~ 255 PS: MySQL中無布爾值,使用tinyint(1)構造。 ======================================== int[(m)][unsigned][zerofill] 整數,數據類型用於保存一些範圍的整數數值範圍: 有符號: -2147483648 ~ 2147483647 無符號: 0 ~ 4294967295 ======================================== bigint[(m)][unsigned][zerofill] 大整數,數據類型用於保存一些範圍的整數數值範圍: 有符號: -9223372036854775808 ~ 9223372036854775807 無符號: 0 ~ 18446744073709551615 ========================================

驗證:

技術分享圖片
=========有符號和無符號tinyint==========
#tinyint默認為有符號
MariaDB [db1]> create table t1(x tinyint); #默認為有符號,即數字前有正負號
MariaDB [db1]> desc t1;
MariaDB [db1]> insert into t1 values
    -> (-129),
    -> (-128),
    -> (127),
    -> (128);
MariaDB [db1]> select * from t1;
+------+
| x    |
+------+
| -128 | #-129存成了-128
| -128 | #有符號,最小值為-128
|  127 | #有符號,最大值127
|  127 | #128存成了127
+------+



#設置無符號tinyint
MariaDB [db1]> create table t2(x tinyint unsigned);
MariaDB [db1]> insert into t2 values
    -> (-1),
    -> (0),
    -> (255),
    -> (256);
MariaDB [db1]> select * from t2;
+------+
| x    |
+------+
|    0 | -1存成了0
|    0 | #無符號,最小值為0
|  255 | #無符號,最大值為255
|  255 | #256存成了255
+------+



============有符號和無符號int=============
#int默認為有符號
MariaDB [db1]> create table t3(x int); #默認為有符號整數
MariaDB [db1]> insert into t3 values
    -> (-2147483649),
    -> (-2147483648),
    -> (2147483647),
    -> (2147483648);
MariaDB [db1]> select * from t3;
+-------------+
| x           |
+-------------+
| -2147483648 | #-2147483649存成了-2147483648
| -2147483648 | #有符號,最小值為-2147483648
|  2147483647 | #有符號,最大值為2147483647
|  2147483647 | #2147483648存成了2147483647
+-------------+



#設置無符號int
MariaDB [db1]> create table t4(x int unsigned);
MariaDB [db1]> insert into t4 values
    -> (-1),
    -> (0),
    -> (4294967295),
    -> (4294967296);
MariaDB [db1]> select * from t4;
+------------+
| x          |
+------------+
|          0 | #-1存成了0
|          0 | #無符號,最小值為0
| 4294967295 | #無符號,最大值為4294967295
| 4294967295 | #4294967296存成了4294967295
+------------+




==============有符號和無符號bigint=============
MariaDB [db1]> create table t6(x bigint);
MariaDB [db1]> insert into t5 values  
    -> (-9223372036854775809),
    -> (-9223372036854775808),
    -> (9223372036854775807),
    -> (9223372036854775808);

MariaDB [db1]> select * from t5;
+----------------------+
| x                    |
+----------------------+
| -9223372036854775808 |
| -9223372036854775808 |
|  9223372036854775807 |
|  9223372036854775807 |
+----------------------+



MariaDB [db1]> create table t6(x bigint unsigned);
MariaDB [db1]> insert into t6 values  
    -> (-1),
    -> (0),
    -> (18446744073709551615),
    -> (18446744073709551616);

MariaDB [db1]> select * from t6;
+----------------------+
| x                    |
+----------------------+
|                    0 |
|                    0 |
| 18446744073709551615 |
| 18446744073709551615 |
+----------------------+




======用zerofill測試整數類型的顯示寬度=============
MariaDB [db1]> create table t7(x int(3) zerofill);
MariaDB [db1]> insert into t7 values
    -> (1),
    -> (11),
    -> (111),
    -> (1111);
MariaDB [db1]> select * from t7;
+------+
| x    |
+------+
|  001 |
|  011 |
|  111 |
| 1111 | #超過寬度限制仍然可以存
+------+
View Code

註意:int(3)表示的是顯示寬度,不是存儲寬度,自己沒有必要指定,其他所有類型指定的寬度表示存儲寬度!

技術分享圖片

2、浮點型

定點數類型 DEC等同於DECIMAL

浮點類型:FLOAT DOUBLE

作用:存儲薪資、身高、體重、體質參數等

======================================
#FLOAT[(M,D)] [UNSIGNED] [ZEROFILL]

定義:
        單精度浮點數(非準確小數值),m是數字總個數,d是小數點後個數。m最大值為255,d最大值為30

有符號:
           -3.402823466E+38 to -1.175494351E-38,
           1.175494351E-38 to 3.402823466E+38
無符號:
           1.175494351E-38 to 3.402823466E+38


精確度: 
           **** 隨著小數的增多,精度變得不準確 ****


======================================
#DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL]

定義:
           雙精度浮點數(非準確小數值),m是數字總個數,d是小數點後個數。m最大值為255,d最大值為30

有符號:
           -1.7976931348623157E+308 to -2.2250738585072014E-308
           2.2250738585072014E-308 to 1.7976931348623157E+308

無符號:
           2.2250738585072014E-308 to 1.7976931348623157E+308

精確度:
           ****隨著小數的增多,精度比float要高,但也會變得不準確 ****

======================================
decimal[(m[,d])] [unsigned] [zerofill]

定義:
          準確的小數值,m是數字總個數(負號不算),d是小數點後個數。 m最大值為65,d最大值為30。


精確度:
           **** 隨著小數的增多,精度始終準確 ****
           對於精確數值計算時需要用此類型
           decaimal能夠存儲精確值的原因在於其內部按照字符串存儲。

驗證:

技術分享圖片
mysql> create table t1(x float(256,31));
ERROR 1425 (42000): Too big scale 31 specified for column x. Maximum is 30.
mysql> create table t1(x float(256,30));
ERROR 1439 (42000): Display width out of range for column x (max = 255)
mysql> create table t1(x float(255,30)); #建表成功
Query OK, 0 rows affected (0.02 sec)

mysql> create table t2(x double(255,30)); #建表成功
Query OK, 0 rows affected (0.02 sec)

mysql> create table t3(x decimal(66,31));
ERROR 1425 (42000): Too big scale 31 specified for column x. Maximum is 30.
mysql> create table t3(x decimal(66,30));
ERROR 1426 (42000): Too-big precision 66 specified for x. Maximum is 65.
mysql> create table t3(x decimal(65,30)); #建表成功
Query OK, 0 rows affected (0.02 sec)

mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| t1            |
| t2            |
| t3            |
+---------------+
rows in set (0.00 sec)



mysql> insert into t1 values(1.1111111111111111111111111111111); #小數點後31個1
Query OK, 1 row affected (0.01 sec)

mysql> insert into t2 values(1.1111111111111111111111111111111);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t3 values(1.1111111111111111111111111111111);
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> select * from t1; #隨著小數的增多,精度開始不準確
+----------------------------------+
| x                                |
+----------------------------------+
| 1.111111164093017600000000000000 |
+----------------------------------+
row in set (0.00 sec)

mysql> select * from t2; #精度比float要準確點,但隨著小數的增多,同樣變得不準確
+----------------------------------+
| x                                |
+----------------------------------+
| 1.111111111111111200000000000000 |
+----------------------------------+
row in set (0.00 sec)

mysql> select * from t3; #精度始終準確,d為30,於是只留了30位小數
+----------------------------------+
| x                                |
+----------------------------------+
| 1.111111111111111111111111111111 |
+----------------------------------+
row in set (0.00 sec)
View Code

3、日期類型

create table student(
    id int,
    name char(6),
    born_year year,
    birth_date date,
    class_time time,
    reg_time datetime
);

insert into student values
(1,egon,now(),now(),now(),now());

insert into student values
(2,alex,"1997","1997-12-12","12:12:12","2017-12-12 12:12:12");

DATE TIME DATETIME TIMESTAMP YEAR

作用:存儲用戶註冊時間,文章發布時間,員工入職時間,出生時間,過期時間等
    YEAR
            YYYY(1901/2155)# 範圍

        DATE
            YYYY-MM-DD(1000-01-01/9999-12-31)

        TIME
            HH:MM:SS(-838:59:59/838:59:59)

        DATETIME

            YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59    Y)

        TIMESTAMP

            YYYYMMDD HHMMSS(1970-01-01 00:00:00/2037 年某時)

驗證:

============year===========
MariaDB [db1]> create table t10(born_year year); #無論year指定何種寬度,最後都默認是year(4)
MariaDB [db1]> insert into t10 values  
    -> (1900),
    -> (1901),
    -> (2155),
    -> (2156);
MariaDB [db1]> select * from t10;
+-----------+
| born_year |
+-----------+
|      0000 |
|      1901 |
|      2155 |
|      0000 |
+-----------+


============date,time,datetime===========
MariaDB [db1]> create table t11(d date,t time,dt datetime);
MariaDB [db1]> desc t11;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| d     | date     | YES  |     | NULL    |       |
| t     | time     | YES  |     | NULL    |       |
| dt    | datetime | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+

MariaDB [db1]> insert into t11 values(now(),now(),now());
MariaDB [db1]> select * from t11;
+------------+----------+---------------------+
| d          | t        | dt                  |
+------------+----------+---------------------+
| 2017-07-25 | 16:26:54 | 2017-07-25 16:26:54 |
+------------+----------+---------------------+



============timestamp===========
MariaDB [db1]> create table t12(time timestamp);
MariaDB [db1]> insert into t12 values();
MariaDB [db1]> insert into t12 values(null);
MariaDB [db1]> select * from t12;
+---------------------+
| time                |
+---------------------+
| 2017-07-25 16:29:17 |
| 2017-07-25 16:30:01 |
+---------------------+



============註意啦,註意啦,註意啦===========
1. 單獨插入時間時,需要以字符串的形式,按照對應的格式插入
2. 插入年份時,盡量使用4位值
3. 插入兩位年份時,<=69,以20開頭,比如50,  結果2050      
                >=70,以19開頭,比如71,結果1971
MariaDB [db1]> create table t12(y year);
MariaDB [db1]> insert into t12 values  
    -> (50),
    -> (71);
MariaDB [db1]> select * from t12;
+------+
| y    |
+------+
| 2050 |
| 1971 |
+------+



============綜合練習===========
MariaDB [db1]> create table student(
    -> id int,
    -> name varchar(20),
    -> born_year year,
    -> birth date,
    -> class_time time,
    -> reg_time datetime);

MariaDB [db1]> insert into student values
    -> (1,alex,"1995","1995-11-11","11:11:11","2017-11-11 11:11:11"),
    -> (2,egon,"1997","1997-12-12","12:12:12","2017-12-12 12:12:12"),
    -> (3,wsb,"1998","1998-01-01","13:13:13","2017-01-01 13:13:13");

MariaDB [db1]> select * from student;
+------+------+-----------+------------+------------+---------------------+
| id   | name | born_year | birth      | class_time | reg_time            |
+------+------+-----------+------------+------------+---------------------+
|    1 | alex |      1995 | 1995-11-11 | 11:11:11   | 2017-11-11 11:11:11 |
|    2 | egon |      1997 | 1997-12-12 | 12:12:12   | 2017-12-12 12:12:12 |
|    3 | wsb  |      1998 | 1998-01-01 | 13:13:13   | 2017-01-01 13:13:13 |
+------+------+-----------+------------+------------+---------------------+

datetime與timestamp的區別

在實際應用的很多場景中,MySQL的這兩種日期類型都能夠滿足我們的需要,存儲精度都為秒,但在某些情況下,會展現出他們各自的優劣。
下面就來總結一下兩種日期類型的區別。

1.DATETIME的日期範圍是1001——9999年,TIMESTAMP的時間範圍是1970——2038年。

2.DATETIME存儲時間與時區無關,TIMESTAMP存儲時間與時區有關,顯示的值也依賴於時區。在mysql服務器,
操作系統以及客戶端連接都有時區的設置。

3.DATETIME使用8字節的存儲空間,TIMESTAMP的存儲空間為4字節。因此,TIMESTAMP比DATETIME的空間利用率更高。

4.DATETIME的默認值為null;TIMESTAMP的字段默認不為空(not null),默認值為當前時間(CURRENT_TIMESTAMP),
如果不做特殊處理,並且update語句中沒有指定該列的更新值,則默認更新為當前時間。


5 MySQL--表--數據類型