因為在做Oracle---->MySQL的數(shù)據(jù)遷移的時候,發(fā)現(xiàn)Oracle中的date類型,對應(yīng)的mysql的時間類型設(shè)置不當(dāng)容易引起錯誤,特別是存在空值的時候
創(chuàng)新互聯(lián)致力于網(wǎng)站建設(shè)、網(wǎng)站制作,成都網(wǎng)站設(shè)計,集團網(wǎng)站建設(shè)等服務(wù)標(biāo)準(zhǔn)化,推過標(biāo)準(zhǔn)化降低中小企業(yè)的建站的成本,并持續(xù)提升建站的定制化服務(wù)水平進行質(zhì)量交付,讓企業(yè)網(wǎng)站從市場競爭中脫穎而出。 選擇創(chuàng)新互聯(lián),就選擇了安全、穩(wěn)定、美觀的網(wǎng)站建設(shè)服務(wù)!
mysql 版本 5.6.40版本
mysql> desc t1;
+-------------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-----------+------+-----+-------------------+-----------------------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| time_1 | time | YES | | NULL | |
| date_2 | date | YES | | NULL | |
| datetime_3 | datetime | YES | | NULL | |
| timestamp_4 | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+-----------+------+-----+-------------------+-----------------------------+
5 rows in set (0.00 sec)
可以插入當(dāng)前的時間
mysql> insert into t1 values(null,now(),now(),now(),now());
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> show warnings;
+-------+------+--------------------------------------------------------------------------+
| Level | Code | Message |
+-------+------+--------------------------------------------------------------------------+
| Note | 1292 | Incorrect date value: '2018-05-11 11:18:41' for column 'date_2' at row 1 |
+-------+------+--------------------------------------------------------------------------+
1 row in set (0.00 sec)
提示date類型插入告警,但是依舊可以插入進去,因為date類型只記錄年月(yyyy-mm)
Query OK, 1 row affected (0.01 sec)
4個時間空值插入測試 ,time類型,插入0
mysql> insert into t1 values (null,'0','2018-01-01','2018-01-01 12:12:12','2018-10-10 00:00:00');
Query OK, 1 row affected (0.01 sec)
mysql> select * from t1;
+----+----------+------------+---------------------+---------------------+
| id | time_1 | date_2 | datetime_3 | timestamp_4 |
+----+----------+------------+---------------------+---------------------+
| 1 | 22:21:23 | 2018-05-08 | 2018-05-08 22:21:23 | 2018-05-08 22:21:23 |
| 2 | 22:21:54 | 2018-05-08 | 2018-05-08 22:21:54 | 2018-05-08 22:21:54 |
| 3 | 00:00:00 | 2018-01-01 | 2018-01-01 12:12:12 | 2018-10-10 00:00:00 | time_1 自動填充為00:00:00
| 4 | 00:00:00 | 2018-01-01 | 2018-01-01 12:12:12 | 2018-10-10 00:00:00 |
+----+----------+------------+---------------------+---------------------+
4 rows in set (0.00 sec)
接著全部插入0,看是否能夠插入進去
測試date類型---------------
第三列為date類型
mysql> insert into t1 values(null,'0','0','0','0'); 插入 0
ERROR 1292 (22007): Incorrect date value: '0' for column 'date_2' at row 1
mysql> insert into t1 values(null,'0','','0','0'); 插入‘ ’測試,留空白,測試插入
ERROR 1292 (22007): Incorrect date value: '' for column 'date_2' at row 1
-------------測試datetime類型--
第四列為datetime類型
mysql> insert into t1 values(null,'0',null,'0','0');
ERROR 1292 (22007): Incorrect datetime value: '0' for column 'datetime_3' at row 1
插入null成功
---------測試timestamp類型
第五列為timestamp
mysql> insert into t1 values(null,'0',null,null,'0');
ERROR 1292 (22007): Incorrect datetime value: '0' for column 'timestamp_4' at row 1
mysql> insert into t1 values(null,'0',null,null,null);
Query OK, 1 row affected (0.00 sec)
插入null 成功
mysql> select * from t1;
+----+----------+------------+---------------------+---------------------+
| id | time_1 | date_2 | datetime_3 | timestamp_4 |
+----+----------+------------+---------------------+---------------------+
| 1 | 22:21:23 | 2018-05-08 | 2018-05-08 22:21:23 | 2018-05-08 22:21:23 |
| 2 | 22:21:54 | 2018-05-08 | 2018-05-08 22:21:54 | 2018-05-08 22:21:54 |
| 3 | 00:00:00 | 2018-01-01 | 2018-01-01 12:12:12 | 2018-10-10 00:00:00 |
| 4 | 00:00:00 | 2018-01-01 | 2018-01-01 12:12:12 | 2018-10-10 00:00:00 |
| 5 | 00:00:00 | NULL | NULL | 2018-05-08 22:33:22 |
+----+----------+------------+---------------------+---------------------+
5 rows in set (0.00 sec)
總結(jié) : Oracle數(shù)據(jù)庫的date類型和mysql的date類型是不一樣的,Oracle為yyyy-mm-dd hh:mi:ss和mysql中的datetime類型匹配, 而 mysql 為 yyyy-mm 。當(dāng)在存在空值的時候,mysql的time 類型可以使用0零來插入,而date,datetime,timestamp可以使用null 來插入,但是timestamp即使為null,也會默認(rèn)插入當(dāng)前時間戳。
本文名稱:oracle--date-對應(yīng)mysql時間類型的以及空值的處理
當(dāng)前鏈接:http://jinyejixie.com/article2/ipjjoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護、品牌網(wǎng)站設(shè)計、網(wǎng)站排名、企業(yè)建站、網(wǎng)站改版、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)