参考自:https://www.postgresql.org/docs/10/static/functions-formatting.html
-
To get three milliseconds, one must write
12.003, which the conversion treats as 12 + 0.003 = 12.003 seconds.
第一点:众所周知,timestamp数据类型最细粒度到微秒(us),也就是timestamp(6)
那么
postgres=# create table abc_s.test_lei (c1 timestamp(7));
WARNING: TIMESTAMP(7) precision reduced to maximum allowed, 6
第1行create table abc_s.test_lei (c1 timestamp(7));
^
WARNING: TIMESTAMP(7) precision reduced to maximum allowed, 6
CREATE TABLE
postgres=#
那么如上的语句虽然报错了,但是这个表还是create成功了,见下:
postgres=# \d abc_s.test_lei
数据表 "abc_s.test_lei"
栏位 | 类型 | Collation | Nullable | Default
------+--------------------------------+-----------+----------+---------
c1 | timestamp(6) without time zone | | |
---->>>>可以看到,自动给变成6了。因为最大到6--微秒
postgres=#
第二点:
sql语句中想插入400毫秒,就要写:
insert into test_t1 values(to_timestamp('2049-11-22 21:21:21.4','YYYY-MM-DD HH24:MI:SS.MS'));
sql语句中想插入40毫秒,就要写:
insert into test_t1 values(to_timestamp('2049-11-22 21:21:21.04','YYYY-MM-DD HH24:MI:SS.MS'));
sql语句中想插入4毫秒,就要写:
insert into test_t1 values(to_timestamp('2049-11-22 21:21:21.004','YYYY-MM-DD HH24:MI:SS.MS'));
因为1000毫秒=1秒

本文介绍了在PostgreSQL中timestamp数据类型的精度上限为6位(即微秒级别),并展示了当尝试创建超过此精度的表时系统的处理方式。此外,还提供了如何在SQL语句中正确插入不同毫秒值的具体示例。
8467

被折叠的 条评论
为什么被折叠?



