PostgreSQL数据类型-时间数据类型
2017-11-09 00:00
381 查看
摘要: 时间数据类型分为日期、时间和日期时间三种类型。
PostgreSQL数据类型支持6种时间数据类型。如下图所示:
time、timestamp和interval接受可选精度值 p,精度值是秒域中小数点后保留位数。缺省情况下,精度没有明确限制,timestamp和interval类型p允许范围从 0 到 6。
timestamp默认以8字节整数储存,整个数值范围内保持微秒以上精度,当被修改为双精度浮点数时,极端情况下,精度会下降到毫秒级以上。
interval类型附加选项fields通过下列短语限制存储fields集合:
注意如果fields和p被指定,fields必须包括SECOND,因为精度只应用于秒。
类型time with time zone是SQL标准,但可能影响可用性。大多数情况下, date、time、timestamp without time zone和timestamp with time zone组合就能提供任何应用所需全范围日期/时间功能。
时间输入格式为
如下代码演示插入时间或日期。
还可以查看数据库系统时间显示格式。例如YMD为年月日格式。
插入数值必须符合设定日期格式,否则可能会导致出错或输入错误。
如上代码可知,不同DateStyle可能会输出结果不相同,或者提示错误。
推荐"-"作为分隔符,推荐年月日方式输入日期数值。"/"可能会导致歧义。也可以使用"2017-July-1"类似明确无歧义格式输入日期和时间。
查询已输入数据结果。
time数据类型等效于time without time zone。当输入带市区格式时,时区信息自动被剔除。
输入有时区时间数据使用有时区数据类型。
推荐例如"07:59:59.111111+08:00"格式输入数据,时区缩写和声明时区等格式可能导致输入错误发生。
参考链接:
https://www.postgresql.org/docs/current/static/datatype-datetime.html
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,'1979 June 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,'1979 June 1','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,'1979 June 1','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'),(2,'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
相关文章推荐
- 《卸甲笔记》-PostgreSQL和Oracle的数据类型的对比系列三:时间类型
- Oracle-07-数据类型例子&查询当前系统时间&创建表语法
- Oracle数据库-date数据类型(时间)比较大小
- MySQL日期数据类型、时间类型使用总结
- SqlServer中日期和时间数据类型及函数/高精度获取时间
- Mysql数据库学习(二):数据类型(数值类型 日期和时间类型 字符串类型)
- springMVC form表单提交---包含时间类型的数据
- MySQL数据类型--日期时间
- MySQL日期数据类型、时间类型使用总结
- EF里Guid类型数据的自增长、时间戳和复杂类型的用法
- postgresql的时间数据处理
- postgresql的时间数据处理
- 基本数据类型转换以及时间格式化
- MySQL学习----MySQL数据类型----03MySQL 日期和时间类型
- PostgreSQL: hstore 数据类型使用介绍
- MySQL的数据类型,MySQL增删改--添加主外键、添加属性、删除主外键、改表名、获取系统当前时间等
- PostgreSQL数据类型
- oracle 日期时间数据类型
- postgresql 数据类型转换,日期操作函数
- 【MySQL数据类型2之--日期时间类型】