您好,登錄后才能下訂單哦!
因為在做Oracle---->mysql的數據遷移的時候,發現Oracle中的date類型,對應的mysql的時間類型設置不當容易引起錯誤,特別是存在空值的時候
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)
可以插入當前的時間
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)
總結 : Oracle數據庫的date類型和mysql的date類型是不一樣的,Oracle為yyyy-mm-dd hh:mi:ss和mysql中的datetime類型匹配, 而 mysql 為 yyyy-mm 。當在存在空值的時候,mysql的time 類型可以使用0零來插入,而date,datetime,timestamp可以使用null 來插入,但是timestamp即使為null,也會默認插入當前時間戳。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。