主要數據庫日期時間類型比較


關鍵詞:Oracle , SQL Server,MYSQL,Postgresql,KingbaseES

不同數據庫支持的日期時間類型數據的格式不同,用戶在遷移至KingbaseES 需要選擇正確的時間類型。本文以例子形式展示5種數據庫時間類型上的差異。

1、Oracle

Oracle 只支持 date 和 timestamp類型,date 實際是日期 + 時間 的格式。

SQL> create table test(id1 date,id2 timestamp);

Table created.

SQL> insert into test select sysdate,current_timestamp from dual;

1 row created.

SQL> select * from test;

ID1                 ID2
------------------- ---------------------------------------------------------------------------
2021-09-08 16:23:55 08-SEP-21 04.23.55.207029 PM

2、SQL Server

SQL Server 支持date , time , datetime , datetime2 四種類型。datetime2 相比datetime 時間精度更高。

create table t1(id1 date,id2 time,id3 datetime,id4 datetime2)

ID1             ID2                     ID3                        ID4
-----------     -----------------       -------------------------  -------------------------------
2021-09-08      15:59:33.3933333        2021-09-08 15:59:33.393    2021-09-08 15:59:33.3933333

3、MYSQL

timestamp類型與dateTime類型顯示的格式是一樣的。timestamp類型范圍比較小,沒有dateTime類型的范圍那么大。所以輸入值時要保證在timestamp類型的有效范圍內。timestamp類型的范圍是從1970-01-01 08:00:01~~2038-01-19 11:14:07。

mysql> create table test(id1 date,id2 time,id3 datetime,id4 timestamp);
Query OK, 0 rows affected (0.07 sec)

mysql> select * from test;
+------------+----------+---------------------+---------------------+
| id1        | id2      | id3                 | id4                 |
+------------+----------+---------------------+---------------------+
| 2021-09-08 | 14:28:59 | 2021-09-08 14:28:59 | 2021-09-08 14:28:59 |
+------------+----------+---------------------+---------------------+
1 row in set (0.00 sec)

4、Postgresql

testdb=# create table t1(id1 date,id2 time,id3 timestamp);
CREATE TABLE
testdb=# insert into t1 select current_date,current_time,now();
INSERT 0 1
testdb=# select * from t1;
    id1     |       id2       |            id3             
------------+-----------------+----------------------------
 2021-09-08 | 16:07:48.520207 | 2021-09-08 16:07:48.520207

5、KingbaseES

這里的KingbaseES 是 oracle 模式,PG 模式參照 Postgresql 。可以看到KingbaseES Oracle模式支持的類型與 Postgresql 是相同的,但date 類型與Oracle 兼容。

test=# create table test(id1 date,id2 time,id3 timestamp);     
CREATE TABLE
test=# insert into test select sysdate,current_time,now();     
INSERT 0 1
test=# select * from test;                                
         id1         |       id2       |            id3             
---------------------+-----------------+----------------------------
 2021-09-08 16:38:24 | 16:38:24.382536 | 2021-09-08 16:38:24.382536
(1 row)

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM