主要数据库日期时间类型比较
关键词: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研究院
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!