微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PostgreSQL数据类型-时间数据类型

Postgresql数据类型支持6种时间数据类型。如下图所示:

time、timestamp和interval接受可选精度值 p,精度值是秒域中小数点后保留位数。缺省情况下,精度没有明确限制,timestamp和interval类型p允许范围从 0 到 6。

timestamp认以8字节整数储存,整个数值范围内保持微秒以上精度,当被修改为双精度浮点数时,极端情况下,精度会下降到毫秒级以上。

interval类型附加选项fields通过下列短语限制存储fields集合:

YEAR
MONTH
DAY
HOUR
MINUTE
SECOND
YEAR TO MONTH
DAY TO HOUR
DAY TO MINUTE
DAY TO SECOND
HOUR TO MINUTE
HOUR TO SECOND
MINUTE TO SECOND

注意如果fields和p被指定,fields必须包括SECOND,因为精度只应用于秒。

类型time with time zone是sql标准,但可能影响可用性。大多数情况下, date、time、timestamp without time zone和timestamp with time zone组合就能提供任何应用所需全范围日期/时间功能

时间输入格式为

type[ fields ] [ ( 精度 ) ] [ | with time zone] 'timevalue'

如下代码演示插入时间或日期。

test=# create table testdatetime(id int,testdate date,testtime time);
CREATE TABLE
test=# insert into testdatetime values(1,'1644-1-1','01:11:11'),(2,'2017-11-9','15:02:22+08');
INSERT 0 2
test=#

还可以查看数据库系统时间显示格式。例如YMD为年月日格式。

---查看DateStyle
test=# show datestyle;
 DateStyle
-----------
 ISO,YMD
(1 行记录)

---修改DateStyle
test=# set DateStyle='YMD';
SET
test=#

插入数值必须符合设定日期格式,否则可能会导致出错或输入错误

test=# insert into testdatetime values(1,'1795/1/3','01:11:11');                
INSERT 0 1
test=# insert into testdatetime values(1,'1979 June 1','01:11:11');
INSERT 0 1
test=# insert into testdatetime values(1,'01:11:11');

如上代码可知,不同DateStyle可能会输出结果不相同,或者提示错误

推荐"-"作为分隔符,推荐年月日方式输入日期数值。"/"可能会导致歧义。也可以使用"2017-July-1"类似明确无歧义格式输入日期和时间。

---输入公元前时间、儒略历日期
---AD 放在最后
test=# insert into testdatetime values(3,'117-1-9 BC','16:00:00'),(5,'J2455000','10:17:20');
INSERT 0 3
test=#

查询已输入数据结果。

test=# select * from testdatetime;                                               id |   testdate    | testtime
----+---------------+----------
  1 | 1644-01-01    | 01:11:11
  2 | 2017-11-09    | 15:02:22
  1 | 1795-01-03    | 01:11:11
  1 | 1979-06-01    | 01:11:11
  1 | 1979-06-01    | 01:11:11
  3 | 0117-01-09 BC | 16:00:00
  5 | 2009-06-17    | 10:17:20
(7 行记录)

test=#

time数据类型等效于time without time zone。当输入带市区格式时,时区信息自动被剔除。

test=# insert into testdatetime values(4,'01:11:11+8');
test=# select * from testdatetime where id =4;
 id |  testdate  | testtime
----+------------+----------
  4 | 1979-06-01 | 01:11:11
(1 行记录)

test=#
---不推荐,无时分秒分隔符,分隔符推荐:
test=# insert into testdatetime values(4,'011111+8');
INSERT 0 1
test=#

输入有时区时间数据使用有时区数据类型。

test=# create table testdatetimewithtimezone(id int,timewithzone time with time zone);
CREATE TABLE
---插入不同类型带时区时间数据
test=# insert into testdatetimewithtimezone values(1,'09:17:22.011111'),'17:33:59+11'),(3,'231545+07:45');
INSERT 0 3
test=#

推荐例如"07:59:59.111111+08:00"格式输入数据,时区缩写和声明时区等格式可能导致输入错误发生。

test=# select * from testdatetimewithtimezone;
 id |    timewithzone
----+--------------------
  1 | 09:17:22.011111+08
  2 | 17:33:59+11
  3 | 23:15:45+07:45
(3 行记录)

test=#

参考链接

https://www.postgresql.org/docs/current/static/datatype-datetime.html

原文地址:https://www.jb51.cc/postgresql/193425.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐