【问题标题】:Interval type without time in PostgreSQLPostgreSQL中没有时间的间隔类型
【发布时间】:2021-10-25 16:06:17
【问题描述】:

我在应用程序端使用 PostgreSQL 类型 INTERVALjava.time.Period 类型。

我想要的是限制插入除年、月和日之外的所有字段的INTERVAL 列。

我知道INTERVAL type definition 中有[ fields ] 部分,但我只能将YEAR TO MONTH 放入其中:

CREATE TABLE test_interval (
  col1 INTERVAL YEAR TO MONTH -- only years and month allowed
)

我需要:

-- not a real SQL
CREATE TABLE test_interval (
  col1 INTERVAL YEAR TO MONTH -- only years, month and days allowed
)

有什么解决方法吗?

【问题讨论】:

    标签: sql postgresql intervals period


    【解决方案1】:

    试试这个:

    create table test_interval 
    (col1 interval
    ,constraint col1_check check(date_trunc('day', col1) = col1)
    )
    

    【讨论】:

      猜你喜欢
      • 2012-07-19
      • 2020-02-17
      • 1970-01-01
      • 2021-06-12
      • 1970-01-01
      • 2019-06-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      相关资源
      最近更新 更多