加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgresql – now()和current_timestamp之间的区别

发布时间:2020-12-13 16:21:34 所属栏目:百科 来源:网络整理
导读:在PostgreSQL中,我使用now()和current_timestamp函数,我看到没有区别: # SELECT now(),current_timestamp; now | now --------------------------------+-------------------------------- 04/20/2014 19:44:27.215557 EDT | 04/20/2014 19:44:27.215557 ED
在PostgreSQL中,我使用now()和current_timestamp函数,我看到没有区别:
# SELECT now(),current_timestamp;
              now               |              now               
--------------------------------+--------------------------------
 04/20/2014 19:44:27.215557 EDT | 04/20/2014 19:44:27.215557 EDT
(1 row)

我错过了什么吗?

没有区别. Three quotes from the documentation:

1)

These SQL-standard functions all return values based on the start time
of the current transaction:

CURRENT_TIMESTAMP

2)

transaction_timestamp() is equivalent to CURRENT_TIMESTAMP,but is
named to clearly reflect what it returns.

3)

now() is a traditional PostgreSQL equivalent to transaction_timestamp().

大胆强调我的.

所以,基本上,CURRENT_TIMESTAMP,transaction_timestamp()和now()完全相同. CURRENT_TIMESTAMP是一个函数的语法奇怪,没有尾对括号.这是根据SQL标准.

如果未在SQL语句中声明函数调用的列别名(需要一个),则别名默认为函数的名称.在内部,标准SQL CURRENT_TIMESTAMP使用now()实现,并在您查看默认列别名时显示.

transaction_timestamp()执行相同的操作,但是这个是一个正确的Postgres函数,因此分配了默认的别名transaction_timestamp.

不要将这些函数中的任何一个与特殊的input constant 'now'混淆.这只是特定日期/时间/时间戳值的几个符号缩写之一,quoting the manual:

… that will be converted to ordinary date/time values when read. (In particular,now and related strings are converted to a specific time value as soon as they are read.) All of these values need to be enclosed in single quotes when used as constants in SQL commands.

它可能会增加混淆(当前 – 第10页)从这些特殊输入值中修剪任意数量的前导和尾随空格和括号({[()]}).因此’now()’:: timestamptz – 或者只是’now()’,其中不需要显式类型转换 – 也是有效的,并且恰好在大多数情况下评估与函数now()相同的时间戳.但这些是常量,通常不是你想要的列默认值.

SQL Fiddle.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读