如何在PostgreSQL中表示不确定的日期 [英] How to represent dates with uncertainty in PostgreSQL

查看:275
本文介绍了如何在PostgreSQL中表示不确定的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PostgreSQL提供 date 格式的数据类型来存储日期。这些日期的问题是,但据我所知,它们无法确定不确定性的原因。

PostgreSQL provides the date format datatype to store dates. The problem with these dates is however they can't - as far as I know - reason about uncertainty.

有时候,某人不知道某事的完整日期,但知道它发生在1995年1月或 1999或2000( date2 )中。可能有以下几个原因:

Sometimes one does not know the full date of something, but knows it happened in January 1995 or in "1999 or 2000" (date2). There can be several reasons for that:


  • 人们不记得确切的日期;

  • 确切的日期从根本上是未知的:例如,某人在某天最后一次被人看到,几天后被发现死亡;或

  • 我们处理未来的事件,所以仍有可能出现问题。

我想知道是否存在一种数据类型来存储此类日期以及如何处理它们。对于某些操作,例如 date2< ;,将导致 thee-valued逻辑。 20001/01/01 应该为 true date2< 2000/01/01 可能 date2< 1998/01/01 应该为 false

I was wondering if there is a datatype to store such "dates" and how they are handed. It would result in thee-valued logic for some operations like for instance date2 < 20001/01/01 should be true, date2 < 2000/01/01 be possible and date2 < 1998/01/01 should be false.

如果没有这样的数据类型,如何自行构造表?

If no such datatype is available, what are good practices to construct such "table" onself?

推荐答案

有几种方法可以处理模糊日期。在PostgreSQL中,可以使用

There are several different ways to approach fuzzy dates. In PostgreSQL, you can use


  • 一对日期列(earlyest_possible_date,latest_possible_date),

  • 日期列和精度列( 2012-01-01,年份),或

  • a 范围数据类型(日期范围),或

  • a varchar( 2013-01-2?, 2013-? -05'),或

  • 另一个具有任何这些数据类型的表。

  • a pair of date columns (earliest_possible_date, latest_possible_date),
  • a date column and a precision column ('2012-01-01', 'year'), or
  • a range data type (daterange), or
  • a varchar ('2013-01-2?', '2013-??-05'), or
  • another table or tables with any of those data types.

范围数据类型是PostgreSQL的最新版本所特有的。您可以在任何SQL dbms中使用其他数据库。

The range data type is peculiar to recent versions of PostgreSQL. You can use the others in any SQL dbms.

所需的模糊性取决于应用程序。如何查询模糊日期取决于您选择的数据类型或结构。您需要牢牢把握需要存储的模糊性以及用户需要回答的问题类型。而且,您需要进行测试以确保您的数据库能够回答他们的问题。

The kind of fuzziness you need is application-dependent. How you query fuzzy dates depends on which data type or structure you pick. You need a firm grasp on what kinds of fuzziness you need to store, and on the kind of questions your users need answered. And you need to test to make sure your database can answer their questions.

例如,在法律体系中,日期可能记不清或污损了。有人可能会说 2014年1月大约是星期四。我知道是星期四,因为那是垃圾收集日,或者这是去年6月或7月的第一周。要记录这种模糊性,您需要另一张桌子。

For example, in legal systems dates might be remembered poorly or defaced. Someone might say "It was some Thursday in January 2014. I know it was a Thursday, because it was trash pick-up day", or "It was the first week in either June or July last year". To record that kind of fuzziness, you need another table.

否则可能会破坏邮戳,以便您只能阅读 14,2014。您知道它是在14日邮戳的,但您不知道是哪个月。再次,您需要另一个表。

Or a postmark might be marred so that you can read only "14, 2014". You know it was postmarked on the 14th, but you don't know which month. Again, you need another table.

其中一些(全部?)不会给您提供三值逻辑,除非您跳了几圈。 (可能不是有效的布尔值。)

Some (all?) of these won't give you three-valued logic unless you jump through some hoops. ("Possible" isn't a valid Boolean value.)

这篇关于如何在PostgreSQL中表示不确定的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆