主要数据库日期时间类型比较
关键词:Oracle , SQL Server,MYSQL,Postgresql,KingbaseES
不同数据库支持的日期时间类型数据的格式不同,用户在迁移至KingbaseES 需要选择正确的时间类型。本文以例子形式展示5种数据库时间类型上的差异。
1、Oracle
Oracle 只支持 date 和 timestamp类型,date 实际是日期 + 时间 的格式。
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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 时间精度更高。
1 2 3 4 5 | 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。
1 2 3 4 5 6 7 8 9 10 | 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
1 2 3 4 5 6 7 8 | 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 兼容。
1 2 3 4 5 6 7 8 9 | 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) |
KINGBASE研究院
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 中的改进
· 当数据爆炸遇上SQL Server:优化策略全链路解析
· 记录一次线上问题排查:JDK序列化问题
· 微服务之间有哪些调用方式?
· 记一次SQL隐式转换导致精度丢失问题的排查
· 四十种AI编程工具,让你码字如飞
· 当数据爆炸遇上SQL Server:优化策略全链路解析
· 分享5款开源、美观的 WinForm UI 控件库
· DeepSeek又在节前放大招!
· 2025AI应用全景图谱报告