主要数据库日期时间类型比较

关键词: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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值