• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase
您的位置:首页 > 数据库 >PostgreSQL > 浅谈 PostgreSQL 的 timestamp 类型

浅谈 PostgreSQL 的 timestamp 类型

作者:menghuannvxia的专栏 字体:[增加 减小] 来源:互联网 时间:2017-09-08

menghuannvxia的专栏通过本文主要向大家介绍了postgresql,timestamp等相关知识,希望本文的分享对您有所帮助

转载自:http://blog.csdn.net/menghuannvxia/article/details/77883743


一 PostgreSQL 中的时间类型如下
Name                                          Storage Size   Description                                 Low Value    High Value     Resolution
timestamp [ (p) ] [ without time zone ] 8 bytes   both date and time (no time zone)  4713 BC 294276 AD    1 microsecond / 14 digits
timestamp [ (p) ]  with time zone         8 bytes   both date and time, with time zone  4713 BC 294276 AD    1 microsecond / 14 digits
date                                                4 bytes   date (no time of day)                        4713 BC 5874897 AD      1 day
time [ (p) ][ without time zone ]            8 bytes   time of day (no date)                        00:00:0024:00:00     1 microsecond / 14 digits
time [ (p) ]with time zone           12 bytes   times of day only, with time zone  00:00:00+1459 24:00:00-1459    1 microsecond / 14 digits
interval [ fields ] [ (p) ]                           12 bytes   time interval                                 -178000000 years178000000 years    1 microsecond / 14 digits
 

  备注:这里不准备详细介绍各种类型,请注意上面表格中的[ (p) ] ,这个是什么呢?这个是指时间的精度,
  time, timestamp, 和 interval 类型都可以指定精度,精度的取值范围是 0 到 6, 下面通过具体实验来体验下精度。


二 current_timestamp 实验
--2.1 查询  current_timestamp


 skytf=> select current_timestamp;
              now              
-------------------------------
 2012-06-07 14:00:02.412827+08
(1 row)        
   
 备注: current_timestamp 函数返回时间类型为 timestamp with  time zone,故返回结果后面包括时区 +08 ,以及精度 412827,那么如何去掉精度和时区呢?
 


--2.2 去掉精度


 skytf=> select current_timestamp(0);
      timestamptz       
------------------------
 2012-06-07 14:07:17+08
(1 row)
   


--2.3 去掉时区


 skytf=> select current_timestamp(0)::timestamp without time zone;
      timestamp      
---------------------
 2012-06-07 14:07:49
(1 row)
   
select to_timestamp('2015/9/18 13:57:05','yyyy/mm/dd hh24:mi:ss')::timestamp without time zone;
--2015-09-18 13:57:05




--2.4 也可以用 cast 函数类型转换


 skytf=> select cast (current_timestamp(0) as  timestamp without time zone);
      timestamp      
---------------------
 2012-06-07 14:14:55
(1 row)
   
                 
            
--2.5 了解 [p] 的含义
 skytf=> select current_timestamp(2)::timestamp without time zone;
       timestamp        
------------------------
 2012-06-07 14:15:42.64
(1 row)
skytf=> select current_timestamp(6)::timestamp without time zone;
         timestamp          
----------------------------
 2012-06-07 14:15:46.281422
(1 row)


   
备注:可见 [p] 是指时间类型小数点后面的精度,如果 p 指定 2,则精度为2,如果 p 指定 6则精度为 6; 
所以在定义表的时候就应该事先定义 timestamp 时间类型的精度。
        
三 创建表测试,定义时间类型精度为0


 skytf=> create table test_p (id int4 primary key, create_time  timestamp(0) without time zone);
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test_p_pkey" for table "test_p"
CREATE TABLE
skytf=> \d test_p
                   Table "skytf.test_p"
   Column    |              Type              | Modifiers 
-------------+--------------------------------+-----------
 id          | integer                        | not null
 create_time | timestamp(0) without time zone | 
Indexes:
    "test_p_pkey" PRIMARY KEY, btree (id)


skytf=> select current_timestamp;
              now              
-------------------------------
 2012-06-07 14:18:31.683105+08
(1 row)
                              
skytf=> insert into test_p values (1,current_timestamp);
INSERT 0 1


skytf=> select * from test_p;
 id |     create_time     
----+---------------------
  1 | 2012-06-07 14:19:02
(1 row)                    

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • CentOS 7下安装PostgreSQL 9.6的教程分享
  • windows PostgreSQL 9.1 安装详细步骤
  • PostgreSQL安装、配置及简单使用方法
  • Mybatis调用PostgreSQL存储过程实现数组入参传递
  • Linux CentOS 7源码编译安装PostgreSQL9.5
  • Linux CentOS 7安装PostgreSQL9.3图文教程
  • PostgreSQL中常用的时间日期脚本使用教程
  • 深入解读PostgreSQL中的序列及其相关函数的用法
  • Postgresql ALTER语句常用操作小结
  • PostgreSQL教程(二十):PL/pgSQL过程语言

相关文章

  • 2017-05-11phpPgAdmin 常见错误和问题的解决办法
  • 2017-05-11深入理解PostgreSQL的MVCC并发处理方式
  • 2017-05-11PostgreSQL教程(二十):PL/pgSQL过程语言
  • 2017-05-11PostgreSQL教程(七):函数和操作符详解(3)
  • 2017-05-11PostgreSQL 创建表分区
  • 2017-05-11phpPgAdmin 配置文件参数说明中文版
  • 2017-05-11PostgreSQL中的OID和XID 说明
  • 2017-05-11Mac OS上安装PostgreSQL的教程
  • 2017-05-11Postgre数据库Insert 、Query性能优化详解
  • 2017-05-11PostgreSQL 角色与用户管理介绍

文章分类

  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase

最近更新的内容

    • 用PostgreSQL数据库做地理位置app应用
    • PostgreSQL中调用存储过程并返回数据集实例
    • PostgreSQL 角色与用户管理介绍
    • PostgreSQL 安装和简单使用第1/2页
    • 解决PostgreSQL服务启动后占用100% CPU卡死的问题
    • PostgreSQL中关闭死锁进程的方法
    • 在windows下手动初始化PostgreSQL数据库教程
    • postgresql 数据库时间间隔数据查询
    • PostgreSQL教程(十):性能提升技巧
    • PostgreSQL教程(三):表的继承和分区表详解

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有